Jump to content

MYSQL Query question


ltbaggz

Recommended Posts

I have a general question to try and figure out the best way to handle multiple queries in php.  i have a page that queries 2 different tables and used data from both accessing it row at a time.  the problem is that they are using the same database connection so once i execute the other query the first query is no longer available.  Hopefully I explained this in a reasonable way. 

Now my question is what would be better for functionality if I do the first query run a loop and dump all the data into an array then do the next and continue that way, or use my mysql class and create 2 mysql objects one for each query?

i can get around the problem, just curious if one way is better than the other.

thanks
Drew
Link to comment
https://forums.phpfreaks.com/topic/33027-mysql-query-question/
Share on other sites

If you have a situation like this, where you pull a value from the first table to use as a parameter to pull records from the second table
[code]
<?php
    $resA = mysql_query ("SELECT colA FROM tableA WHERE colB = '$someval'");
   
    while ($rowA = mysql_fetch_assoc($resA)) {
       
        $a = $rowA['colA'];
       
        $resB = mysql_query ("SELECT colname FROM tableB WHERE somecol = '$a' ");
   
        while ($rowB = mysql_fetch_assoc($resB)) {
           
            echo $rowB['colname'] . '<br>';
           
        }
    }
?>
[/code]

then you should be using a single joined query
[code]
<?php
    $res = mysql_query ("SELECT b.colname FROM tableB b
                        INNER JOIN tableA a
                        ON b.somecol = a.colA
                        WHERE a.colB = '$someval'");
   
    while ($row = mysql_fetch_assoc($res)) {
                 
            echo $row['colname'] . '<br>';
           
    }
?>

[/code]
Link to comment
https://forums.phpfreaks.com/topic/33027-mysql-query-question/#findComment-153879
Share on other sites

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.