Page 1 of 1

User Profiles [part 02] Fetch_Users() function error.

Posted: Sun Aug 19, 2012 12:26 am
by Djmann1013
I keep getting this error when viewing the user_list.php file:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a8913488/public_html/core/inc/user.inc.php on line 13

and it says this at the very bottom left:

Array()

here is the page live:

awsomechat.comuv.com/user_list.php.

Here is my code:
<?php
//user.inc.php file
session_start();

// SQL stuff.
mysql_connect('mysql_host','username','password');
mysql_select_db('database');

// Get users from table
function fetch_users() {
$result = mysql_query('SELECT `user_id` AS `id`, `user_name` AS `username` FROM users');
$users = array();
while(($row = mysql_fetch_assoc($result)) !== false) {
    $users[] = $row;
}
return $users;
}
?>
I would be glad if someone helped.

Re: User Profiles [part 02] Fetch_Users() function error.

Posted: Sun Aug 19, 2012 12:35 am
by drama22
Fix this line
while(($row = mysql_fetch_assoc($result)) !== false) {

Re: User Profiles [part 02] Fetch_Users() function error.

Posted: Sun Aug 19, 2012 12:39 am
by Djmann1013
Still getting this error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a8913488/public_html/core/inc/user.inc.php on line 13

Re: User Profiles [part 02] Fetch_Users() function error.

Posted: Sun Aug 19, 2012 12:51 am
by drama22
Djmann1013 wrote:How do I fix it?
while($row = mysql_fetch_assoc($result))
also you may use fetch_array

Re: User Profiles [part 02] Fetch_Users() function error.

Posted: Sun Aug 19, 2012 12:59 am
by Djmann1013
I am still getting the error.

Re: User Profiles [part 02] Fetch_Users() function error.

Posted: Sun Aug 19, 2012 1:00 am
by drama22
Djmann1013 wrote:I am still getting the error.
past the code after edit..

Re: User Profiles [part 02] Fetch_Users() function error.

Posted: Sun Aug 19, 2012 1:16 am
by Temor
That error means something went wrong with your SQL query. Adding
echo mysql_error();
under the failing query will provide a more detailed description of the error.
function fetch_users() {
$result = mysql_query('SELECT `user_id` AS `id`, `user_name` AS `username` FROM users');

echo mysql_error();

$users = array();
while(($row = mysql_fetch_assoc($result)) !== false) {
    $users[] = $row;
}
return $users;
}

Re: User Profiles [part 02] Fetch_Users() function error.

Posted: Sun Aug 19, 2012 2:07 am
by Djmann1013
It works. Thanks.