【发布时间】:2013-01-02 00:31:01
【问题描述】:
数据库连接是正确的,但我收到一个错误提示
Warning:
mysql_fetch_array(): supplied argument is not a valid MySQL result on line 31
<?php
$hostname = "localhost";
$username = "user";
$password = "pass";
$usertable = "products";
$dbName = "test_prods";
$s = $_GET["s"];
$name = $row["product_name"];
MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect to database");
@mysql_select_db( "$dbName") or die( "Unable to select database");
//error message (not found message)
$XX = "No Products Found";
$query = mysql_query("SELECT * FROM $usertable WHERE $s LIKE '$name'");
while ($row = mysql_fetch_array($query))
{
$variable1=$row["row_name1"];
$variable2=$row["row_name2"];
$variable3=$row["row_name3"];
print ("this is for $variable1, and this print the $variable2 end so on...");
}
//below this is the function for no record!!
if (!$variable1)
{
print ("$XX");
}
//end
?>
【问题讨论】:
-
您可以在调用
mysql_query后查看mysql_error以查看运行查询是否有任何问题。 -
$name = $row["product_name"];--- 请解释这一行 -
请注意旧的 mysql_% 功能将被停用。这意味着当 PHP 升级时,您的代码可能会停止工作。对于新项目,您应该切换到 mysqli 扩展为recommended in the PHP docs。
-
@Arnold Daniels:建议在 mysqli/pdo 之间进行选择,而不是直接“切换到 mysqli”
-
@zerkms,你是对的! PDO 的 API 与旧的 mysql 有很大的不同。所以如果有人已经熟悉了,我通常建议切换到 mysqli 而不是 PDO。