Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My SQL Query

Status
Not open for further replies.

Thomas001

Programmer
Jun 24, 2004
29
US
Code:
$row = mysql_query("SELECT dept FROM $db_table WHERE superemail = '$sent_superemail' ");
$set_dept = $row[?];

$sql = "SELECT * FROM $dbtable WHERE dept = '$set_dept' "

Basicly I'm trying to get the dept based on the superemail, but I can't figure out how I would do it. I'm farely new to SQL.


Thanks.
 
$row is not the resultset, its a pointer to the resultset



Code:
$row = mysql_query("SELECT dept FROM $db_table WHERE superemail = '$sent_superemail' ");

if($row){
  $my_row = mysql_fetch_array($row);

$set_dept = $my_row['dept'];

$sql = "SELECT * FROM $dbtable WHERE dept = '$set_dept' "

Bastien

Cat, the other other white meat
 
What if the superemail has multiple depts and I need to retreive all of them and use them in the next query?
 
You'll have to loop through the result set and build the WHERE clause.

Code:
$count = 0;
$result = mysql_query("SELECT dept FROM $db_table WHERE superemail = '$sent_superemail' ");

$query = "SELECT * FROM $dbtable WHERE dept IN ("
if($result){
    while ($row = mysql_fetch_array($result)) {
        if ($count > 0)
            $query .= ",'" . $row['dept'] . "'";
        else
            $query .= "'" . $row['dept'] . "'";
        $count = $count + 1;
    }
    $query .= ")";
} else {
    echo 'error executing query.";
}

*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
this is still only giving me back one entry.

Code:
$count = 0;
$result = mysql_query("SELECT dept FROM $dbtable WHERE superemail = '$sent_superemail' ");
$query = "SELECT * FROM $dbtable WHERE dept IN (";
if ($result)
 {
    while ($row = mysql_fetch_array($result))
    {
        if ($count > 0)
             $query .= ",'" . $row['dept'] . "'";
            else
             $query .= "'" . $row['dept'] . "'";
             
        $count = $count + 1;
    }
    $query .= ")";
 } 
 else
 
echo the $query after this processing, to see what the SQL looks like.

*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
I did, it returns:

Code:
Print("[$sent_superemail] controls [$query] with a total of [$count]");


[JMONTGOM] controls [SELECT * FROM employees WHERE dept IN ('E1433')] with a total of [1]
 
So, it looks to me as if you're only returning one department in the original query.

How many are you expecting?

*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
Give or take 15 different dept numbers.

Perhaps it's the SQL statement? The loop looks flawless. Or maybe there should be a SQL statement within the loop.
 
For testing-

Print out the previous SQL statement, as well as mysql_num_rows($result) right after the $result = ... above the while loop.

What do you get?

*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
nevermind, apparrently there is only one entry.

Though I may have to create another thread to house my next problem..

thanks cLFlaVA.
 
If its mysql related, probably a better home for it will be in the MySQL Forum

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top