2017-09-20 81 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

非常歡迎。我很高興它有幫助 –