Page 1 of 1

mysql_fetch_assoc(): supplied argument is not a valid MySQL

Posted: Wed Dec 28, 2011 3:37 pm
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! :)

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

Posted: Wed Dec 28, 2011 3:57 pm
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}";

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

Posted: Wed Dec 28, 2011 11:43 pm
by jacek
If you add an
echo mysql_error();
after the failing query it will tell you what's wrong.

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

Posted: Thu Dec 29, 2011 1:03 pm
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"

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

Posted: Thu Dec 29, 2011 1:25 pm
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?

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

Posted: Thu Dec 29, 2011 4:44 pm
by techm3
Thanks for all your replies.

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