Multiple SQL Queries, One Result - Help?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
uemtux
Forum Newbie
Posts: 8
Joined: Fri Jan 22, 2010 9:16 am

Multiple SQL Queries, One Result - Help?

Post 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
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: Multiple SQL Queries, One Result - Help?

Post by aravona »

try, this instead:

mysql_num_rows()

http://php.net/manual/en/function.mysql-num-rows.php
uemtux
Forum Newbie
Posts: 8
Joined: Fri Jan 22, 2010 9:16 am

Re: Multiple SQL Queries, One Result - Help?

Post by uemtux »

Nope, different function name, same error :)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Multiple SQL Queries, One Result - Help?

Post 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%"
uemtux
Forum Newbie
Posts: 8
Joined: Fri Jan 22, 2010 9:16 am

Re: Multiple SQL Queries, One Result - Help?

Post by uemtux »

Thank you, the quotes fixed my broken query! :)
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: Multiple SQL Queries, One Result - Help?

Post 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 ^_^
User avatar
akuji36
Forum Contributor
Posts: 190
Joined: Tue Oct 14, 2008 9:53 am
Location: Hartford, Connecticut

Re: Multiple SQL Queries, One Result - Help?

Post 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
Post Reply