在PHP中重写从弃用的mysql_connect到PDO的查询

时间:2016-05-25 05:19:33

标签: php mysql pdo

我正在重写下面弃用的代码mysql_connect,以便在PDO中工作但无法使其正常工作。没有显示错误。我尽我所能。有人可以帮助我

不推荐使用mysql_connect

<?php include('config.php'); ?>
<?php
    if(isset($_POST['page'])):
    $paged=$_POST['page'];
    $sql="SELECT * FROM `users` where qualify='Po' ORDER BY `uid` desc ";
    if($paged>0){
           $page_limit=$resultsPerPage*($paged-1);
           $pagination_sql=" LIMIT  $page_limit, $resultsPerPage";
           }
    else{
    $pagination_sql=" LIMIT 0 , $resultsPerPage";
    }

    $result=mysql_query($sql.$pagination_sql);

    $num_rows = mysql_num_rows($result);
    if($num_rows>0){
    while($data=mysql_fetch_array($result)){
    $userid=$data['uid'];
    $fullname=$data['fullname'];
    echo "<li><h3>$userid</h3><p>$fullname<p></li>";
    }
    }
    if($num_rows == $resultsPerPage){?>
    <li class="loadbutton"><button class="loadmore" data-page="<?php echo  $paged+1 ;?>">Load More</button></li>
 <?php 
  }else{
    echo "<li class='loadbutton'><h3>No More Data</h3></li>";
 }
  endif;
   ?>

转换为PDO

<?php


$resultsPerPage=1;

$db = new PDO (
    'mysql:host=localhost;dbname=chat;charset=utf8', 
    'root', // username

    '' // password
);


?>

<?php include('pdo.php'); ?>
<?php


    if(isset($_POST['page'])):
    $paged=$_POST['page'];


$prefix = "";
//Loadmore configuarion
$resultsPerPage=1;

$sql = $db->prepare("SELECT * FROM users where qualify=:qualify  ORDER BY uid desc");
        $sql->execute(array(':qualify'=>'po'));
if($paged>0){
           $page_limit=$resultsPerPage*($paged-1);
           $pagination_sql=" LIMIT  $page_limit, $resultsPerPage";
           }
    else{
    $pagination_sql=" LIMIT 0 , $resultsPerPage";
    }
$result = $db->prepare($sql.$pagination_sql);

$num_rows = $result->rowCount();
if($num_rows>0){

 while ($row = $result->fetch()) {

$userid=htmlentities($row['uid'], ENT_QUOTES, "UTF-8");


$fullname=htmlentities($row['fullname'], ENT_QUOTES, "UTF-8");

    echo "<li><h3>$userid</h3><p>$fullname<p></li>";
    }
    }
    if($num_rows == $resultsPerPage){?>
    <li class="loadbutton"><button class="loadmore" data-page="<?php echo  $paged+1 ;?>">Load More</button></li>
 <?php 
  }else{
    echo "<li class='loadbutton'><h3>No More Data</h3></li>";
 }
  endif;
   ?>

非常感谢

1 个答案:

答案 0 :(得分:0)

这里的解决方案是查看http://php.net/manual/en/pdo.setattribute.php并在PDO构造之后输入以下代码:

$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

这不会修复您的代码,但它应该显示错误发生的原因,您可以从那里完全调试。