可能重复:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
当我使用下面的代码时,我收到此错误: 警告:mysql_fetch_assoc():提供的参数不是有效的MySQL结果资源
返回数据时,任何人都可以修复它吗?谢谢!
<?php
$mysql_server_name="localhost";
$mysql_username="";
$mysql_password="";
$mysql_database="";
$conn=mysql_connect($mysql_server_name, $mysql_username,
$mysql_password);
?>
<?php
$result = mysql_query("SELECT * FROM users");
$arrays = array();
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $key => $val) {
if (!array_contains_key($key)) {
$arrays[$key] = array();
}
$arrays[$key][] = $val;
}
}
?>
<script type="text/javascript">
<?php
foreach ($arrays as $key => $val) {
print 'var ' . $key . ' = ' . json_encode($val) . ";\r\n";
}
?>
</script>
答案 0 :(得分:1)
这不是mysql_fetch_assoc问题而是查询问题
做到这一点
$sql="SELECT * FROM users";
$result = mysql_query($sql) or trigger_error(mysql_error().$sql);
并查看实际错误
答案 1 :(得分:0)
使用mysql_error检查MySQL的错误消息:
<?php
$result = mysql_query('SELECT * FROM users');
$error = mysql_error();
if ($error != '')
die($error);
?>
答案 2 :(得分:0)
通常我会像这样连接Mysql:
$con = mysql_connect("host","user","passwd");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}