Abel1216 Posted July 16, 2020 Share Posted July 16, 2020 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/311102-why-did-i-get-access-array-offset-value-on-type-null/ Share on other sites More sharing options...
gw1500se Posted July 16, 2020 Share Posted July 16, 2020 What is the line causing the error? Why did you not post the entire error message? Quote Link to comment https://forums.phpfreaks.com/topic/311102-why-did-i-get-access-array-offset-value-on-type-null/#findComment-1579698 Share on other sites More sharing options...
Abel1216 Posted July 16, 2020 Author Share Posted July 16, 2020 Notice: Trying to access array offset on value of type null in ***/cpanel/t10.php on line 81 Quote Link to comment https://forums.phpfreaks.com/topic/311102-why-did-i-get-access-array-offset-value-on-type-null/#findComment-1579699 Share on other sites More sharing options...
Abel1216 Posted July 16, 2020 Author Share Posted July 16, 2020 1 hour ago, Abel1216 said: $db_Uno = $row['user_number']; That's line 81 Quote Link to comment https://forums.phpfreaks.com/topic/311102-why-did-i-get-access-array-offset-value-on-type-null/#findComment-1579700 Share on other sites More sharing options...
Abel1216 Posted July 16, 2020 Author Share Posted July 16, 2020 The column user_number exist in my database Quote Link to comment https://forums.phpfreaks.com/topic/311102-why-did-i-get-access-array-offset-value-on-type-null/#findComment-1579701 Share on other sites More sharing options...
gw1500se Posted July 16, 2020 Share Posted July 16, 2020 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/311102-why-did-i-get-access-array-offset-value-on-type-null/#findComment-1579702 Share on other sites More sharing options...
Abel1216 Posted July 16, 2020 Author Share Posted July 16, 2020 (edited) Just did so and I have a blank page Edited July 16, 2020 by Abel1216 Quote Link to comment https://forums.phpfreaks.com/topic/311102-why-did-i-get-access-array-offset-value-on-type-null/#findComment-1579703 Share on other sites More sharing options...
mac_gyver Posted July 16, 2020 Share Posted July 16, 2020 (edited) 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 July 16, 2020 by mac_gyver 1 Quote Link to comment https://forums.phpfreaks.com/topic/311102-why-did-i-get-access-array-offset-value-on-type-null/#findComment-1579704 Share on other sites More sharing options...
Abel1216 Posted July 16, 2020 Author Share Posted July 16, 2020 Wow. Tanks so much. Never saw it.. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/311102-why-did-i-get-access-array-offset-value-on-type-null/#findComment-1579717 Share on other sites More sharing options...
gw1500se Posted July 16, 2020 Share Posted July 16, 2020 Don't feel bad, neither did I. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311102-why-did-i-get-access-array-offset-value-on-type-null/#findComment-1579719 Share on other sites More sharing options...
Abel1216 Posted July 16, 2020 Author Share Posted July 16, 2020 So what is the best comparison to use @mac_gyver. Thanks. Am a learner. Don't mind my question Quote Link to comment https://forums.phpfreaks.com/topic/311102-why-did-i-get-access-array-offset-value-on-type-null/#findComment-1579721 Share on other sites More sharing options...
kicken Posted July 16, 2020 Share Posted July 16, 2020 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311102-why-did-i-get-access-array-offset-value-on-type-null/#findComment-1579731 Share on other sites More sharing options...
Abel1216 Posted July 16, 2020 Author Share Posted July 16, 2020 Thanks boss Quote Link to comment https://forums.phpfreaks.com/topic/311102-why-did-i-get-access-array-offset-value-on-type-null/#findComment-1579732 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.