MyBB Community Forums

Full Version: Getting all results from a "LIKE" mysql query.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Im creating a small search script to find all the people based off the person's name.

Im using the following code to get the results.
$result = mysql_query("SELECT * FROM customers WHERE name LIKE '%$term%'");


However, lets say I type in the last name of Green. There are two people in the database. James Green and Derek Green. The script finds the first row that contains the word "Green" and displays it. However, I want it to find every result that contains the word "Green", so it would display both James and Derek.

Any help would be nice.
It sounds like you're not looking through your result set correctly because that query is correct. On another note, you really should be using the MySQLi object, or their procedural counterparts Wink
(2012-04-12, 12:38 AM)Charlie Hadden Wrote: [ -> ]It sounds like you're not looking through your result set correctly because that query is correct. On another note, you really should be using the MySQLi object, or their procedural counterparts Wink

Can you tell me how I can fix the query so that it is set correctly? I know about MySQLi however, at the time how I use the mysql commands have worked just fine.
The query is fine, I'd need to see the code to which actually loops through the results to give you a further advice. And yes, the MySQL extension does work, but performance wise, MySQLi is a lot better.
(2012-04-12, 12:42 AM)Charlie Hadden Wrote: [ -> ]The query is fine, I'd need to see the code to which actually loops through the results to give you a further advice. And yes, the MySQL extension does work, but performance wise, MySQLi is a lot better.

There isn't a result loop. It just posts into a table after posting the $result to a $row like this "$row = mysql_fetch_array($result);" and posting it to the table like . $row['name'] . and so on and so forth.
So what code are you using to set $row? Just mysql_fetch_row()?
(2012-04-12, 12:47 AM)Charlie Hadden Wrote: [ -> ]So what code are you using to set $row? Just mysql_fetch_row()?

Yes: $row = mysql_fetch_array($result);
There's your issue then.

if(mysql_num_rows($result) > 0)
{
	while($row = mysql_fetch_assoc($result))
	{
		echo $row['name'];
	}
}
It works now! Thanks!
I know it does... but no problem.