Forum Moderators: coopster

Message Too Old, No Replies

valid MySQL syntax

         

snowman304

10:14 pm on Jul 13, 2007 (gmt 0)

10+ Year Member



I thought for sure I had this query correct, but for some reason, it isn't working. I get this error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in......

I am working on a search for my DB. Nothing advance yet, just one input field for the search keyword. Here is my query:

$sqlQuery = "SELECT * FROM log WHERE wTemp like '%$thisKeyword%' OR wSpeed like '%$thisKeyword%' 
OR wDirection like '%$thisKeyword%' OR sky like '%$thisKeyword%' OR precipitation like '%$thisKeyword%'
OR pressure like '%$thisKeyword%' OR moon like '%$thisKeyword%' OR spot like '%$thisKeyword%'
OR waterName like '%$thisKeyword%' OR waterTemp like '%$thisKeyword%' OR city '%$thisKeyword%'
OR state like '%$thisKeyword%' OR clarity like '%$thisKeyword%' OR bottom like '%$thisKeyword%'
OR bType like '%$thisKeyword%' OR bName like '%$thisKeyword%' OR rig like '%$thisKeyword%'
OR species like '%$thisKeyword%' OR length like '%$thisKeyword%' OR weight like '%$thisKeyword%'
OR cTime like '%$thisKeyword%' OR cDate like '%$thisKeyword%' OR cDepth like '%$thisKeyword%'";

If I do a single table field, it works good, but for some reason, this "OR" statement doesn't work.

Any ideas?

Thanks,
Mike

phranque

10:43 pm on Jul 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, snowman304!

does it work with just 2 fields?

also, i'm not sure about variable interpolation in php, but could that be an issue?

mysql_query should return either a resource set or FALSE, so you could always try this:

$result = mysql_query($sqlQuery);
if (!$result) {
die('query error: ' . mysql_error());
}

snowman304

11:22 pm on Jul 13, 2007 (gmt 0)

10+ Year Member



Thanks for the welcome. I will have to get use to this forum, it is a little different then what I am use to. lol There is alot of information on this site, I am glad I found it.

The full error says this:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\FishingLog\search.php on line 38
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 ''%78%' OR state like '%78%' OR clarity like '%78%' OR bottom like '%78%' OR bTy' at line 1

line 38 in my code is where the "SELECT * FROM" starts.

I double checked the variable names and they all match up.

Thanks for the tip in trying 2. I just tested it, and it worked with 2 using the "OR" statement. So.........I guess I will keep adding additional fields in until it stops working.

Thanks,
Mike

snowman304

11:31 pm on Jul 13, 2007 (gmt 0)

10+ Year Member



Ok, after testing each field, by removing them one at a time until it started working, I found out it is the "OR city '%$thisKeyword%'" that is causing the error. Still haven't figured out why, but at least I am one step further in solving it. lol

I guess now I will go through and double check everything dealing with the city field.

dreamcatcher

11:03 am on Jul 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Mike,

You are missing an operator.

OR city '%$thisKeyword%'" doesn`t tell the query anything. Need to be something like:

OR city = '%$thisKeyword%'"
OR city LIKE '%$thisKeyword%'"

dc

snowman304

11:02 pm on Jul 14, 2007 (gmt 0)

10+ Year Member



Thanks so much dreamcatcher. In the mix of all those fields all saying the same thing, I must have over looked that mistake a million times. I don't know how many times I broke that part down and checked each field. Never seen the missing "Like".

Thanks so much.

-Mike