Re: [php] & [mysql] select and subselect

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Allen McCabe wrote:

> I have a page on my site where I can optionaly filter by certain fields
> (order by filesize or file category), but I am implementing a shopping
> cart type of idea where users can submit an order.
> 
> As administrators, my coworkers and I need to be able to filter orders by
> their contents. For example:
> 
> View all orders for "Jack and the Beanstalk," where an order may have Jack
> and the Beanstalk and other items.
> 
> I have an order table that keeps track of the order_id, the date, the
> status, etc. I also have an order_lineitem table that is the contents of
> the order. This has a one-to-many structure (without foreign keys because
> it is mysql).
> 
> I was baffled as to how to filter the orders by the item_id that appears
> in the order_lineitem table.
> 
> I just came up with this, but I'm not sure how the mysql_queries will
> handle an array. Do I have to do some extensive regular expression
> management here to get this to work, or will it accept an array?
> 
> <?php
> 
> if (isset($_POST['showid']))
>    $showid = $_POST['showid'];
>    $subSQL = "SELECT order_id FROM afy_show_lineitem WHERE show_id =
> {$_POST['showid']};";
>    $subResult = mysql_query($subSQL);
>    $where = "WHERE";
>    $extQuery = 'order_id = {$subResult}';
>   }
> 
> $resultOrders = mysql_query("SELECT * FROM afy_order {$where}
> {$extQuery};") or die(mysql_error("Could not query the database!"));
> 
> ?>
If $_POST['showid'] is likely to be an array, use implode to create a comma
separated string of values, then use IN to build the query.

$ids = implode(',', $_POST['showid'];
$subSQL = "SELECT order_id FROM afy_show_lineitem WHERE show_id IN ($ids)"

But you need also to check your logic - your query above will have two WHERE
in it and will fail miserably :-) And your call to mysql_error will
probably not help you either, as the (optional) argument should be a link
identifier, not a string of your choosing.



Cheers
-- 
David Robley

MS-DOS: celebrating ten years of obsolescence
Today is Sweetmorn, the 29th day of The Aftermath in the YOLD 3175. 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux