2017-09-14 47 views
0

我想獲取此行並將其保存到$notescheck中,但是當我嘗試執行此操作時,$notescheck爲空,當我要回顯並且沒有錯誤時。使用未準備好的聲明,它工作正常。

準備好的語句獲取行不會返回任何內容

代碼:

if($user_ok == true) { 
    $sql = "SELECT notescheck FROM users WHERE username=? LIMIT 1"; 
    $stmt = $conn->prepare($sql); 
    $stmt->bind_param("s",$log_username); 
    $stmt->execute(); 
    $row = $stmt->fetch(); 
    $notescheck = $row[0]; 
    $stmt->close(); 
} 

對於非事先準備好的聲明它是這樣的:

if($user_ok == true) { 
    $sql = "SELECT notescheck FROM users WHERE username='$log_username' LIMIT 1"; 
    $query = mysqli_query($conn, $sql); 
    $row = mysqli_fetch_row($query); 
    $notescheck = $row[0]; 
    mysqli_close($conn); 
} 

回答

1

這不是如何獲取()和預處理語句的作品,你」不要像你認爲的那樣獲取數組。您還需要將選擇的結果綁定到變量,然後使用這些來顯示。如果有多個記錄,你會使用一個while($stmt->fetch){ echo $notescheck };

if($user_ok == true) { 
    $sql = "SELECT notescheck FROM users WHERE username=? LIMIT 1"; 
    $stmt = $conn->prepare($sql); 
    $stmt->bind_param("s",$log_username); 
    $stmt->execute(); 
    $stmt->bind_result($notescheck); 
    $stmt->fetch(); 
    $stmt->close(); 
} 
echo $notescheck; 

您應該檢查到閱讀本:

http://php.net/manual/en/mysqli-stmt.fetch.php

多個記錄匹配的用戶名= X應該是這樣的:

if($user_ok == true) { 
     $sql = "SELECT notescheck FROM users WHERE username=? LIMIT 1"; 
     $stmt = $conn->prepare($sql); 
     $stmt->bind_param("s",$log_username); 
     $stmt->execute(); 
     $stmt->bind_result($notescheck); 
     $stmt->store_result() 
     while($stmt->fetch()){ 
      echo $notescheck; 
     } 
     $stmt->close(); 
    }