0

I'm increment an ID from my code. When I retrieve the maximum value from database it does't assign to the variable.

I test my query from database SQL then it shows the maximum value. but when I echo it doesn't show.

Here is my code,

<html>
    <body>
        <form enctype="multipart/form-data" method="post" action="test_issue.php">
           <input name="submit" type="submit" id="submit" value="submit"  /> 
        </form>
    </body>
</html>

<?php
    include ("connection.php");
            if(isset($_POST['submit']))
            {               
                    // $queryselect =  "SELECT issued_id FROM pass WHERE pass_id=8";
                    $queryselect= "SELECT MAX(issued_id) FROM pass WHERE status='Issued'";
                    $last_sec_result = mysqli_query($connection,$queryselect );

                    if($last_sec_result!=0)
                    {
                        $last_sec_value = mysqli_fetch_array($last_sec_result);
                        $sec_id_no = $last_sec_value['issued_id'];
                        echo $sec_id_no;   
                    }
                    else
                    {
                        echo $last_sec_result ;
                        echo "database query failed please check data you enter.";
                    }
                    // echo $sec_id_no;
            }
?>

When I retrieve value with a simple select query (SELECT issued_id FROM pass WHERE pass_id=8) it shows the value. I'm stuck with this. Can anyone help me Please.

6
  • what does echo $sec_id_no; give Commented Jun 22, 2019 at 8:50
  • its on the page. I just echo to check whether value was passed or not. Commented Jun 22, 2019 at 8:51
  • did you try var_dump();? Commented Jun 22, 2019 at 8:54
  • yes It shows null. Commented Jun 22, 2019 at 9:00
  • @M.Hemant it shows value when use var_dump but when I echo it doesn't show the value. Commented Jun 22, 2019 at 9:06

3 Answers 3

1

Your query defines column MAX(issued_id) without alias, so, in fetched record it has some fancy name like... well, MAX(issued_id).

Try to change your query like this (note AS issued_id part):

SELECT MAX(issued_id) AS issued_id FROM pass WHERE status='Issued'

This will make sure fetched record hasissued_id field, and your following code will be able to extract it.

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

Comments

0

1- mysql_query() doesn't return an integer as your condition is waiting for 2- use alias to be sure about the index in the returned result set

include ("connection.php");
        if(isset($_POST['submit']))
        {               
                // $queryselect =  "SELECT issued_id as issued_id FROM pass WHERE pass_id=8";
                $queryselect= "SELECT MAX(issued_id) as issued_id FROM pass WHERE status='Issued'";
                $last_sec_result = mysqli_query($connection,$queryselect );

                if($last_sec_result !== null)
                {
                    $last_sec_value = mysqli_fetch_array($last_sec_result);
                    $sec_id_no = $last_sec_value['issued_id'];
                    echo $sec_id_no;   
                }

                else
                {
                    echo $last_sec_result ;
                    echo "database query failed please check data you enter.";
                }
                // echo $sec_id_no;
        }

2 Comments

I converted by using (int)$variable then it shows 0 but the value is not zero.
SELECT MAX(issued_id) as issued_id FROM pass WHERE status='Issued', run this query directly in phpmyadmin or whatever.
0

So if previous answers do not give you an expected solution then there are no rows with status set to exact same content 'Issued', that is why query return empty result array.

SELECT issued_id, status FROM pass;
# must return some rows

SELECT MAX(issued_id) AS issued_id FROM pass;
# must return some numbers

SELECT MAX(issued_id) AS issued_id FROM pass WHERE status='Issued';
# should return expected value

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.