MySql没有输出

时间:2014-02-27 12:58:05

标签: php mysql sql database

好的,我有这个代码

<?php
$email = htmlentities($_SESSION['user']['dato'], ENT_QUOTES, 'UTF-8');
$username = "mcnsaoia_onsafe";
$password = "XXX";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
 or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//select a database to work with
$selected = mysql_select_db("mcnsaoia_onsafe",$dbhandle) 
  or die("Could not select examples");

//execute the SQL query and return records
$result = mysql_query("SELECT * FROM users WHERE id= '$email'") or die(mysql_error()); 

//fetch tha data from the database 
while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['status'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
//close the connection
mysql_close($dbhandle);
?>

但是当我运行它时,它只是说连接到MySQL并且它不输出任何东西?  我不知道为什么会这样做?!

2 个答案:

答案 0 :(得分:2)

while($row = mysqli_fetch_array($result))

使用mysql_fetch_array()

进行更改
while($row = mysql_fetch_array($result))

答案 1 :(得分:1)

使用mysql_num_rows()函数检查查询是否返回行。

if(mysql_num_rows($result)>1)
{
  while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['status'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "</tr>";
  }
}
else
{
echo "No rows found";
}

注意:使用mysqli_ *函数。 mysql_ *函数已被弃用。