Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql query condition check

Tags:

php

mysql

I'm having the table

  • sales_orders

In this table having column names

  • order_no
  • order_status
  • order_type

SQL query:

SELECT order_no FROM sales_orders where order_status='Pending' and order_type='1'

In the above query if the order_type=1 value doesn't exists in the database column that means instead of value 1 there is a value '0' , I want to display an error.

How to modify the above query for that?

like image 244
jax Avatar asked Nov 12 '22 17:11

jax


1 Answers

If I understand correctly, you want to show an error if order_type is not equal to 1. If so, you can do something like this:

$query = "SELECT order_no FROM sales_orders where order_status='Pending' and order_type = 1";

if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_object()) {
        if ($row->order_type != 1) {
            printf('There is no rail order for order number %d', $row->order_no);
        }
    }

    $result->close();
}
like image 86
Andris Avatar answered Nov 15 '22 07:11

Andris