When i use the code below, im getting this error:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
when returning the data, anyone can fix it? Thanks!
$mysql_server_name="localhost";
$mysql_username="";
$mysql_password="";
$mysql_database="";
$conn=mysql_connect($mysql_server_name, $mysql_username,
$mysql_password);
?>
$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;
}
}
?>
foreach ($arrays as $key => $val) {
print 'var ' . $key . ' = ' . json_encode($val) . ";\r\n";
}
?>
解决方案
this is not mysql_fetch_assoc problem but query problem
make it
$sql="SELECT * FROM users";
$result = mysql_query($sql) or trigger_error(mysql_error().$sql);
and see actual error