I am trying to use table data in a single row data as the value of an variable in my code below and i keep getting "Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\done\test1.php on line 29"
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT lev1 FROM ref where id=1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "lev1: " . $row["lev1"]. "";
}
}
$mamo=$row["lev1"];
$conn->close();
what I'm I dong wrong ?
$rowis not an array, but only contains NULL. It appears you don’t actually think about the code that you are using, resp. did not bother to figure out who it works in the first place? The while loop over the result set ends, becausefetch_assocwill return NULL, after the last available record was processed in the previous iteration. So you can not access any data via $row now after the loop any more.$mamo=$row["lev1"];. Read the "Return value" section of thefetch_assocmanual.idis the PK thus unique, a loop is totally useless...