Jump to content

[SOLVED] useing data from 2 querys ?


peterbarone

Recommended Posts

$query = "SELECT * FROM  table1;

   

$result = mysql_query($query);

while ($row = mysql_fetch_array($result)){

 

    $a1=$row[price];

 

$query2 = "SELECT * FROM  table2;

 

$result = mysql_query($query2);

while ($row = mysql_fetch_array($result)){

 

    $b1=$row[cost];

 

$c1=$a1-$b1;

 

 

Up to there I'm good.

 

Now what I would like to happen is

 

if $c1=1 then $d+1

if $c1=2 then $e+1

 

Sorry if that is not really clear.  I'm new as you can tell

Link to comment
https://forums.phpfreaks.com/topic/39302-solved-useing-data-from-2-querys/
Share on other sites

why not join the tables so that you only need one SQL query?

$d = 0;
$e = 0;
$query = "SELECT table1.price, table2.cost FROM table1, table2 WHERE table1.someColumn = table2.someColumn";
$result = mysql_query($result);
while ($row = mysql_fetch_assoc($result))
{
     $a1=$row[price];
     $b1=$row[cost];
     $c1=$a1-$b1;
}
if ($c1 == 1){$d++;}
if ($c1 == 2){$e++;}

 

 

<?

$d = $e = 0;

mysql_connect ( 'localhost', 'user', 'pass' );
mysql_select_db ( 'dbname' );

$query = mysql_query ( "SELECT (ta.price - tb.cost) AS number FROM table1 AS ta, table2 AS tb;" );

if ( mysql_num_rows ( $query ) > 0 )
{
while ( $r = mysql_fetch_assoc ( $query ) )
{
	( $r['number'] == 1 ? $d++ : $e++ );
}
}

echo '$d = ' . $d;
echo '<br />';
echo '$e = ' . $e;

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.