mysql_fetch_assoc extension

Ask about a PHP problem here.
Post Reply
Porkypie
Posts: 25
Joined: Wed Jan 02, 2013 3:52 pm
Location: Netherlands

mysql_fetch_assoc extension

Post 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
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: mysql_fetch_assoc extension

Post by Helx »

mysql_fetch_array()?
Porkypie
Posts: 25
Joined: Wed Jan 02, 2013 3:52 pm
Location: Netherlands

Re: mysql_fetch_assoc extension

Post 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
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: mysql_fetch_assoc extension

Post 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;
}
<?php while(!$succeed = try()); ?>
Porkypie
Posts: 25
Joined: Wed Jan 02, 2013 3:52 pm
Location: Netherlands

Re: mysql_fetch_assoc extension

Post 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.
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: mysql_fetch_assoc extension

Post 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>
<?php while(!$succeed = try()); ?>
Porkypie
Posts: 25
Joined: Wed Jan 02, 2013 3:52 pm
Location: Netherlands

Re: mysql_fetch_assoc extension

Post 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 :P
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: mysql_fetch_assoc extension

Post 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
<?php while(!$succeed = try()); ?>
Porkypie
Posts: 25
Joined: Wed Jan 02, 2013 3:52 pm
Location: Netherlands

Re: mysql_fetch_assoc extension

Post 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.
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: mysql_fetch_assoc extension

Post 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 :P

Quite a lot easier.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: mysql_fetch_assoc extension

Post 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.
Image
Post Reply