mysql_fetch_array() WARNING

Hi ALL,

I’m not sure if this is a mySql or PHP issue… I’m getting really confused about the response of my script. The script suppose to search a table in the DB according to the user’s input.


$art_group = $_POST['group'];
$category = $_POST['category'];
if ($category == "") { $category = "all"; }
$with_rec = $_POST['with_rec'];
if ($with_rec == 1) { $with_rec = 1; } else { $with_rec = 0; }
$country = $_POST['country'];

echo "<h1>Search results : </h1> (group : ".$art_group.", category : ".$category.", with rec.: ".$with_rec.", country : ".$country.")<br /><br />";
$search_result = $database->searchDatabase($art_group, $category, $with_rec, $country);
	while ($search_rows = mysql_fetch_array($search_result)) {
		echo $search_rows['full_name']."<br />";
	}

and here’s the called function :


function searchDatabase($art_group, $category, $with_rec, $country) {
	
	$q = "SELECT * FROM users_details WHERE group = '$art_group'";
	$result = mysql_query($q, $this->connection);
	return $result;
	
} // end SEARCH function

The result in the browser is :


Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\\Xampp\\htdocs\\MYSITE\\ajax\\ajaxupload.php on line 24

I’ve tried to pass all the other parameters ($category, $with_rec and $country) and the script is working with ALL of them! :injured:
That’s why I thought it might be a mySql issue… I simply CAN’T figure out what’s wrong with the script or the table…
Please HELP! THNX

Let the function tell you what the problem is:


$result = mysql_query($q, $this->connection) or die(mysql_error());

see what that says.

The warning was :


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 'group = artist ORDER BY full_name ASC' at line 1

which is even more confusing, as that’s EXACTLY what I passed to the script - i.e. I want the script to list the users that have value ‘artist’ at the column ‘group’
I’ve tried


$q = "SELECT * FROM users_details WHERE group = ".$art_group." ORDER BY full_name ASC";

and then


$q = "SELECT * FROM users_details WHERE group = '$art_group' ORDER BY full_name ASC";

even


$q = "SELECT * FROM users_details WHERE group = '".$art_group."' ORDER BY full_name ASC";

returned the same warning… :frowning:

Try echo $q before you run the query and see how it outputs the query.

GROUP is a reserved word for MySQL, if it’s not to late - change the column name.
If its too late then you will have to backtick it everytime you use it.


$q = "SELECT * FROM users_details WHERE `group` = ".$art_group." ORDER BY full_name ASC"; 

Even though if GROUP is reserved word you can use it as a field name if you put it inside the tickles or what ever (`) these are called.

Right:


SELECT * FROM test_table WHERE `group` LIKE 'TEST'

Wrong:


SELECT * FROM test_table WHERE group LIKE 'TEST'

Can’t say its good idea to use reserved words as a field names though.

Thank you guys! I think I’ve had a similar issue with a reserved word before… I guess I will remember the ‘group’ one now! Is there a list with reserved words for MySql?

Google is your friend…
MySQL :: MySQL 5.6 Reference Manual :: 8.3 Reserved Words