here's the code
$query4 = "SELECT airport_name
FROM airports
WHERE airport_code = '$origin'";
$result4 = $dbh->query($query4);
$da = $result4->FETCH(PDO::FETCH_ASSOC);
$da_name = $da['airport_name'];
The above produces this warning message
Warning: Trying to access array offset on value of type bool in C:\xampp\htdocs\CAA_Tom\logbook.php on line 104
I don't even know where to begin.
What is the warning trying to tell me?
On Sun, 22 Aug 2021 20:00:04 -0400, Call Me Tom wrote:
here's the code
$query4 = "SELECT airport_name
FROM airports
WHERE airport_code = '$origin'";
$result4 = $dbh->query($query4);
$da = $result4->FETCH(PDO::FETCH_ASSOC);
$da_name = $da['airport_name'];
The above produces this warning message
Warning: Trying to access array offset on value of type bool in
C:\xampp\htdocs\CAA_Tom\logbook.php on line 104
I don't even know where to begin.
Start with looking at line 104 of the PHP code in file >C:\xampp\htdocs\CAA_Tom\logbook.php
What is the warning trying to tell me?
On line 104, you tried to access a variable as an array.
However, that variable /is not/ an array; it is a boolean
value. Thus, the array access failed.
From your code snippet above, I suspect that line 104 looks like
$a = $b['something'];
The error message is trying to tell you that
$b
is not an array (and helpfully tells you that it /is/ a boolean),
so it cannot resolve the array access
$b['something']
As to /why/ the variable is /not/ an array, you will have to determine
that for yourself.
With regards to the code snippet you attached here, I CAN tell you
that the PDO fetch() method returns an array on success, OR the
boolean value FALSE on failure. Perhaps you need take a look at
the code that initializes the suspect array named in line 104,
to see if the initializer does something similar.
HTH
$da_name = $da['airport_name']; is line 104. When I Access $da_name
in my table, it prints the airport name. Can you tell what it thinks
is a boolean?
$da_name = $da['airport_name']; is line 104. When I Access $da_name
in my table, it prints the airport name. Can you tell what it thinks
is a boolean?
On Mon, 23 Aug 2021 01:50:29 -0000 (UTC), Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote:
With regards to the code snippet you attached here, I CAN tell you
that the PDO fetch() method returns an array on success, OR the
boolean value FALSE on failure. Perhaps you need take a look at
the code that initializes the suspect array named in line 104,
to see if the initializer does something similar.
On 23/08/2021 07.03, Call Me Tom wrote:
$da_name = $da['airport_name']; is line 104. When I Access $da_name
in my table, it prints the airport name. Can you tell what it thinks
is a boolean?
The $da is boolean for the row before failed:
$da = $result4->FETCH(PDO::FETCH_ASSOC);
So you should check the $da value before using it
if($da) {
$da = $result4->FETCH(PDO::FETCH_ASSOC);
$da_name = $da['airport_name'];
} else {
// do what you need to tell it didn't find any thing
}
also worth mentioning, the variable $origin needs to be sanitized, as
it's a prone for SQL-injection. I do suggest you use withe list method,
where you check that the string has only allowed characters.
On Mon, 23 Aug 2021 09:13:04 +0200, J.O. Aho wrote:
On 23/08/2021 07.03, Call Me Tom wrote:
$da_name = $da['airport_name']; is line 104. When I Access $da_name
in my table, it prints the airport name. Can you tell what it thinks
is a boolean?
The $da is boolean for the row before failed:
$da = $result4->FETCH(PDO::FETCH_ASSOC);
So you should check the $da value before using it
if($da) {
$da = $result4->FETCH(PDO::FETCH_ASSOC);
$da_name = $da['airport_name'];
} else {
// do what you need to tell it didn't find any thing
}
ITYM
$da = $result4->FETCH(PDO::FETCH_ASSOC);
if ($da === FALSE)
{
/* fetch() failed for some reason. abort this activity */
}
else $da_name = $da['airport_name'];
And, to be complete, the OP should check the value in $result4
before invoking the fetch() method. So the logic fragment should
look more like
$result4 = $dbh->query($query4);
if ($result4 === FALSE)
{
/* query() failed for some reason - abort this activity */
}
else
{
$da = $result4->FETCH(PDO::FETCH_ASSOC);
if ($da === FALSE)
{
/* fetch() failed for some reason - abort this activity */
}
else $da_name = $da['airport_name'];
}
also worth mentioning, the variable $origin needs to be sanitized, as
it's a prone for SQL-injection. I do suggest you use withe list method,
where you check that the string has only allowed characters.
And, to be complete, the OP should check the value in $result4
before invoking the fetch() method. So the logic fragment should
look more like
$result4 = $dbh->query($query4);
if ($result4 === FALSE)
{
/* query() failed for some reason - abort this activity */
}
else
{
$da = $result4->FETCH(PDO::FETCH_ASSOC);
if ($da === FALSE)
{
/* fetch() failed for some reason - abort this activity */
}
else $da_name = $da['airport_name'];
}
here's the code
$query4 = "SELECT airport_name
FROM airports
WHERE airport_code = '$origin'";
$result4 = $dbh->query($query4);
$da = $result4->FETCH(PDO::FETCH_ASSOC);
$da_name = $da['airport_name'];
The above produces this warning message
Warning: Trying to access array offset on value of type bool in C:\xampp\htdocs\CAA_Tom\logbook.php on line 104
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 351 |
Nodes: | 16 (2 / 14) |
Uptime: | 27:32:38 |
Calls: | 7,634 |
Calls today: | 9 |
Files: | 12,796 |
Messages: | 5,688,672 |