1

I want to make if condition according to whether a query is null or not and print two different tables. following is my code.

$number_of_date= "SELECT SUM(number_of_date) AS number_of_date
                    FROM emp_leaves
                    WHERE emp_id='$userID'
                      AND leave_category='Annual'
                      AND apply_year='$year'";
$number_date = mysql_query ($number_of_date);

if (($number_date == 'NULL'))
{

    $result2 = "SELECT * FROM leave_info
                  WHERE employment_type='permanent'
                    AND leave_type='annual'";
    $sql2 = mysql_query ($result2);

    while($row = mysql_fetch_array($sql2)) {
       ?>
    <tr>
      <td><?php echo $row["leave_type"]?></td>
      <td><?php echo $row["leave_count"]?></td>
      </td></tr><?php }
}
else
{
    $sql = "SELECT * FROM emp_leaves
              WHERE emp_id='$userID'
                AND leave_category='Annual'
              ORDER BY leave_id DESC limit 1";
    $result = mysql_query ($sql);

    while($row = mysql_fetch_array($result)) {
    ?>
    <tr>
        <td><?php echo $row["leave_category"]?></td>
        <?php }?><?php
    while($row11 = mysql_fetch_array($number_date)) {
            ?>
            <td><?php echo $row11["number_of_date"]?></td><?php } } ?>
    </tr>

But, "If" condition is not work. What is the correct way of make that condition. Anyone please help me!

Darwin von Corax
  • 5,201
  • 3
  • 17
  • 28
Chathurika
  • 419
  • 2
  • 6
  • 18

5 Answers5

2

you can use isset() or empty() php function.

if(isset($number_date)) or if(empty($number_date))

EDIT

take a look HERE

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

so please try this.

if (mysql_query ($number_of_date)) or in your code edit like this.

if (($number_date == true))

Community
  • 1
  • 1
Archish
  • 850
  • 8
  • 32
2

Check wether query execute or result have not a row

  if (!$number_date or count($number_date) < 1){
      if(mysql_error()){
          die('Invalid query: ' . mysql_error());
        }
    }

mysql_query($sql) execute query and return a result,we need to check,query is executed or not using if condition if($number_date) means query executed,if(!$number_date) means query not executed

user229044
  • 232,980
  • 40
  • 330
  • 338
Parth Chavda
  • 1,819
  • 1
  • 23
  • 30
  • Fine, but execute only if condition always, not go the else statement, – Chathurika May 17 '16 at 05:11
  • only go to the else statement when your query executed and result is more than 1 row if you want to only check to query executed or not than remove `count($number_date) < 1` – Parth Chavda May 17 '16 at 05:13
  • @Chathurika can you explain me mean of "query is null" means return result have no value ?? – Parth Chavda May 17 '16 at 05:15
  • It means, if the emp_leaves table not has any sum value of number of date, according to particular user ID. – Chathurika May 17 '16 at 05:19
  • `count($number_date)` show number of returned value if 0 means no value else that have some value – Parth Chavda May 17 '16 at 05:22
  • @Chathurika if always goes in the if condition it means result is 0 or your query is not executed plz write code in if `die('Invalid query: ' . mysql_error());` – Parth Chavda May 17 '16 at 05:25
  • @Chathurika don't use `mysql` instead of use `mysqli`,`mysql` is deprecated and no more longer sported – Parth Chavda May 17 '16 at 06:04
1

try,

    $number_of_date= "SELECT SUM(number_of_date) AS number_of_date
                      FROM   emp_leaves
                      WHERE  emp_id='$userID' AND
                             leave_category='Annual' AND
                             apply_year='$year'";

    $res_qry     = mysql_query($number_of_date) 
                  or 
                  die ('Invalid query :: <br/>'.$number_of_date.
                                       ' <br/>'.mysql_error());

    $rowqry      = mysql_fetch_assoc($res_qry);
    $number_date = $rowqry ['number_of_date'];

now use, $number_date

    if (($number_date == '') || empty($number_date))

if the query returns NULL, normally PHP considers it as a empty..

you can also try is_null()

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

Hytool
  • 1,358
  • 1
  • 7
  • 22
1

I assume you're referring to the line

if (($number_date == 'NULL'))

This will not work, as you're comparing the variable $number_date to the string 'NULL', not to the value NULL. In order to test whether mysql_query ($number_of_date) returns NULL, you need to use the function is_null(), as

if (is_null($number_date))

Having said that, let me also say DO NOT USE THE mysql_* API! This API is deprecated for a number of very good reasons; please migrate your code to use either mysqli or PDO. Both of these APIs provide prepared statements, which (among other things) provide a measure of protection against SQL injection attacks.

Community
  • 1
  • 1
Darwin von Corax
  • 5,201
  • 3
  • 17
  • 28
1

From mysql_query() manual:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

So your check == 'NULL' is wrong. Function will return handle if query succeeded or FALSE otherwise. You can check if something was found by !== false. Any other answer here with isset(), empty() or if (!$number_date) are correct too.

Also, note that mysql extension in PHP is deprecated. Better use other extension like mysqli or PDO.

Andrew
  • 1,858
  • 13
  • 15