为什么我不能在PHP中回应MySQL的结果?

时间:2014-09-05 00:32:16

标签: php arrays mysqli

我尝试显示当前用户名在数据库中的每个结果, 但它不会工作.. 我尝试过任何事情,你是我最后的希望。 提前谢谢!

我无法从下面的代码中得到任何答案:

<?php
session_start();
include_once('../inc/db/config.php');
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$query = "SELECT serverip, servername FROM servers WHERE username=".$_SESSION['user_name']."";

 $results = mysqli_query($mysqli, $query);


 if(mysqli_num_rows($results) >= 1)
{

while($row = mysqli_fetch_array($results))
{
  echo "<hr/>"; 
  echo "<a href='../s_info.php?server='" . $row['serverip'] . "><h2>" . $row['servername'] . "</h2>";
}
}
?>

1 个答案:

答案 0 :(得分:2)

这条线对我来说很可疑:

$query = "SELECT serverip, servername FROM servers WHERE username=".$_SESSION['user_name']."";

尝试:

$query = "SELECT serverip, servername FROM servers WHERE username='{$_SESSION['user_name']}'";

你应该能够将该字符串作为测试回显出来并在mysql命令行/ phpMyAdmin中单独运行,或者任何允许您测试sql语句的工具。确保它返回结果。