peterbarone Posted February 20, 2007 Share Posted February 20, 2007 $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 More sharing options...
printf Posted February 20, 2007 Share Posted February 20, 2007 Where is $d and $e coming from? Link to comment https://forums.phpfreaks.com/topic/39302-solved-useing-data-from-2-querys/#findComment-189478 Share on other sites More sharing options...
peterbarone Posted February 20, 2007 Author Share Posted February 20, 2007 I want to declare them based on the results so to start $d=0 and $e=0 Link to comment https://forums.phpfreaks.com/topic/39302-solved-useing-data-from-2-querys/#findComment-189482 Share on other sites More sharing options...
dustinnoe Posted February 20, 2007 Share Posted February 20, 2007 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++;} Link to comment https://forums.phpfreaks.com/topic/39302-solved-useing-data-from-2-querys/#findComment-189489 Share on other sites More sharing options...
printf Posted February 20, 2007 Share Posted February 20, 2007 <? $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; ?> Link to comment https://forums.phpfreaks.com/topic/39302-solved-useing-data-from-2-querys/#findComment-189490 Share on other sites More sharing options...
dustinnoe Posted February 20, 2007 Share Posted February 20, 2007 @printf: Don't you need to join the two tables with the WHERE clause? Link to comment https://forums.phpfreaks.com/topic/39302-solved-useing-data-from-2-querys/#findComment-189500 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.