mysql_fetch_assoc(): supplied argument is not a valid MySQL

Ask about a PHP problem here.
Post Reply
techm3
Posts: 12
Joined: Wed Dec 28, 2011 3:34 pm

mysql_fetch_assoc(): supplied argument is not a valid MySQL

Post by techm3 »

Hi there, Im using this code:
<?php
	function fetch_user_info($name){	
		$sql = "SELECT 
				'bname' AS 'name',
				'email' AS 'email',
				'website' AS 'website',
			FROM 'cInfo'
			WHERE 'name' = {$name}";
		$result = mysql_query($sql);
		return mysql_fetch_assoc($result);
	}
?>
but I'm getting "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/core/user.inc.php on line 15" which is the return line.

Any ideas?

Thanks! :)
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: mysql_fetch_assoc(): supplied argument is not a valid My

Post by Temor »

you need to use backticks ( ` ) instead of semiqoutes ( ' ).
$sql = "SELECT
                                `bname` AS `name`,
                                `email` AS `email`,
                                `website` AS `website`,
                        FROM `cInfo`
                        WHERE `name` = {$name}";
You also don't need the AS part on email and website seeing as you change the name to the exact same thing.


Try this query and report back :)
$sql = "SELECT
                                `bname` AS `name`,
                                `email`,
                                `website`,
                        FROM `cInfo`
                        WHERE `name` = {$name}";
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: mysql_fetch_assoc(): supplied argument is not a valid My

Post by jacek »

If you add an
echo mysql_error();
after the failing query it will tell you what's wrong.
Image
techm3
Posts: 12
Joined: Wed Dec 28, 2011 3:34 pm

Re: mysql_fetch_assoc(): supplied argument is not a valid My

Post by techm3 »

I tried using the following code:
<?php
	//fetches profile information for the given user
	function fetch_user_info($cname){	
		$sql = "SELECT 
				`cname`,
				`address`,
				`tel`,
				`email`,
				`wsite`
			FROM `cInfo`
			WHERE `cname` = {$cname}";

		$result = mysql_query($sql);

		return mysql_fetch_assoc($result);
	}
?>
but still getting an error that says:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 8"
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: mysql_fetch_assoc(): supplied argument is not a valid My

Post by EcazS »

You should add semiquotes around {$cname}
// stuff...
WHERE `cname` = '{$cname}'
Also make sure you're actually passing in something into the cname variable when using the function,
fetch_user_info("Name");
Also, did you try with
echo mysql_error();
after the query?
techm3
Posts: 12
Joined: Wed Dec 28, 2011 3:34 pm

Re: mysql_fetch_assoc(): supplied argument is not a valid My

Post by techm3 »

Thanks for all your replies.

@EcazS '{$name}' solve it :D
Thank You!
Post Reply