2016-05-04 252 views
1

我當前正試圖從數據庫的多個表中讀取數據並以表格形式顯示結果。mysqli_fetch_row()在mysqli_query()返回true時返回false

但是mysqli_query()返回truemysqli_fetch_row()返回false。 $link是已初始化的連接對象。

代碼段1:

$sql = " 
    SELECT * 
    FROM users_list, user_personal_details, user_bank_details 
    WHERE 
     users_list.email = user_personal_details.email AND 
     users.email=userbank_details.email 
    LIMIT $num_rec_per_page 
    OFFSET $start_from"; 

$rs_result = mysqli_query($link, $sql); 

if ($rs_result == false) { 
    echo "Query failed"; 
    exit(); 
} 
else echo "Query successful"; 

代碼段2:

$row = mysqli_fetch_row($rs_result); 
if ($row == false) 
    echo "Invalid"; 

while ($row = mysqli_fetch_row($rs_result)) { 
    echo "In loop"; 
    $name = $row['name']; 
    $date_added = $row['date_added']; 
    echo $name; 
    echo $date_added; 
} 

mysqli_fetch_row()已使用外循環用於調試目的。 消息查詢成功已打印。 消息打印無效。 可能是什麼原因?

+1

可能是您的查詢返回true,但可能有可能它不會返回任何結果。使用'mysqli_num_rows()'檢查您的查詢返回的行數 – Saty

+0

並且您的讀取行是兩次 – Saty

+1

'mysqli_fetch_row()'返回'array'不是'boolean'例如'$行= mysqli_fetch_row($ rs_result); if($ row == false)' – Abbasi

回答

0

可能是您的查詢返回true,但可能不會返回任何結果。請使用mysqli_num_rows()檢查您的查詢返回的行數。

而且不取行兩次

$sql = "SELECT * FROM users_list,user_personal_details,user_bank_details where users_list.email=user_personal_details.email and users.email=userbank_details.email LIMIT $num_rec_per_page OFFSET $start_from"; 
    $rs_result = mysqli_query($link, $sql); //run the query 
    if ($rs_result == false) { 
     echo "Query failed"; 
     exit(); 
    } else { 
     $data = mysqli_num_rows($rs_result); 
     if ($data > 0) { 
      while ($row = mysqli_fetch_row($rs_result)) { 
       $name = $row['name']; 
       $date_added = $row['date_added']; 
       echo $name; 
       echo $date_added; 
      } 
     } else { 
      echo "No result found"; 
     } 
    } 
1

你叫mysqli_fetch_row()兩次,也許你會得到false上第二個。