0

I'm trying to write an if else statement dependent on the value of a mySQL field.

Basically, in the awards table, if the id_project is PRJ000 then I want the $tempID and $tempProjectName to be different values.

Right now this code runs but the results on the page are all following the "else" code block, which makes me think I've set up the "if" wrong. Any advice?

<?PHP
$query  = "SELECT * FROM `awards` ";
$query  .= "WHERE `active` = '1' ";
$query  .= "ORDER BY `date` DESC ";
$query  .= "LIMIT $idStart, 10 "; 
$result = mysql_query($query); 
while($row = mysql_fetch_array($result)){
    $query2  = "SELECT * FROM `projects` ";
    $query2  .= "WHERE `id_fm` = '".$row["id_project"]."' AND `active` = '1' ";
    $result2 = mysql_query($query2); 
    while($row2 = mysql_fetch_array($result2)){
    if($row["id_project"] == "PRJ000"){
        $tempID = "#";
        $tempProjectName = "General";
    }
    else {
        $tempID = $row2["id_x"];
        $tempProjectName = $row2["title"];
    }
    }
    ?>
4
  • 2
    What is the value of $row["id_project"]? Commented Jul 17, 2015 at 13:20
  • Syntactically it is fine. What are some of the values $row["id_project"] has at each iteration? Commented Jul 17, 2015 at 13:23
  • Sorry if this doesn't quite answer your questions, but $row["id_project"] is set to the value of the field in the mySQL table. When I echo $row["id_project"] it correctly outputs as other project numbers ) like PRJ012, PRJ0134, etc, but when included in the if statement it doesn't work. Commented Jul 17, 2015 at 13:27
  • Dont use mysql drivers they are deprecated ages ago and will be removed. Use mysqli or PDO Commented Jul 17, 2015 at 13:36

3 Answers 3

2

I misunderstood your code and made mistake in last answer, replacing with updated code

<?php
$query  = "SELECT * FROM `awards` ";
$query  .= "WHERE `active` = '1' ";
$query  .= "ORDER BY `date` DESC ";
$query  .= "LIMIT $idStart, 10 "; 
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$projectId = $row["id_project"]; //Set a variable here and use later
    $query2  = "SELECT * FROM `projects` ";
    $query2  .= "WHERE `id_fm` = '$projectId' AND `active` = '1' ";
    $result2 = mysql_query($query2); 
    while($row2 = mysql_fetch_array($result2)){
    if($projectId == "PRJ000"){
        $tempID = "#";
        $tempProjectName = "General";
    }
    else {
        $tempID = $row2["id_x"];
        $tempProjectName = $row2["title"];
    }
}
?>
Sign up to request clarification or add additional context in comments.

3 Comments

thanks! unfortunately that didn't work. While it looks like $row["id_project"] is set, $row2["id_project"] has no values. What's the best way to rewrite the code given this issue?
I updated the answer, i misunderstood the code, my bad
by the way you can do this with more simple query by join both tables
0

insted row to row2

   if($row2["id_project"] == "PRJ000"){
        $tempID = "#";
        $tempProjectName = "General";
    }
    else {
        $tempID = $row2["id_x"];
        $tempProjectName = $row2["title"];
    }

Comments

0

If you collected all the rows from the initial query (with a foreach or with the iterator_to_array() function, then you could loop over that simple array of data, and do the internal query at your leisure.

It would also make debugging easier, as you can just dump the rows between the searches easily.

In the long term, you could convert the query to JOIN the projects table to the awards table, based on id_project = id_fm, and so just have the project-specific data returned on its own for any further processing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.