2017-09-20 85 views
1

我使用foreach loop显示wp_share表中的一些数据。我正在尝试的是,如果没有数据,那么它会显示msg,如“找不到数据”,如果找到,则会转到循环。WordPress查询查询中的空数据

以下代码我写了,但它总是获取空值,但实际上有数据。

<?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>'; 
    } 
?> 
+0

您好我道歉,其实我想批准您的变化,但没有发现任何选项如何做到这一点thnks。 – Mithu

+0

它已经完成了,你可以看到[修订历史记录](https://stackoverflow.com/posts/46314128/revisions)。拥有超过2,000个信誉点的社区成员会自动批准修改。 – halfer

回答

1

在你的代码中有很多错误可用。 mysql_num_rows未在WordPress中使用。在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>'; 
    } 
} 
?> 
+0

@Mithu我的回答有帮助吗? –

+0

嗨gr8其工作。其实我正在忙着我的代码。感谢您的帮助 – Mithu

+0

非常欢迎。我很高兴它有帮助 –