Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with like in php

Status
Not open for further replies.

jdhilljr

Programmer
Sep 1, 2003
296
US
I seem to have an error but can't find it. I keep getting a mysql error.
Here is the live site:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/d/o/l/dollgenie/html/00-fgw/index.php on line 69

I used an echo to output the mysql and it works fine when I copy and paste into phpmyadmin query this is the output of the echo
Code:
SELECT * from images where description LIKE '%mom%' or name LIKE '%mom%' order by name

I appreciate any help with this. It works fine with any other action with the dropdowns using different GET values

Code:
foreach( $_GET as $key => $value){
if($key=='query') {
$value1='%'.$value.'%';
echo "SELECT * from images where description LIKE '$value1' or name LIKE '$value1' order by name";
$mysql=mysql_query("SELECT * from images where description LIKE '$value1' or name LIKE '$value1' order by name");
} else {
$mysql=mysql_query("SELECT * from images where ".$key."='$value'");
}
$page=1;
}
$ax=1;
echo '<table bgcolor="ffffff">';
while($row=mysql_fetch_assoc($mysql)) {

If you can't stand behind your troops, stand in front of them.
Semper Fidelis

Jim
 
Code:
foreach( $_GET as $key => $value){
	if($key == 'query') {
		mysql_query("SELECT * from images where description LIKE '%".mysql_real_escape_string($value) ."%' or name LIKE '%".mysql_real_escape_string($value) ."%' order by name")
		or die(mysql_error());
	} else {
		mysql_query("SELECT * from images where `$key`='". mysql_real_escape_string($value) . "'") 
		or die(mysql_error());
	}
	$page=1;
}
$ax=1;
 
Thank you for your answer. I run all get and post values through mysql_real_escape_string in my connection function. I just did an output of the final submitted mysql statement and it was still picking up another get I need to bypass the foreach and just do a straight if statement instead.

If you can't stand behind your troops, stand in front of them.
Semper Fidelis

Jim
 
Yep that was it. Just didnt use good troubleshooting. Should have made sure what was getting to the mysql lol

Code:
if(isset($_GET[search])) {
$value='%'.$_GET[search].'%';
$page=1;
$mysql=mysql_query("SELECT * from images where description LIKE '$value' or name LIKE '$value' order by name");
$sql="SELECT * from images where description LIKE '$value' or name LIKE '$value' order by name";
} else {
foreach( $_GET as $key => $value){
$mysql=mysql_query("SELECT * from images where ".$key."='$value'");
$page=1;
}
}
$ax=1;
echo '<table bgcolor="ffffff">';
while($row=mysql_fetch_assoc($mysql)) {

If you can't stand behind your troops, stand in front of them.
Semper Fidelis

Jim
 
also, in the loop you are not doing anything with the query other than executing it. i doubt whether that can be useful.
 
I just didnt post the rest of the code. Its just displaying images based on the query or the dropdown selection

If you can't stand behind your troops, stand in front of them.
Semper Fidelis

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top