Re: Multiple MySQL Queries

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

 



Bastien Koert wrote:
> On Tue, Jul 28, 2009 at 10:11 AM, <sono-io@xxxxxxxxxxxxx> wrote:
>>        This may be more of a MySQL question than PHP, but I'm hoping someone
>> can point me in the right direction.  I have working code (below) that pulls
>> data from a particular category in our db.  I'd like to be able to pull data
>> from multiple categories in the same db and place them on the same page.
>>
>>        I've been working on this for the last 4 days with no luck.  Some
>> pages say that you can't do multiple MySQL queries while others say there is
>> a workaround but I haven't found it.  Does anyone know how to do this?
>>
>> Thanks,
>> Frank
>>
>> --------
>>
>> <?php require_once("/home/balcone/db_login.php"); ?> // in header
>>
>> <?php
>>        $item_list = "";
>>        $cat1 = "01100-01200-01300-06403";
>>        $result = mysql_query("SELECT itemid,description,unitprice FROM
>> catalog WHERE categories='" . $cat1 . "'  ORDER BY itemid",$db);
>>
>> while ($item = mysql_fetch_assoc($result))
>> {
>> $item_list .= "<tr><td>" . $item['itemid'] . "</td>
>>        <td><a href=\".../shop.cgi?c=detail.htm&amp;itemid=" .
>> $item['itemid'] . "=1\">". $item['description'] ."</a></td>
>>        <td align=\"right\">$". money_format('%i', $item['unitprice'])
>> ."</td>
>>        <td></tr>";
>> }
>> echo "<table border=\"1\"><tr><th>Item ID</th><th>Description<br /><font
>> size=\"-1\">(Click for more info)</font></th><th>Price
>> Each</th><th>Purchase</th></tr>$item_list</table>";
>> ?>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
> 
> change it to an IN clause for the categories
> 
> $result = mysql_query("SELECT itemid,description,unitprice FROM
> catalog WHERE categories in('$cat1',"$cat2',$cat3','$catN')  ORDER BY
> itemid",$db);
> 
> 

Or if you want the results in separate "tables", do this.

<?php require_once("/home/balcone/db_login.php"); ?> // in header
<?php
$item_list = "";
$cats = array('01100', '01200', '01300', '06403');

foreach ( $cats AS $cat ) {
	$cat = mysql_real_escape_string($cat, $db);
	$SQL = "SELECT	itemid,
			description,
			unitprice
		FROM	catalog
		WHERE	categories='{$cat}"'
		ORDER BY itemid";
	;

	if ( ( $results = mysql_query($SQL, $db) !== false ) {
		echo <<<HTML
<table border="1">
	<tr>
		<th>Item ID</th>
		<th>Description<br />
		<font size="-1">(Click for more info)</font></th>
		<th>Price Each</th>
		<th>Purchase</th>
	</tr>
HTML;
		while ( $item = mysql_fetch_assoc($result) ) {
			$price = money_format('%i', $item['unitprice']);
			echo <<<ROW
	<tr>
		<td>{$item['itemid']}</td>
<td><a href="../shop.cgi?c=detail.htm&itemid={$item['itemid']}">
{$item['description']}</a></td>
	 	<td align="right">{$price}</td>
	</tr>

ROW;
		}
		echo '</table>';
	} else {
		echo "No results for category #{$cat}!";
	}
}

?>



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