我尝试使用PHP从我的数据库输出一个表,但显示" mysql_fetch_object():提供的参数不是有效的MySQL结果资源"。
// Connect to MySQL server
$conn = mysql_connect('localhost','root','') or die(mysql_error());
// Select database
mysql_select_db('bakery') or die(mysql_error());
$sql = "Select * From customer";
$result = mysql_query($sql,$conn);
echo "<table border=1>";
echo "<tr><td>custid</td>
<td>custname</td>
<td>ccn</td>
<td>phoneno</td>
<td>address</td>
<td>city</td>
<td>zip</td></tr>";
while($row = mysql_fetch_object($sql))
echo "<tr><td>$row->custid</td>
<td>$row->custname</td>
<td>$row->ccn</td>
<td>$row->phoneno</td>
<td>$row->address</td>
<td>$row->city</td>
<td>$row->zip</td></tr>";
echo "</table>";
答案 0 :(得分:1)
而不是
while($row = mysql_fetch_object($sql))
应该是
while($row = mysql_fetch_object($result)) //<--- Pass the $result
由于错误状态...... mysql_fetch_object
需要资源,但您已经通过了string
(这背后是主要错误)
此mysql_*
)扩展程序自PHP 5.5.0
起已弃用,将来会被删除。相反,应使用MySQLi
或PDO_MySQL
扩展名。切换到PreparedStatements
甚至可以更好地抵御SQL注入攻击!