PDO query()或fetch()不起作用

时间:2013-07-10 00:59:27

标签: php mysql database pdo

我有这个代码,用于在数据库中显示我网站上的评论 但评论未显示 我想也许PDO query()fetch()不起作用,但问题是我没有收到任何错误。

这是我使用的代码:

<?php
$showcom1 =  $db->query("select * from comments where pc_active='no' order by p_id asc") or die (mysql_error());
$gpid         = $_GET['p_id'];
$dcid         = $_GET['dc_id'];
$ecpid        = $_GET['ec_id'];
$hcpid        = $_GET['hc_id'];
$scpid        = $_GET['sc_id'];

echo "
<table width='100%' align='center' cellpadding='10' cellspacing='10'>
<tr>
    <td class='bodymenu' style='color: #4276a1' align='center'>
    الفئات  <text style='color: #555'>::</text>
    1 = القرآن الكريم
    <text style='color: #555'>||</text>
    2 = أدعية 
    <text style='color: #555'>||</text>
    3 = قصص 
    <text style='color: #555'>||</text>
    4 =مقالات 
    <text style='color: #555'>||</text>
    5 = فتاوى 
    <text style='color: #555'>||</text>
    6 = رمضانيات
    <text style='color: #555'>||</text>
    7 = اسلاميات
    </td>
</tr>
</table>
<table width='100%' align='center' cellpadding='0' cellspacing='0'>
<tr>
    <td class='tbl' colspan='7'>التعليقات</td>
</tr>
<tr align='center' >
    <td class='tblbb2' >الفئة</td>
    <td class='tblbb2' >اسم المعلق</td>
    <td class='tblbb2' >بريد المعلق</td>
    <td class='tblbb2' >اي بي المعلق</td>
    <td class='tblbb2' >تاريخ التعليق</td>
    <td class='tblbb2' >رابط الصفحة</td>
    <td class='tblrlb' >الخيارات</td>
</tr>";

// TABLE comments = pc_id,pc_name,pc_mail,pc_ip,pc_date,pc_text,pc_active,p_id

while ($rcom1 = $showcom1->fetch(PDO::FETCH_OBJ)) {
echo "
<tr>
    <td align='center' class='tblr' >".$rcom1->pc_cat."</td>
    <td align='center' class='tblr' >".$rcom1->pc_name."</td>
    <td align='center' class='tblr' >".$rcom1->pc_mail."</td>
    <td align='center' class='tblr' >".$rcom1->pc_ip."</td>
    <td align='center' class='tblr' >".$rcom1->pc_date."</td>
    <td align='center' class='tblrl' ><a target='_blank' href='../pages.php?p_id=".$rcom1->p_id."'>مشاهدة</a></td>
    <td align='center' class='tbll' >
    <a href='index.php?cpages=comments&dc_id=".$rcom1->pc_id."'>حذف</a> -
    <a href='index.php?cpages=comments&p_id=".$rcom1->pc_id."'>معاينة</a> -
    <a href='index.php?cpages=comments&hc_id=".$rcom1->pc_id."'>اخفاء</a>
    </td>
</tr>
";
}
echo "  <tr colspan='7'>
    <td colspan='7' class='tblb'>
    </td>
    </tr>";
?>

2 个答案:

答案 0 :(得分:1)

您希望在查询中显示评论

$showcom1 =  $db->query("select * from comments where pc_active='no' order by p_id asc") or die (mysql_error());

pc_activeno我想no意味着评论可能隐藏了,而你在数据库中设置的其他单词意味着可见......也许你没有隐藏的评论在您的数据库中,请尝试将no更改为其他字词,或将数据库中的pc_active更改为no

其他事情我注意到你使用 or die (mysql_error()); 并且不能使用pdo你可能想要更改它以使用捕获的函数:D

就像这样:

将您的查询置于

try {

}

然后在尝试之后放置抓住的内容:

catch(PDOException $e){
echo 'ERROR: '.$e->getMessage();
} 

您应该在此处了解有关pdo错误处理的更多信息:PHP Oficcial Site For Error Handling

答案 1 :(得分:0)

如果您使用的是PDO,那么为什么在此查询结束时使用or die(mysql_error())处理错误?

$showcom1 =  $db->query("select * from comments
                       where pc_active='no' order by p_id asc") or die
                       (mysql_error()); 

无论如何,我不知道你的目标是什么,但错误处理是使用像这样的try / catch块完成的:

try{
$showcom1 =  $db->query("select * from comments
                           where pc_active='no' order by p_id asc"); 
}catch(PDOException $e){
echo 'ERROR: '.$e->getMessage();
}

上述方法与您的查询一起将起作用/给您一个人类可读的错误