2014-01-14 70 views
0

我有這個PHP(測試):PHP回聲結果從MySQL SELECT

<?php 
if (isset($_POST['submit'])) { 

    $link = mysqli_connect("localhost","php","","A"); 

    $query = "SELECT passport FROM users WHERE mail = '$_POST[mail]';"; 

    $result = $link->query($query); 

} 
?> 

手動選擇使用相同$query顯示:

mysql> SELECT passport FROM users WHERE mail = '[email protected]'; 
+----------------------+ 
| passport    | 
+----------------------+ 
| 8KQSWCZPJAUV1M9D4TFG | 
+----------------------+ 
1 row in set (0.00 sec) 

我想呼應$result在PHP這應該顯示8KQSWCZPJAUV1M9D4TFG,但它是空的。我在這裏做錯了什麼?

更新:

<?php 
if (isset($_POST['submit'])) { 

    $link = mysqli_connect("localhost","php","","A"); 

    $query = "SELECT passport FROM users WHERE mail = '$_POST[mail]';"; 

    $result = mysqli_query($query); 

    $row = mysqli_fetch_assoc($result); 

    echo $row["passport"]; 

} 
?> 

這也仍然是零。

+0

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

+0

你可以刪除;從查詢字符串的末尾開始(如果需要),並在下一行回顯$ query,以確保準確生成了哪個查詢。 –

回答

1

只是嘗試:

$result = mysqli_query($query); 
$row = mysqli_fetch_assoc($result); 
+0

並輸出它'echo $ row [「passport」];' – akirk

+0

謝謝!我放棄了這個問題並用我正在使用的內容更新了這個問題,但它仍然沒有給出SELECT手冊的結果? – user2656114

1
$link = new mysqli("localhost", "php", "", "A"); 

$query = "SELECT passport FROM users WHERE mail = '$_POST[mail]';"; 
$result = $link->query($query); 

while($row = $result->fetch_array()) 
{ 
    echo $row['passport']; 
} 

您需要使用基於MySQLi對象才能使用$link->query()

0

檢查這一項。

它應該爲你工作。

$mysqli = new mysqli('localhost', 'root', 'pass123', 'test'); 
$query = "SELECT passport FROM users WHERE mail = '$_POST[mail]'"; 

$result = $mysqli->query($query); 

// PRINT 
if($result->num_rows > 0) { 
    while($row = $result->fetch_assoc()) { 
     echo stripslashes($row['passport']);  
    } 
} 
0

你需要在循環中fecth和修復查詢(刪除)這樣的:

<?php 
if (isset($_POST['submit'])) { 

    $link = mysqli_connect("localhost","php","","A"); 

    $query = "SELECT passport FROM users WHERE mail = '$_POST[mail]'"; 

    $result = mysqli_query($query); 

    while($row = mysqli_fetch_assoc($result)){ 

    echo $row["passport"]; 
    } 

} 
?> 
+0

@ user2656114:你試過這個嗎? –