简体   繁体   中英

PDO query() or fetch() not working

I have this code that I made for showing comments in my website from the database but the comment is not shown.
I think maybe PDO query() or fetch() is not working but the problem is that I don't get any error.

This is the code that i use :

<?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>";
?>

you want to show the comments but here in your query

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

you have pc_active as no i guess no means the comment is hidden maybe and the other words that you set in your database means visible so .. maybe you dont have hidden comments in your database so try changing no to the other word or change pc_active in your database to be no

other thing i noticed that you use or die (mysql_error()); and that wont work with pdo you may want to change it to use the caught function :D

it works like this :

placing your query in a

try {

}

then placing the caught after the try like this :

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

you should learn more about pdo error handling on here : PHP Oficcial Site For Error Handling

If you are using PDO then why are you handling error using or die(mysql_error()) at the end of this query???

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

Anyway, I don't know your aim is, but error handling is done using try/catch blocks like this:

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

The above method, in-conjunction with your query will work/give you a human-readable error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM