来自MySQL SELECT的PHP echo结果

时间:2014-01-14 10:07:15

标签: php mysql

我有这个PHP(测试):

<?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的手动SELECT显示:

mysql> SELECT passport FROM users WHERE mail = 'WhoMe@domain.com';
+----------------------+
| passport             |
+----------------------+
| 8KQSWCZPJAUV1M9D4TFG |
+----------------------+
1 row in set (0.00 sec)

我试图在PHP中回显$result,它应该显示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"];

}
?>

这也是空的。

4 个答案:

答案 0 :(得分:1)

试试:

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

答案 1 :(得分: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()

答案 2 :(得分: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']);    
    }
}

答案 3 :(得分:0)

你需要获取循环并修复查询(删除;),如下所示:

<?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"];
    }

}
?>