2016-03-17 117 views
-1

我想在我的頁面中回顯sql數據庫記錄,我正在使用此代碼。當我運行它時不顯示數據庫的結果。我在數據庫中有符合條件的記錄。我是新的PHP和SQL,所以請告訴我哪裏有錯誤。無法顯示數據庫記錄

session_start(); 
if (!isset($_SESSION['name'])) { 
    header('Location:vhod.php'); 
    exit; 
} 

$pageTitle = 'СЪОБЩЕНИЯ'; 
include 'includes/header.html'; 

$email = $_SESSION['email']; 
$name = $_SESSION['name']; 

include 'php/db_connect.php'; 

$msgs = ''; 
$query = 'SELECT `timestamp`, `to`, `sender`, `subject`, `msg` FROM msg WHERE `to`="$name"'; 
$result = mysqli_query($conn, $query); 

if (mysqli_num_rows($result) > 0) { 
    while($row = mysqli_fetch_assoc($result)) { 
     $msgs = "ДАТА: " . $row["timestamp"] . " >> От: " . $row["sender"] . " >> Тема: " . $row["subj"] . " >> Съобщение: " . $row["msg"] . "<br>"; 
    } 
} else { 
    $msgs = "Нямате съобщения :("; 
} 
+0

這些諮詢以下鏈接http://php.net/manual/en/mysqli.error.php和http://php.net/manual/en/function.error-reporting.php 和應用這對你的代碼。 –

+0

btw,你在查詢中選擇了'subject'列,但是在'$ row [「subj」]'中使用'subj',MySQL會拋出一些關於它的東西,就像在「不存在的列」中一樣。 –

回答

0

嘗試像這樣修改代碼的下半部分。

$msgs = ''; 
    $query = "SELECT * FROM msg WHERE to=$name"; 
    $result = mysqli_query($conn, $query); 

    if (mysqli_num_rows($result) > 0) { 
     while($row = mysqli_fetch_assoc($result)) { 
      $msgs = "ДАТА: " . $row['timestamp'] . " >> От: " . $row['sender'] . " >> Тема: " . $row['subj'] . " >> Съобщение: " . $row['msg'] . "<br>"; 
     } 
    } else { 
     $msgs = "Нямате съобщения :("; 
    } 
+0

mysqli_num_rows()期望參數1是mysqli_result,布爾給定 –

+1

你確定msg表存在嗎?或者mysql連接正常工作?,你以前的錯誤是什麼? –

+0

我的味精表存在。我發現它總是在else {}語句中顯示該消息(正在討論這個問題:「Няматесъобщения:(」) –