PHP mysql PDO - 全新 - 需要建议和解释

时间:2015-11-07 12:54:07

标签: php mysql pdo

Tutorial I'm following with all the code I'm using

好的,问题很简单:我只是一个片段回收者,但如果有人能指出我正确的方向,我可以快速学习。

我尝试了各种各样的讲座,但这些对我来说很困惑。

我在我的localhost中运行这个小cms,我需要将mysql查询转换为PDO,有人可以解释我这个代码到底在做什么,以及如何将它转换为不被弃用的PDO?

public static function getById( $id ) {
 $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
 $sql = "SELECT *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM articles WHERE id = :id";
 $st = $conn->prepare( $sql );
 $st->bindValue( ":id", $id, PDO::PARAM_INT );
 $st->execute();
 $row = $st->fetch();
 $conn = null;
 if ( $row ) return new Article( $row );}

它只是连接到数据库的一个片段,我可以直观地理解正在做什么,我有SQL和PHP语法的基本知识,但我需要更友好的帮助。

My GitHub repo for this tutorial, if someone want to help me more directly

谢谢你的建议!

以下是错误消息:

Deprecated: mysql_real_escape_string(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\cms-in-an-afternoon-php-mysql\classes\Article.php on line 105

有问题的代码是

public static function getList( $numRows=1000000, $order="publicationDate DESC" ) {
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM articles
            ORDER BY " . mysql_real_escape_string($order) . " LIMIT :numRows"; // this is line 105
    $st = $conn->prepare( $sql );
    $st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
    $st->execute();
    $list = array();
    while ( $row = $st->fetch() ) {
      $article = new Article( $row );
      $list[] = $article;
    }
    // Now get the total number of articles that matched the criteria
    $sql = "SELECT FOUND_ROWS() AS totalRows";
    $totalRows = $conn->query( $sql )->fetch();
    $conn = null;
    return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) );
  }

0 个答案:

没有答案