0

I have an orders table in mysql and in that for some orders I set particular order status like 'review'. I want to setup a way if any order placed by a particular customer(first and last name) for whom i have previously set order status as 'review' to display a warning in the list.

$sql = "select * from order where firstname = ".$firstname." AND lastname = ".$lastname." AND order_status = 'review';";
                $SQLresult = mysql_query("$sql", $DBcon_MySQL);

            while($row = mysql_fetch_array($SQLresult)) {       
                    foreach($row as $row){

                        $result = "warning!";
                        echo $result;
                    }
            }

The above code does not display anything. please let me know how to fix this.

[EDIT After Applying Answer]

This is how i am using it.

<td width="200">
    <? 
        $sql = "select * from cust_order where firstname = '$firstname' AND lastname = '$lastname' AND order_status = 'review'";
        $SQLResult = mysql_query("$sql", $DBcon_MySQL);

        while($row = mysql_fetch_array($SQLResult )) {      
                //$result;
                foreach($row as $row ){
                    //$result="";

                    $result = "Warning!";

                }

    ?>

        <p><? echo $result;?></p>   
        <?} ?>      
    </td>

How should i insert a check that it should display warning only once No matter how many orders from single customer are marked as review, display warning only once?

7
  • 1
    Enclose text variables in single quotes so that the final query will look like this: "... WHERE firstname = 'john' AND ...". Please explain what output you are expecting from this code. Commented May 25, 2016 at 20:31
  • I didn't get what you mean by enclose in single quote. The result i want is, by searching order table with first and last name, if there is any order with order status review, display a warning. Commented May 25, 2016 at 20:39
  • 1
    $sql = "select * from order where firstname = '".$firstname."' AND lastname = '".$lastname."' AND order_status = 'review';" Note the ' around the variables. Commented May 25, 2016 at 20:41
  • where firstname = '".$firstname."' AND lastname = '".$lastname."' AND Text fields must be in single quotes, but that is probably just the first and most obvious of your issues. You must also makes sure you deal with names that have the single quotes in them like O'Brien. Commented May 25, 2016 at 20:42
  • Oh i didn't notice i dont have single quotes. When i added quotes I now get 'Warning!' twice. Commented May 25, 2016 at 20:52

1 Answer 1

1

try this,

    $sql = "SELECT 
                    * 
            FROM
                    `order` 
            WHERE 
                    firstname = '$firstname' AND lastname = '$lastname' AND 
                    order_status = 'review' LIMIT 1";

    $SQLresult = mysql_query($sql, $DBcon_MySQL);

    while($row = mysql_fetch_array($SQLresult)) {       
        foreach($row as $row){

            $result = "warning!";
            echo $result;
        }
    }

Please be informed that mysql functions are deprecated and not recommended. USE MySQLi or PDO instead. have a reference from following queries.

http://php.net/manual/en/book.mysqli.php

http://php.net/manual/en/book.pdo.php

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

3 Comments

Hi This also works but the issue i am getting is multiple echoes. I think it is due to the fact that if one customer has multiple orders, and i have set order status to review for them, it echoes each time. For example, if 1 customer has 5 orders under status review, then it is echoing "Warning!" 5 times. I have edited the Question to Add Editted Answer and Issues related to those. Please have a look at that. Thanks
USE LIMIT 1, see the answer !
So simple, I actually stored result in an array and called them outside of while loop. But yours is simple. Thank you

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.