• Help With Warning Message

    From Call Me Tom@21:1/5 to All on Sun Aug 22 20:00:04 2021
    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?

    Tom

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to Call Me Tom on Mon Aug 23 01:50:29 2021
    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
    --
    Lew Pitcher
    "In Skills, We Trust"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Call Me Tom@21:1/5 to lew.pitcher@digitalfreehold.ca on Mon Aug 23 01:03:04 2021
    $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?
    Tom

    On Mon, 23 Aug 2021 01:50:29 -0000 (UTC), Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote:

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From J.O. Aho@21:1/5 to Call Me Tom on Mon Aug 23 09:13:04 2021
    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.


    --

    //Aho

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to Call Me Tom on Mon Aug 23 11:47:05 2021
    On Mon, 23 Aug 2021 01:03:04 -0400, 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?

    [snip

    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.

    --
    Lew Pitcher
    "In Skills, We Trust"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to J.O. Aho on Mon Aug 23 18:19:08 2021
    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.




    --
    Lew Pitcher
    "In Skills, We Trust"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Call Me Tom@21:1/5 to lew.pitcher@digitalfreehold.ca on Mon Aug 23 15:26:58 2021
    Thank yopu for your help. I found the problem
    Tom.



    On Mon, 23 Aug 2021 18:19:08 -0000 (UTC), Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote:

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From J.O. Aho@21:1/5 to Lew Pitcher on Tue Aug 24 00:33:43 2021
    On 23/08/2021 20.19, Lew Pitcher wrote:

    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'];
    }

    Make the code even better (and more readable), we should skip the else-part

    $result4 = $dbh->query($query4);
    if ($result4 === FALSE)
    {
    throw new Exception("Query failed: " + $dbh->errorInfo()[2]);
    }

    $da = $result4->FETCH(PDO::FETCH_ASSOC);
    if ($da === FALSE)
    {
    throw new Exception("Fetch failed.");
    }

    $da_name = $da['airport_name'];


    Of course this requires you take care of the exception higher up in the
    code and give a nice general error message, log the exception.

    --

    //Aho

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Arno Welzel@21:1/5 to All on Mon Aug 30 10:21:12 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)