Jump to content

Why did i get access array offset value on type null?


Abel1216

Recommended Posts


I am trying to get a user details from the database and compare it with the posted value. Now I get Trying to access array offset on value of type null in. The column name user_number exists in the database. This is my code below.. Thanks!!!

     <?php
     mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

     error_reporting(E_ALL | E_WARNING | E_NOTICE);

     ini_set('display_errors', TRUE);

      if (session_status() == PHP_SESSION_NONE) {
  session_start();

    }
     if(!isset($_SESSION['login']))
     {
     echo ("<script>location.href='../clogin/'</script>");
     die();
     }

     if (isset($_POST['submit'])) 
      {
      

        include_once('db.php');
       //get post details from user
             
        $UserNumber = $_POST['uNumber'];

        $sql = "SELECT * FROM customer WHERE user_number=?";   
        $stmt = $connection->prepare($sql); 
        $stmt->bind_param('i', $UserNumber);
        $stmt->execute();
        $result = $stmt->get_result();
        $count =  $result->num_rows;
      if($count == 1)
       { 
   
       while($row = $result->fetch_assoc());

         {


         $db_Uno = $row['user_number'];

           if($userNumber !== $db_Uno) 
           {


             
  echo'<script>swal.fire("FAILED!!", "<strong>No Customer with the user number you entered.</strong><hr><br/><i> Check well and try Again.</i>", "error");window.setTimeout(function(){
   window.location.href = "home.php";
   }, 2000);</script>'; 

             exit();
             }

              else
              {
               }


                 }//while loop
          
                  } //end of if rslt     


                    }// end submit for transfer post        
          
                      ?>

 

Link to comment
Share on other sites

The column may exist but that does not mean anything was returned by the query. DO NOT use * in the query. Only specify those columns you actually need to use. Add the following before that line so you can see what is being returned:

echo "<pre>";
print_r($row);
echo "</pre>";

 

Link to comment
Share on other sites

i just noticed the cause of the problem. you have a stray semi-colon ; on the end of the while() statement. this short-circuits the loop, so that it loops over all of the data first, then your code that you think is part of the loop, inside the {}, is actually after the end of the loop, where there is nothing in $row.

this is the the correct syntax for a while() loop when using { } around the conditionally executed statements -

while (some condition is true) {
    code to execute inside the loop
}

// or even better, put the opening { on the next line so that it is alighned with the matching }
while (some condition is true)
{
    code to execute inside the loop
}

  -

you shouldn't even be using a loop to fetch the data from a query that will at most match one row. just directly fetch the row of data.

btw - since you are comparing the user number in the query, if the query matched a row of data, you know that the user number was found. you don't need to compare it again in the php code. also, your comparison in the php code, because you are using an exact, not equal !== match, will always fail. by definition all  form data are strings, regardless of what value they hold and the fetched data from a mysqli query (if using the default settings) is the actual data type of the column in the table. a string data type (the variable with the form data) will always be not equal to an integer data type (the variable holding the fetched user number from the query.)

Edited by mac_gyver
  • Great Answer 1
Link to comment
Share on other sites

If your query is only intended to return one row, then you just fetch the row and check with a simple if statement whether data was fetched or not.

$row = $result->fetch_assoc();
if ($row){
    //Yep, got data
}

or

$row = $result->fetch_assoc();
if (!$row){
    //Nope, no data available.
}

depending on whether a positive or negative test fits best with your situation.

 

  • Great Answer 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.