[英]why mysql_query not working?
Can someone help me figure out why mysql_query not working? 有人可以帮我弄清楚为什么mysql_query无法正常工作吗?
<?php
$dbhost = "localhost";
$dbusername = "USERNAME";
$dbpassword = "PASSWORD";
$dbname = "DB_NAME";
$connection = mysql_connect($dbhost, $dbusername,$dbpassword) or die('Could not connect');
$db_selected = mysql_select_db($dbname, $connection) or die(mysql_error());
echo " connect... \n";
$result = mysql_query("select * from test",$db_selected);
while($row = mysql_fetch_array($result))
{
echo $i++;
echo "Id : " . $row['Id'] . " Name : " .$row['name'] . " Address : " . $row['address'];
echo "<br>";
}
?>
result: connect... 结果:连接...
You must use $connection
instead of $db_connected
in your mysql_query
. 您必须在mysql_query
使用$connection
而不是$db_connected
。 See the man page . 请参见手册页 。
And the fact you did not see the error implies that you have error reporting not configured correctly; 您没有看到错误的事实意味着您的错误报告配置不正确; check your php.ini
and this answer . 检查您的php.ini
和此答案 。
It wouldn't have worked anyway since you were fetching a numeric array instead of an associative array (use mysql_fetch_assoc()
for that) which is needed by $rec['Id']
syntax (as opposed to $rec[3]
). 由于您正在获取$rec['Id']
语法(而不是$rec[3]
)所需的数字数组而不是关联数组 (为此,请使用mysql_fetch_assoc()
,因此无论如何它都不会起作用。
However, if you are just now beginning with PHP and MySQL, do not use mysql_* functions . 但是,如果您刚开始使用PHP和MySQL, 请不要使用mysql_ *函数 。 They are deprecated and will be soon removed, and you'll have wasted time learning something no longer in use. 它们已过时 ,将很快被删除,您将浪费时间学习不再使用的内容。 Use PDO instead. 请改用PDO 。 A teensy bit more difficult to master, but lightyears better . 较难掌握,但更好 。
If you want to reference the columns by name you must use mysql_fetch_assoc($result)
not mysql_fetch_array($result))
如果要按名称引用列,则必须使用mysql_fetch_assoc($result)
而不是mysql_fetch_array($result))
But don't use either. 但不要使用任何一个。 use mysqli_*
functions instead or better still pdo
. 请改用mysqli_*
函数,或者最好还是使用pdo
。
echo $i++
doesn't produce anything as it is undefined echo $i++
不产生任何东西,因为它是未定义的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.