需要帮助将脚本从 PDO 转换为 Mysql

发布于 2024-08-23 14:11:57 字数 4006 浏览 1 评论 0原文

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

http://github.com/jackmoore/color rating/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 访问权限。

谢谢大家,希望不会有太多变化

Im trying to convert this script, in my Zend program.

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&table=$table&user=$ip'>1</a>
    <a class='score2' href='?score=2&table=$table&user=$ip'>2</a>
    <a class='score3' href='?score=3&table=$table&user=$ip'>3</a>
    <a class='score4' href='?score=4&table=$table&user=$ip'>4</a>
    <a class='score5' href='?score=5&table=$table&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']);
}

How can I change this to Mysql?

Im using $dbh = Zend_Registry::get ( "db" ); normally to get my sql access.

Thanks everyone, hopefully there isnt too many changes invloved

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆凄凉。 2024-08-30 14:11:57

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

$dbh->quote() 变为$db->quote() (最简单的)

操作数据的 $dbh->query() 语句可以转换为 $dbh->query() 语句。 query()(同样,简单)。但是,最好更新代码以使用 $db->insert($table, $data) - 或将 Zend_Db_Table 对象与 $table 一起使用->插入($data)

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

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

如果您只需要使用 MySQL,请将 DSN 字符串更改为您的 MySQL 连接。类似于:

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

有关详细信息,请参阅 PDO 文档

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

If the db object in the registry is one of Zend_Db then:

$dbh->quote() becomes $db->quote() (the easy one)

$dbh->query() statements that manipulate data could translate to $dbh->query() (again, easy). However, it would be better to update the code to use $db->insert($table, $data) - or use Zend_Db_Table objects with $table->insert($data).

$dbh->query() statments that return data can become $db->fetchAll($sql) statements, but you need to update the code to expect Zend_Db Row and Rowset objects.

I'd suggest reading the Zend_Db
documentation to understand the what functions map to the different PDO functions.

If you just need this to work with MySQL change the DSN string to your MySQL connection. Something like:

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

See the PDO documentation for details.

If the db object in the registry a PDO instance just grab that object and use it, instead of creating a new PDO object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文