Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #18783
| From | Arno Welzel <usenet@arnowelzel.de> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: Help With Warning Message |
| Date | 2021-08-30 10:21 +0200 |
| Message-ID | <ip3inoFdl1gU1@mid.individual.net> (permalink) |
| References | <7no5igtt055jqdalqa2u2vlkfeu6bvhnag@4ax.com> |
Call Me Tom:
>
> 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
In line 104 of your code you try to access a variable as array but the
variable is a bool.
I believe this is your problem:
$da = $result4->FETCH(PDO::FETCH_ASSOC);
$da_name = $da['airport_name'];
1) Do NOT just copy & paste code which you find somewhere! Learn how PHP
and PDO works first!
2) The method is not "FETCH()" but "fetch()", also see here:
<https://www.php.net/manual/en/pdostatement.fetch.php>
3) Read the documentation:
"The return value of this function on success depends on the fetch type.
In all cases, false is returned on failure."
This means: if the fetch did not return anything the result will not be
an array but the boolean value "false".
You should also never use any string in an SQL statement without
escaping! This will most likely cause a security issue due to possible
SQL injections one day!
A good way to avoid SQL injections is to use prepared statements, also
see here:
<https://www.php.net/manual/en/pdostatement.execute.php>
So one solution could be:
// Prepare the SELECT statement with a parameter for the code
$statement = $dbh->prepare(
"SELECT airport_name FROM airports WHERE airport_code = :code"
);
// Bind the parameter
$statement->bindParam(':code', $origin);
// Execute the prepared statement
$result4 = $statement->execute();
// Fetch the result
$da = $result4->fetch(PDO::FETCH_ASSOC);
// Only use the result if there is one, otherwise set the airport
// name to an empty string
//
// Also keep in mind that "yoda conditions" (value first, variable
// second) are the preferred way to avoid accidental assignments of
// values (e.g. "if ($var = true)" instead of "if ($var === true)")
if (false !== $da) {
$da_name = $da['airport_name'];
} else {
$da_name = '';
}
--
Arno Welzel
https://arnowelzel.de
Back to comp.lang.php | Previous | Next — Previous in thread | Find similar | Unroll thread
Help With Warning Message Call Me Tom <noemail@noemail.com> - 2021-08-22 20:00 -0400
Re: Help With Warning Message Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-08-23 01:50 +0000
Re: Help With Warning Message Call Me Tom <noemail@noemail.com> - 2021-08-23 01:03 -0400
Re: Help With Warning Message "J.O. Aho" <user@example.net> - 2021-08-23 09:13 +0200
Re: Help With Warning Message Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-08-23 18:19 +0000
Re: Help With Warning Message Call Me Tom <noemail@noemail.com> - 2021-08-23 15:26 -0400
Re: Help With Warning Message "J.O. Aho" <user@example.net> - 2021-08-24 00:33 +0200
Re: Help With Warning Message Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-08-23 11:47 +0000
Re: Help With Warning Message Arno Welzel <usenet@arnowelzel.de> - 2021-08-30 10:21 +0200
csiph-web