0
    $id = 2;
    // query to fetch delayed
    $sql = "SELECT * FROM leads WHERE status = ('$id') LIMIT 1";
    $obResult = $objConnection->query($sql);
    // query to fetch if there is no delayed
    $q = "SELECT * FROM leads WHERE status = ('$id') AND later != '1' ORDER BY postnummer LIMIT 1";
    $oResult = $objConnection->query($q);
    // primary query to run, which makes use of the two above accordingly
    $query = "SELECT * FROM leads WHERE status = ('$id') LIMIT 1";
    $objResult = $objConnection->query($query);

    while ($row = $objResult->fetch_object()) {
        if (new DateTime() > $row->delay && $row->delay != '0000-00-00 00:00:00') {
            while ($row = $obResult->fetch_object()) {
                echo $row->firmanavn;
            }
        } else {
            while ($row = $oResult->fetch_object()) {
                echo $row->firmanavn;
            }
        }
    }

Changed my code to this, and still i got the same problem, the if clause if met, but it echoes from my else instead

6
  • if $id is given earlier why do query inside a loop? Commented Aug 19, 2014 at 20:32
  • I need this part in the second query: ORDER BY postnummer LIMIT 1 Commented Aug 19, 2014 at 20:34
  • @user he's saying you can make that query outside of the loop and save yourself some resources. Commented Aug 19, 2014 at 20:35
  • Learn how to use SQL JOINs, and then you won't need to execute queries inefficiently inside a PHP loop Commented Aug 19, 2014 at 20:36
  • Is it even possible to join the same table into itself? Commented Aug 19, 2014 at 21:26

2 Answers 2

2

Reason is that you are using same variable names that overwrite each other. $objResult rename this to something like $objResult2 for the inner query.

One thing to keep in mind is that your inner query inside a loop is really unnecessary unless you did not provide some piece of code. You can just put that query outside of while loop. Will save you time & memory.

Sign up to request clarification or add additional context in comments.

1 Comment

Id did that now, still if fails
0

Although I think there are other ways this code could be cleaned up, it seems to me that you're attempting to compare a DateTime object to a date string, which is going to yield unpredictable results. Try changing your while part to:

$current_date = new DateTime();

while ($row = $objResult->fetch_object()) {
    if (
        $current_date->format('Y-m-d H:i:s') > $row->delay &&
        $row->delay != '0000-00-00 00:00:00'
    ) {
        while ($row = $obResult->fetch_object()) {
            echo $row->firmanavn;
        }
    } else {
        while ($row = $oResult->fetch_object()) {
            echo $row->firmanavn;
        }
    }
}

I'm not sure this will fully solve your issues, but it should get you closer.

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.