Page 1 of 1
mysql_fetch_assoc extension
Posted: Sat Jan 05, 2013 11:44 pm
by Porkypie
Hey,
I'm getting the following error:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\................www\2\core\inc\user.inc.php on line 9
my code looks like this:
Function fetch_users(){
$result = mysql_query('select `user_id` as `id`, `user_username` as `username` from `users`');
$users = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$users[] = $row;
}
return $users;
}
I get the same error message in the following function:
function fetch_user_info($uid){
$uid = (int)$uid;
$sql = "select
`user_id` as `id`,
`user_username` as `username`,
`user_firstname` as `firstname`,
`user_lastname` as `lastname`,
`user_email` as `email`,
`user_tel` as `tel`,
`user_about` as `about`,
`user_gender` as `gender`
from `users`
where `user_id` = {$uid}";
$result = mysql_query($sql);
$info = mysql_fetch_assoc($result);
$info['avatar'] = (file_exists("{$GLOBALS['path']}/user_avatars/{$info['id']}.jpg")) ? "core/user_avatars/{$info['id']}.jpg" : "core/user_avatars/default.jpg";
return $info;
}
I found the following about mysql_fetch_assoc extension:
http://php.net/manual/en/function.mysql-fetch-assoc.php
Is this also the reason for my error or should I be looking somewhere else?
And if this does causes the error how can I get the codes to work?
thanks in advance!
Porkypie
Re: mysql_fetch_assoc extension
Posted: Sat Jan 05, 2013 11:56 pm
by Helx
mysql_fetch_array()?
Re: mysql_fetch_assoc extension
Posted: Sun Jan 06, 2013 12:23 am
by Porkypie
I've tried that too but the error just changes to:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\...\www\2\core\inc\user.inc.php on line 9
Re: mysql_fetch_assoc extension
Posted: Sun Jan 06, 2013 1:16 am
by ExtremeGaming
It means your query is failing. Change it to this.
Function fetch_users(){
$result = mysql_query('select `user_id` as `id`, `user_username` as `username` from `users`');
return mysql_error();
$users = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$users[] = $row;
}
return $users;
}
I also don't get your $user[] = $row;...hopefully you know that won't work. And change the other to:
function fetch_user_info($uid){
$uid = (int)$uid;
$sql = "select
`user_id` as `id`,
`user_username` as `username`,
`user_firstname` as `firstname`,
`user_lastname` as `lastname`,
`user_email` as `email`,
`user_tel` as `tel`,
`user_about` as `about`,
`user_gender` as `gender`
from `users`
where `user_id` = {$uid}";
return mysql_error();
$result = mysql_query($sql);
$info = mysql_fetch_assoc($result);
$info['avatar'] = (file_exists("{$GLOBALS['path']}/user_avatars/{$info['id']}.jpg")) ? "core/user_avatars/{$info['id']}.jpg" : "core/user_avatars/default.jpg";
return $info;
}
Re: mysql_fetch_assoc extension
Posted: Sun Jan 06, 2013 3:56 pm
by Porkypie
I got the $user[] = $row; part out of the tutorial. And I'm a beginner with PHP so I wouldn't know why it wouldn't work.
At the moment it don't get an error message for it but I guess it won't stay like that if you tell it won't work.
But after changing the code like you told me to, everything seems to work fine.
I just have 2 other problems in other parts of the tutorial codes now:
1st.
Warning: Invalid argument supplied for foreach() in C:\.........\www\2\user_list.php on line 16
<?php
include('core/init.inc.php');
?>
<html>
<head>
<title>Geregistreerde Gebruikers</title>
<link rel="stylesheet" type="text/css" href="Style.css">
</head>
<body>
<div class="container">
<?php
foreach (fetch_users() as $user){
?>
<p>
<a href="profile.php?uid=<?php echo $user['id']; ?>"><?php echo $user['username']; ?></a>
</p>
<?php
}
?>
</div>
</body>
</html>
2nd.
Parse error: parse error, unexpected ')', expecting ',' or ';' in C:\.........\www\2\profile.php on line 28
<?php
include('core/init.inc.php');
$user_info = fetch_user_info($_GET['uid']);
?>
<html>
<head>
<title><?php echo $user_info['username']; ?>'s Profiel</title>
<link rel="stylesheet" type="text/css" href="Style.css">
</head>
<body>
<div class="container">
<?php
if ($user_info === false){
echo 'Deze gebruiker bestaat niet.';
}else{
?>
<h1><?php echo $user_info['firstname']; ?> <?php echo $user_info['lastname']; ?></h1>
<img src="<?php echo $user_info['avatar']; ?>" alt="Avataar" />
<p>Gebruikersnaam: <?php echo $user_info['username']; ?></p>
<p>Geslacht: <?php echo $user_info['gender'] == 1) ? 'Man' : 'Vrouw'; ?></p>
<p>Email: <?php echo $user_info['email']; ?></p>
<p>Telefoon: <?php echo $user_info['tel']; ?></p>
<p><?php echo $user_info['about']; ?></p>
<?php
}
?>
</div>
</body>
</html>
I've tried to change the ')' but then the '?' seems to be wrong.
Re: mysql_fetch_assoc extension
Posted: Mon Jan 07, 2013 12:13 am
by ExtremeGaming
For the first error, you need to post your fetch_users() function. Make sure it is returning an array.
For the second error, your turnery operator needs an opening parenthesis (
<p>Geslacht: <?php echo ($user_info['gender'] == 1) ? 'Man' : 'Vrouw'; ?></p>
Re: mysql_fetch_assoc extension
Posted: Mon Jan 07, 2013 12:35 am
by Porkypie
Here's the function:
Function fetch_users(){
$result = mysql_query('select `user_id` as `id`, `user_username` as `username` from `users`');
return mysql_error();
$users = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$users[] = $row;
}
return $users;
}
I guess it has to do with the $users[] = $row; you didn't understand
Re: mysql_fetch_assoc extension
Posted: Mon Jan 07, 2013 2:36 am
by ExtremeGaming
Ok. That's to be expected. My bad
But in user_list.php: somewhere in the top of the file, doesn't matter where as long as it is before the foreach and after the include(); and within the php tags but put:
echo fetch_users();
Note that you will still get the foreach error, but another message should be displayed. Post it here
Re: mysql_fetch_assoc extension
Posted: Mon Jan 07, 2013 7:40 am
by Porkypie
I get the following error:
Unknown column 'user_username' in 'field list'
my column was called 'user_name' so i've changed all of the "user_username" to "user_name".
The Unknown column... error is gone now but the old error remains.
Re: mysql_fetch_assoc extension
Posted: Mon Jan 07, 2013 7:58 am
by Helx
Why are you using:
`real_ column_name` as `another_name` ?
It works just as fine without all that I'm sure.
Just rename it to what the actual column name is
Quite a lot easier.
Re: mysql_fetch_assoc extension
Posted: Thu Jan 10, 2013 7:46 pm
by jacek
Porkypie wrote:The Unknown column... error is gone now but the old error remains.
Did you remove the
return mysql_error();
? Leaving that there will exit the function block at that point meaning no value ever gets returned.