1

I am making a form which will load leave already availed values from mysql database. But if the value is nothing it shows nothing in the output. But i need to show 0 in the ($avail field)for the null value how to get that what i have tried is

$sqls = "SELECT  (@days :=  a.days  + @days) AS availed_days,days from tblleaves a,(SELECT @days:=0) c where 
LeaveType = '$type' AND empid ='$eid' ORDER BY availed_days desc ";

$querys = $dbh -> prepare($sqls);
$querys->bindParam(':eid',$eid,PDO::PARAM_STR);
$querys->execute();
$results=$querys->fetchAll(PDO::FETCH_OBJ);
if($querys->rowCount() > 0)
{
foreach($results as $result)
{  
    $avail=($result->availed_days);
    //$actual=($result->days);

 ?>

 <input value=" <?php  echo $avail?>" id="avail_days" name="avail_days"  type="text"  >

3 Answers 3

1

You can use:

$avail = $result->availed_days ?? 0;

This is the so-called Null coalescing operator.

If $result->availed_days is null it will use the zero.

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

4 Comments

Thanks for your response sir, still i can't solve the issue
still the echo value field doesn't show anything
If one record is entered with value it shows the value perfectly.. without entering any records this issue occurs
Sorry, I cannot run your code, and see what your problem is.
0

Assuming the value contains integers besides the possible NULL value. Cast it to an integer.

<?php echo intval($avail); ?>

Or

<?php echo (int) $avail; ?>

1 Comment

Thanks for your response sir, still i can't solve the issue
0

Your problem seems to be when there is no data as it can't reach the code that initializes the $avail variable(You might wanna check your if condition).

As a workaround you can pre-assign $avail=0 before the if.

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.