• SQL subquery with multiple results.
    7 replies, posted
Hey, so I am running this query: [code]"SELECT * FROM BusDetails WHERE category=(SELECT name FROM cats WHERE parent='".$cat."')"[/code] and it doesn't work because subquery returns around 8-9 results. I was wondering how do I make it work without typing like 50 OR's ? BusDetails = details of business listings. Cats = categories and subcategories. Name field is name of subcategory and parent is name of main category.
[code]"SELECT * FROM BusDetails WHERE category IN (SELECT name FROM cats WHERE parent='".$cat."')"[/code] ?
deadeye536's solution will solve your issue, but you might want to take notice for future queries that you do not have to close the query and insert the variable with those dots. The query runs fine like this: [PHP] "SELECT * FROM BusDetails WHERE category IN (SELECT name FROM cats WHERE parent='$cat')" [/PHP]
[QUOTE=Svenskunganka;40971348]deadeye536's solution will solve your issue, but you might want to take notice for future queries that you do not have to close the query and insert the variable with those dots. The query runs fine like this: [PHP] "SELECT * FROM BusDetails WHERE category IN (SELECT name FROM cats WHERE parent='$cat')" [/PHP][/QUOTE] Even better yet, if you're using PHP, use prepared statements as offered by [url=http://us3.php.net/manual/en/intro.pdo.php]PDO[/url] and [url=http://us3.php.net/manual/en/intro.mysqli.php]MySQLi[/url].
Thank you guys. That worked.
Okay an update: my SQL seems to be fucked up, I mean database. Here is a screenshot of database records ( don't mind weird entries, they are for testing) [IMG]http://i42.tinypic.com/9uumpc.png[/IMG] But when I run following query no matter if I use the statement above or even just simple one like: [code]SELECT * FROM BusDetails WHERE category='$cat'[/code] $cat = "Accountants" it doesn't return anything. Even though there is a record with such category. And I know its not my PHP fault, I tried running this query in myadmin SQL. [editline]11th June 2013[/editline] Found my error. Turns out that when declaring array like this: $cats = array( "hi ","hello ","how ","are ","you"); caused to ti add [space] after every word. fixed it now.
[QUOTE=arleitiss;40982503]Okay an update: my SQL seems to be fucked up, I mean database. Here is a screenshot of database records ( don't mind weird entries, they are for testing) [IMG]http://i42.tinypic.com/9uumpc.png[/IMG] But when I run following query no matter if I use the statement above or even just simple one like: [code]SELECT * FROM BusDetails WHERE category='$cat'[/code] $cat = "Accountants" it doesn't return anything. Even though there is a record with such category. And I know its not my PHP fault, I tried running this query in myadmin SQL. [editline]11th June 2013[/editline] Found my error. Turns out that when declaring array like this: $cats = array( "hi ","hello ","how ","are ","you"); caused to ti add [space] after every word. fixed it now.[/QUOTE] Glad that it worked! As deadeye536 said, you should use prepared statements for your query. Here's the MySQLi version of it on the php manual: [URL="http://www.php.net/manual/en/mysqli-stmt.prepare.php"]Stmt Prepare[/URL]
[QUOTE=Svenskunganka;40990052]Glad that it worked! As deadeye536 said, you should use prepared statements for your query. Here's the MySQLi version of it on the php manual: [URL="http://www.php.net/manual/en/mysqli-stmt.prepare.php"]Stmt Prepare[/URL][/QUOTE] I will use that in future once I learn about it, I have to use any method right now because I need to finish this asap.
Sorry, you need to Log In to post a reply to this thread.