Jump to content

Query not working?


Twango
 Share

Recommended Posts

Hi, the code$result = mysql_query("SELECT usr, xp, hp, level, faction FROM aad WHERE usr LIKE %$rcu%");$row = mysql_fetch_array($result);returnsWarning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/appattac/public_html/aadimensions/rsq.php on line 15Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/appattac/public_html/aadimensions/rsq.php on line 27Help!

Link to comment
Share on other sites

obviously something is wrong with your query. what have you done so far to debug it? have you checked the field and table names to make sure they are correct? Have you connected to the database correctly? have you tried adding error handling?

$result = mysql_query("SELECT usr, xp, hp, level, faction FROM aad WHERE usr LIKE %$rcu%") or die (mysql_error());

edit: and as mentioned below, are you sure you are implementing 'LIKE' correctly?

Edited by thescientist
Link to comment
Share on other sites

Please see the PHP manual for mysql_query().http://php.net/manual/en/function.mysql-query.phpThe mysql_query is not returning a MySQL result resource, and that's why you get the first error.According to the PHP manual, possible return values, in addition to a result resource, are true or false:"For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error . . .. . . mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query."If you do indeed have permission to the table, your problem is most likely with the LIKE. I believe the value of the LIKE clause must be quoted.http://dev.mysql.com/doc/refman/5.0/en/str...-functions.html

Link to comment
Share on other sites

I noticed you started multiple threads.For one, you need to single quote %$rcu%. Secondly, from an alternate thread, it appears you were only getting one row, but actually, the result resource most likely had what you were looking for but you misunderstood how to extract all the rows from it. mysql_fetch_array() only returns one row at a time. You need to run a loop to extract all the rows. When all the rows have been fetched from the resource, it will return false, terminating the loop. My hunch is that you were doing everything else right. Take a look at my test code to see how I use a loop to do this:

<?php$db_host = 'localhost';$db_name = 'test';$db_user = 'test';$db_pass = 'aJGsZE393xjDzCLG';// establish database connection$link = mysql_connect($db_host, $db_user, $db_pass) or die('Could not connect: ' . mysql_error());mysql_select_db($db_name) or die('Unable to connect to database: ' . mysql_error());// query the database$rcu = 'King';$result = mysql_query("SELECT id, usr FROM aad WHERE usr LIKE '%$rcu%'") or die (mysql_error());// loop through rows$rows = array();while ($row = mysql_fetch_array($result)){	$rows[] = $row;}// dump resultsvar_dump($result);var_dump($rows);// cleanupmysql_close($link);?>

The result:

resource(11, mysql result)array  0 => 	array	  0 => string '2' (length=1)	  'id' => string '2' (length=1)	  1 => string 'KingBar' (length=7)	  'usr' => string 'KingBar' (length=7)  1 => 	array	  0 => string '1' (length=1)	  'id' => string '1' (length=1)	  1 => string 'KingFoo' (length=7)	  'usr' => string 'KingFoo' (length=7)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...