Page 1 of 1

Multiple SQL Queries, One Result - Help?

Posted: Mon Jan 25, 2010 10:56 am
by uemtux
So I'm writing a shopping cart, and one of the functions is when someone visits an individual product view they're given a view of 'related products', All product records have a field called 'keywords' which contains a list of comma delimited keywords.

I construct the query with a loop, and I've been echoing it to inspect it before it gets passed to the mysql_query() function.

Here is the string, maybe one of you can tell me why this query doesn't work ... ?

Code: Select all

SELECT * FROM books WHERE keywords LIKE %safety% OR keywords LIKE %marine% OR keywords LIKE %usmc% OR keywords LIKE %naval safety center% OR keywords LIKE %leader%
$result = mysql_query($query);
$num=mysql_numrows($result);

The error i get is this: Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource

Re: Multiple SQL Queries, One Result - Help?

Posted: Mon Jan 25, 2010 10:58 am
by aravona
try, this instead:

mysql_num_rows()

http://php.net/manual/en/function.mysql-num-rows.php

Re: Multiple SQL Queries, One Result - Help?

Posted: Mon Jan 25, 2010 11:00 am
by uemtux
Nope, different function name, same error :)

Re: Multiple SQL Queries, One Result - Help?

Posted: Mon Jan 25, 2010 11:02 am
by requinix
mysql_numrows is a deprecated alias of mysql_num_rows. It works fine (though you really should be using mysql_num_rows instead).

The keywords need quotes around them.

Code: Select all

SELECT * FROM books WHERE keywords LIKE "%safety%" OR keywords LIKE "%marine%" OR keywords LIKE "%usmc%" OR keywords LIKE "%naval safety center%" OR keywords LIKE "%leader%"

Re: Multiple SQL Queries, One Result - Help?

Posted: Mon Jan 25, 2010 11:19 am
by uemtux
Thank you, the quotes fixed my broken query! :)

Re: Multiple SQL Queries, One Result - Help?

Posted: Mon Jan 25, 2010 11:20 am
by aravona
tasairis wrote:mysql_numrows is a deprecated alias of mysql_num_rows. It works fine (though you really should be using mysql_num_rows instead).
I didn't know that :) always got taught the latter ^_^

Re: Multiple SQL Queries, One Result - Help?

Posted: Mon Jan 25, 2010 11:27 am
by akuji36
I 've been told that the implode function

is the best way to handle multiple queries

follow this link to a vid tutorial:

http://www.learningnerd.com/phpmysql-day-4

also check this link which shows implode function in use:

http://codingforums.com/showthread.php?t=75107.

Implode creates one query while a simple insert creates
multiple queries which decrease server speed.

thanks

Rod
http://www.webpagesofease.com