Jump to content

how to resolve errors like this in php 8 Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\AplikasiInventaris\admin\detailpinjam.php on line 7 Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\A


sandi123
 Share

Recommended Posts

<?php

require '../function.php';

 

$id_peminjaman = $_GET['id'];

$get = mysqli_query($con,"SELECT * FROM peminjaman WHERE id_peminjaman='$id_peminjaman'");

$fetch = mysqli_fetch_array($get);

$nama_pegawai = $fetch['nama_pegawai'];

$tanggal_pinjam = $fetch['tanggal_pinjam'];

$tanggal_kembali = $fetch['tanggal_kembali'];

?>

Link to comment
Share on other sites

the most immediate problem is that the query didn't match any data (or possibly didn't execute at all, but there would have been another php error at the mysqli_fetch_array statement.) see the php.net documentation for mysqli_fetch_array's return value - https://www.php.net/manual/en/mysqli-result.fetch-array.php

code should ALWAYS test if data was fetched from a query before trying to use the data, and setup a user message if there was no data. 

// at the point of using the data
if(!$fetch)
{
    echo 'no data was found.';
}
else
{
    // use the data here...
}

the most likely reason for no matching data is either because $_GET['id'] doesn't contain any value or the value it does contain doesn't match any data in the table. you should ALWAYS validate inputs to your code before using them. if $_GET['id'] isn't set, is empty, or doesn't contain an integer value > 0, that's an error. you should setup a user message in this case and not attempt to run the sql query.

you should ALWAYS have error handling for statements that can fail. for database statements that can fail - connection, query, prepare, and execute, the easiest way of adding error handling, without adding logic at each statement, is to use exceptions for errors and in most cases let php catch and handle the exception, where php will use its error related settings to control what happens with the actual error information, via and uncaught exception error (database statement errors will 'automatically' get displayed/logged the same as php errors.)

next, don't put external, unknown, dynamic values directly into an sql query statement. use a prepared query instead.

lastly, don't copy variables to other variables for nothing. this is just a waste of typing. just use the original variables.

  • Great Answer 1
Link to comment
Share on other sites

I agree with everything @mac_gyverwrote.

Please use the "<>" button to paste in your code in the future.

As to the error, it is telling you that you are trying to reference a key in an array, but the variable isn't an array at all, it is the null value.  Since it is the result of the fetch() that pretty much tells you the fetch failed.

Link to comment
Share on other sites

Here's is how you could handle the problems that people are mentioning.  Just a quick "how I would do it":

$errmsg = '';
if ($_GET['id'] == '')
	$errmsg .= 'Missing id value<br>';
else
{
	$q = "SELECT * FROM peminjaman WHERE id_peminjaman='" . $_GET['id'] . "'";
	if (!$get = mysqli_query($con, $q))
		$errmsg .= "Query failed to run<br>Query was:<br>$q<br>";
}
if ($errmsg <> '')
{
	//	show the errmsg and let the user know he has 
	//	a problem and stop this script
}
//  continue on with query results
$fetch = mysqli_fetch_array($get);
$nama_pegawai = $fetch['nama_pegawai'];
$tanggal_pinjam = $fetch['tanggal_pinjam'];
$tanggal_kembali = $fetch['tanggal_kembali'];
//  no need to do the above 'assignments' when you can simply
//	use the values from the fetched array
			

You really should check the results of your query since any error in  your statement or possibly with your db server will cause issues.  

  • Like 1
Link to comment
Share on other sites

On 1/9/2022 at 6:40 PM, ginerjm said:

Having not used the empty function much (ever?), I re-read the manual and like what I see there.  And found out why the OP reported a problem with it when the input was a '0'.

One reason I keep coming back to the forum, is that there are times when someone uses some syntax or a function or technique I'm not familiar with or wouldn't have thought to use.  Seeing what other people offer as a solution to questions is a nice diversion and helps keep the learning process fresh.

Link to comment
Share on other sites

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.

 Share

×
×
  • 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.