WordPress检查查询中的空数据

时间:2017-09-20 05:46:05

标签: php mysql wordpress

我使用foreach loop显示来自wp_share表的一些数据。我正在尝试如果没有数据,那么它将显示一个msg,如“找不到数据”,如果找到,那么它将进入循环。

以下代码我写了,但它总是得到空值,尽管实际上有数据。

<?php 

   $me=$current_user->user_login;
   $retrieve_data = $wpdb->get_results( "SELECT * FROM wp_share where Status='onsell' and user!='$me'" );
   echo '<table border=0 class="table">
   <tr> <th colspan="5" style="color:blue;border-bottom:2px solid blue;"> Purchase share from user offer</th> </tr>

   <tr><th>OfferBy</th><th>OfferValue</th><th>Qnty</th> <th> ref</th> <th> action</th></tr>';


     if (mysql_num_rows($retrieve_data)<=0) {
     echo '<tr><td style="color:crimson;">'  ."there is no data found". '</td>';
     }

    else{
    foreach ($retrieve_data as $retrieved_data){

    echo '<tr><td>'. $retrieved_data->user.'</td>';
    echo '<td>'.$retrieved_data->offervalue.'</td>';

    echo '<td>'. $retrieved_data->user.'</td>';
    echo '<td>'. $retrieved_data->id.'</td>';
   }
?>

1 个答案:

答案 0 :(得分:1)

在您的代码中有许多错误可用。 WordPress中未使用mysql_num_rows。在WordPress中检查使用的结果中的行数$wpdb->num_rows

尝试以下代码帮助您

<?php 
global $wpdb;
$me=$current_user->user_login;
$retrieve_data = $wpdb->get_results( "SELECT * FROM wp_share where Status='onsell' and user!='".$me."'" );
echo '<table border=0 class="table">
<tr> <th colspan="5" style="color:blue;border-bottom:2px solid blue;"> Purchase share from user offer</th> </tr><tr><th>OfferBy</th><th>OfferValue</th><th>Qnty</th> <th> ref</th> <th> action</th></tr>';


if ($wpdb->num_rows<=0) {
    echo '<tr><td style="color:crimson;">'  ."there is no data found". '</td>';
}else{
    foreach ($retrieve_data as $retrieved_data){

        echo '<tr><td>'. $retrieved_data->user.'</td>';
        echo '<td>'.$retrieved_data->offervalue.'</td>';

        echo '<td>'. $retrieved_data->user.'</td>';
        echo '<td>'. $retrieved_data->id.'</td></tr>';
    }
}
?>