How to reslove mysql_fetch_assoc(): problems!
        Posted  
        
            by sky
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by sky
        
        
        
        Published on 2010-04-19T08:29:45Z
        Indexed on 
            2010/04/19
            8:33 UTC
        
        
        Read the original article
        Hit count: 194
        
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!
<?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> 
© Stack Overflow or respective owner