results from mysql_query php use it in another query mysql_query

0

I'm trying to do 2 PHP queries ...

basically it would be the first one:

$sql = \mysql_query("SELECT * FROM tblaccounts WHERE date BETWEEN '2015-12-25 0:00:00' AND '2016-07-20 23:59:00'");

the results of this query I need to be able to use them to generate the data of another query ... example:

 while($r2 =  mysql_fetch_array(mysql_query("SELECT * FROM tblinvoiceitems WHERE invoiceid = '$RESULTADO_DE_LA_PRIMERA_CONSULTA'"))){ 

 echo "'.$r2[invoiceid].'";

 } 

If you see in the query there is another variable that would be the result of the first query ....

the first query throws a series of fields of 1 table ....

How could this be achieved?

    
asked by Jesús Suárez 10.09.2016 в 11:21
source

2 answers

0

You can use a subquery and this saves you having to double consult, as long as you do not need any other data from the first one. The query would be like this:

SELECT * FROM tblinvoiceitems WHERE invoiceid in(SELECT valor de invoiceid FROM tblaccounts WHERE date BETWEEN '2015-12-25 0:00:00' AND '2016-07-20 23:59:00')

Another way to do it would be to go through the results of the first consultation and generate the second query, as you plan

<? 
$db_link=conectarse();
$consulta="SELECT * FROM tblaccounts WHERE date BETWEEN '2015-12-25 0:00:00' AND '2016-07-20 23:59:00'";
$sql=mysql_query($consulta, $db_link);
$num_reg = mysql_num_rows($sql);
if ($num_reg!=0){
   while ($mostrar = mysql_fetch_array($sql)){
          $consulta2="SELECT * FROM tblinvoiceitems WHERE invoiceid = =$mostrar[id]";
          $sql2=mysql_query($consulta2, $db_link);
          $num_reg = mysql_num_rows($sql2);
          if ($num_reg!=0){
             while ($equipos = mysql_fetch_array($sql2)){ 
             }
          }
          mysql_free_result($sql);
      }
?>
    
answered by 10.09.2016 / 11:52
source
0

Thank you very much, I solved it integrating the query to the main one with inner and join ... let's say that relating tables like this:

$sql = mysql_query("SELECT * from tblaccounts inner join tblinvoiceitems on tblinvoiceitems.invoiceid=tblaccounts.invoiceid WHERE tblaccounts.date BETWEEN '$_POST[date1] 0:00:00' AND '$_POST[date2] 23:59:00'"); 
    
answered by 11.09.2016 в 11:45