【问题标题】:Need help converting a script from PDO to Mysql需要帮助将脚本从 PDO 转换为 Mysql
【发布时间】:2023-03-03 03:35:01
【问题描述】:

我正在尝试在我的 Zend 程序中转换此脚本。

http://github.com/jackmoore/colorrating/raw/master/rating/rating.php

<?php
class rating{

 public $average = 0;
 public $votes;
 public $status;
 public $table;
 private $path;

 function __construct($table){
  try{
   $pathinfo = pathinfo(__FILE__);
   $this->path = realpath($pathinfo['dirname']) . "/database/ratings.sqlite";
   $dbh = new PDO("sqlite:$this->path");
   $this->table = $dbh->quote($table);
   // check if table needs to be created
   $table_check = $dbh->query("SELECT * FROM $this->table WHERE id='1'");
   if(!$table_check){
    // create database table
    $dbh->query("CREATE TABLE $this->table (id INTEGER PRIMARY KEY, rating FLOAT(3,2), ip VARCHAR(15))");
    $dbh->query("INSERT INTO $this->table (rating, ip) VALUES (0, 'master')");    
   } else {
    $this->average = $table_check->fetchColumn(1);
   }
   $this->votes = ($dbh->query("SELECT COUNT(*) FROM $this->table")->fetchColumn()-1);
  }catch( PDOException $exception ){
    die($exception->getMessage());
  }
  $dbh = NULL;  
 }

 function set_score($score, $ip){
  try{
   $dbh = new PDO("sqlite:$this->path");
   $voted = $dbh->query("SELECT id FROM $this->table WHERE ip='$ip'");
   if(sizeof($voted->fetchAll())==0){

    $dbh->query("INSERT INTO $this->table (rating, ip) VALUES ($score, '$ip')");
    $this->votes++;

    //cache average in the master row
    $statement = $dbh->query("SELECT rating FROM $this->table");
    $total = $quantity = 0;
    $row = $statement->fetch(); //skip the master row
    while($row = $statement->fetch()){
     $total = $total + $row[0];
     $quantity++;
    }
    $this->average = round((($total*20)/$quantity),0);
    $statement = $dbh->query("UPDATE $this->table SET rating = $this->average WHERE id=1");
    $this->status = '(thanks!)';
   } else {
    $this->status = '(already scored)';
   }

  }catch( PDOException $exception ){
    die($exception->getMessage());
  }
  $dbh = NULL;
 }
}

function rating_form($table){
 $ip = $_SERVER["REMOTE_ADDR"];
 if(!isset($table) && isset($_GET['table'])){
  $table = $_GET['table'];
 }
 $rating = new rating($table);
 $status = "<div class='score'>
    <a class='score1' href='?score=1&amp;table=$table&amp;user=$ip'>1</a>
    <a class='score2' href='?score=2&amp;table=$table&amp;user=$ip'>2</a>
    <a class='score3' href='?score=3&amp;table=$table&amp;user=$ip'>3</a>
    <a class='score4' href='?score=4&amp;table=$table&amp;user=$ip'>4</a>
    <a class='score5' href='?score=5&amp;table=$table&amp;user=$ip'>5</a>
   </div>
 ";
 if(isset($_GET['score'])){
  $score = $_GET['score'];
  if(is_numeric($score) && $score <=5 && $score >=1 && ($table==$_GET['table']) && isset($_GET["user"]) && $ip==$_GET["user"]){
   $rating->set_score($score, $ip);
   $status = $rating->status;
  }
 }
 if(!isset($_GET['update'])){ echo "<div class='rating_wrapper'>"; }
 ?>
 <div class="sp_rating">
  <div class="rating">Rating:</div>
  <div class="base"><div class="average" style="width:<?php echo $rating->average; ?>%"><?php echo $rating->average; ?></div></div>
  <div class="votes"><?php echo $rating->votes; ?> votes</div>
  <div class="status">
   <?php echo $status; ?>
  </div>
 </div>
 <?php
 if(!isset($_GET['update'])){ echo "</div>"; }
}

if(isset($_GET['update'])&&isset($_GET['table'])){
 rating_form($_GET['table']);
}

如何将其更改为 Mysql?

我通常使用$dbh = Zend_Registry::get ( "db" ); 来获取我的 sql 访问权限。

谢谢大家,希望没有太多改动

【问题讨论】:

  • 希望你不要觉得这个厚颜无耻,我知道人们不想“为你做你的工作”,但我很难确切知道要改变什么,我估计总共大约 4 或 5 行,当然你可以通过例子来学习这些东西

标签: php mysql zend-framework pdo


【解决方案1】:

如果注册表中的db 对象是Zend_Db 之一,那么:

$dbh-&gt;quote() 变成 $db-&gt;quote()(最简单的那个)

$dbh-&gt;query() 操作数据的语句可以转换为$dbh-&gt;query()(同样,很简单)。但是,最好更新代码以使用$db-&gt;insert($table, $data) - 或使用Zend_Db_Table 对象和$table-&gt;insert($data)

返回数据的$dbh-&gt;query() 语句可以变成$db-&gt;fetchAll($sql) 语句,但您需要更新代码以期待Zend_Db RowRowset 对象。

我建议阅读Zend_Db 文档以了解哪些功能映射到不同的 PDO 功能。

如果您只是需要它来使用 MySQL,请将 DSN 字符串更改为您的 MySQL 连接。比如:

$dbh = new PDO("mysql:dbname=testdb;host=127.0.0.1", $user, $pass);

详情请参阅PDO documentation

如果注册表中的 db 对象是一个 PDO 实例,只需抓取该对象并使用它,而不是创建一个新的 PDO 对象。

【讨论】:

  • 我不知道,我仍然在整个节目中遇到错误,我认为 PDO 的全部意义在于语法始终保持不变?
  • 如果您尝试使用 Zend_DB 对象,那么您没有使用 PDO。如果您只需要使用 MySQL,只需更改 DSN 字符串。
  • 干杯,脚本似乎还没有运行得那么好,不过你的新 PDO 喊得很好
猜你喜欢
  • 2018-11-09
  • 2021-12-14
  • 2019-10-16
  • 2017-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
  • 2010-12-22
相关资源
最近更新 更多