#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    346
    Rep Power
    1

    Question Why Variables Do Not Echo After Myqli_stmt_fetch ?


    This is really starting to get on my nerves now. 4hrs wasted trying to fix it!
    Why is not the first & surname not getting echoed ?

    I'm working on the User/Member's account homepage where when he logs-in he finds a welcome message or greeting by his first & surname.


    GREETING ECHO ISSUE

    Greeting should be echoed like this:
    Welcome First Name Surname


    For some reason the $db_first_name and $db_surname variables are not getting echoed.
    I did fetch them from db.

    I tried all these but no luck! On some, I used the php closing tag where html is about to begin.

    1.
    PHP Code:
    echo "Welcome <h2><b>\"$db_first_name\" \"$db_surname\"</b></h2>"?><br> 
    Outputs only this:
    Welcome
    "" ""



    2.
    PHP Code:
    echo "Welcome <h2><b>\"$db_first_name\" \"$db_surname\""?></b></h2><br> 
    Outputs only this:
    Welcome
    "" ""


    3.
    PHP Code:
    echo "Welcome <h2><b> $db_first_name $db_surname"?></b></h2><br> 
    Outputs only this:
    Welcome


    4.
    PHP Code:
    echo "Welcome ?><h2><b><?php \"$db_first_name\" \"$db_surname\""?></b></h2><br>
    Outputs only this:
    Welcome ?>


    Full Code of the homepage coded so far ...
    PHP Code:
    <?php

    /*
    ERROR HANDLING
    */
    declare(strict_types=1);
    ini_set('display_errors''1');
    ini_set('display_startup_errors''1');
    error_reporting(E_ALL);
    mysqli_report(MYSQLI_REPORT_ERROR MYSQLI_REPORT_STRICT);

    include 
    'config.php';

    //Check if user is already logged-in or not.
    if (is_logged() === false)    
    {
        
    //Redirect User to Log-in Page after 2 secs.
        
    header("refresh:2;url=login_editing.php");
        exit;
    }
    else
    {
        
    $user $_SESSION["user"];
        echo 
    "<a href='user.php?user=$user'>$user</a>"?><br>
        
        <?php 
        
    //Grab User details from DB.
        
    $query "SELECT ids, usernames, passwords, emails, first_names, surnames FROM users WHERE usernames = ?";
        
    $stmt mysqli_prepare($conn$query);
        
    mysqli_stmt_bind_param($stmt's'$user);
        
    mysqli_stmt_execute($stmt);
        
    $result mysqli_stmt_bind_result($stmt$db_id$db_username$db_password$db_email$db_first_name$db_surname);
        
    mysqli_stmt_fetch($stmt);
        if (!
    $stmt)
        {
            echo 
    "Sorry! Our system is currently experiencing a problem gathering your account details!";
            exit();
        }
        else
        {
            echo 
    "$db_id";
            echo 
    "$db_username";
            echo 
    "$db_password";
            echo 
    "$db_email";
            echo 
    "$db_first_name";
            echo 
    "$db_surname";
            
    var_dump($stmt);
        }
        
        
    ?>
        
        <!DOCTYPE html>
        <html>
        <head>
        <title>Member <?php echo $user ?> Home Page</title>
            <meta charset="utf-8">
        </head>
        <body>
        <body background="background.png">
        <h2>Member <?php echo $user ?> Home Page</h2>
        <?php
        
    //Welcome User by name. Display Log-out Link.
        
    echo "Welcome <h2><b> $db_first_name $db_surname"?></b></h2><br>
            
        <?php echo "$db_first_name"?>
        
        
        <?php echo "|<a href='logout.php'>$user Log Out</a>|";?><br>
        <?php

    }
        
        
    ?>
        </body>
        </html>
    Last edited by UniqueIdeaMan; October 16th, 2017 at 06:25 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,373
    Rep Power
    631
    How can you possibly have worked with PHP for so long and learned absolutely nothing? It *IS* echoing exactly what those variables contain. Get it?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    105
    Rep Power
    65
    You have a variable naming problem in your code and the sql query isn't matching any row. Unfortunately, php isn't helping you find this problem by producing an undefined variable error, because php's variable referencing doesn't produce an error if the variable being referenced doesn't exist. I recommend that you proof-read your code, looking for variables that you have changed the name of, from where you have assigned a value to the variable to where you are referencing that variable.

    You should also ALWAYS test if a query matched any row(s) before/at the same time, you fetch the data. If you had logic testing if the query match a row or not, you would at least know it didn't and wouldn't be expecting variables to have values in them.

    Lastly, if you had switched to the php PDO extension, the most common and simplest way of using it WOULD have produced a php error about the variable naming problem that would have helped you find why the query isn't matching a row.

    Comments on this post

    • UniqueIdeaMan agrees
  6. #4
  7. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,815
    Rep Power
    4554
    <?php $user ?>

    lol... What is this supposed to do? How many months have we been at this and you're still confused on how to echo a variable?

    Comments on this post

    • UniqueIdeaMan agrees
    -- Cigars, whiskey and wild, wild women. --
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    346
    Rep Power
    1
    Originally Posted by Sepodati
    <?php $user ?>

    lol... What is this supposed to do? How many months have we been at this and you're still confused on how to echo a variable?
    Thanks for pointing that typo out. I added the echo and it is working. I fixed my original post with my current code update.
    But, the other problem remains. I still don't get the $db_first_name and $db_surname echoed.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    346
    Rep Power
    1
    Originally Posted by gw1500se
    How can you possibly have worked with PHP for so long and learned absolutely nothing? It *IS* echoing exactly what those variables contain. Get it?
    I thought you were saying "Get it ?" angrily just to be mean. But, after reading DSmabismad, I realize you're saying that, it is echoing nothing because the variables are hold nothing.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    346
    Rep Power
    1
    Originally Posted by DSmabismad
    You have a variable naming problem in your code and the sql query isn't matching any row. Unfortunately, php isn't helping you find this problem by producing an undefined variable error, because php's variable referencing doesn't produce an error if the variable being referenced doesn't exist. I recommend that you proof-read your code, looking for variables that you have changed the name of, from where you have assigned a value to the variable to where you are referencing that variable.

    You should also ALWAYS test if a query matched any row(s) before/at the same time, you fetch the data. If you had logic testing if the query match a row or not, you would at least know it didn't and wouldn't be expecting variables to have values in them.

    Lastly, if you had switched to the php PDO extension, the most common and simplest way of using it WOULD have produced a php error about the variable naming problem that would have helped you find why the query isn't matching a row.
    No. I do not have variable naming or db column naming problem. I checked nearly a hundred times now.
    Also, look at my error reporting code. Variable name typos usually result in "undefined variable" error. Not getting any this time round.
    Column name typos also usually show some error. Not getting any this time round, either.
    However, I did do a foolish thing and that is my "first_names" and "surnames" column had no data. Filled them in now.
    And so, the variables $db_first name and db_surname should show data being echoed. But they still don't!
    I also added the condition like you suggested. I get no echo saying $result is holding blank data.

    PHP Code:
    //Grab User details from DB.
        
    $query "SELECT ids, usernames, passwords, emails, first_names, surnames FROM users WHERE usernames = ?";
        
    $stmt mysqli_prepare($conn$query);
        
    mysqli_stmt_bind_param($stmt's'$username);
        
    mysqli_stmt_execute($stmt);
        
    //mysqli_stmt_get_result($stmt); // Use this line or next ?
        
    $result mysqli_stmt_bind_result($stmt$db_id$db_username$db_password$db_email$db_first_name$db_surname); // Use this line or previous ?
        
    mysqli_stmt_fetch($stmt);
        if (!
    $result)
        {
            echo 
    "Sorry! Our system is currently experiencing a problem gathering your account details!";
            exit();
        }
        else
        {
            echo 
    "$db_id";
            echo 
    "$db_username";
            echo 
    "$db_password";
            echo 
    "$db_email";
            echo 
    "$db_first_name";
            echo 
    "$db_surname";
        } 
    Now, this is slowly becoming a mystery to why none of these are being echoed:

    PHP Code:
    echo "$db_id";
    echo 
    "$db_username";
    echo 
    "$db_password";
    echo 
    "$db_email";
    echo 
    "$db_first_name";
    echo 
    "$db_surname"
    Last edited by UniqueIdeaMan; October 15th, 2017 at 09:24 PM.
  14. #8
  15. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,126
    Rep Power
    4103
    Originally Posted by UniqueIdeaMan
    No. I do not have variable naming or db column naming problem. I checked nearly a hundred times now.
    Yes, you do. Make your 101st check. Here's a hint:
    Code:
        mysqli_stmt_bind_param($stmt, 's', $username);

    Originally Posted by UniqueIdeaMan
    Also, look at my error reporting code. Variable name typos usually result in "undefined variable" error. Not getting any this time round.
    You're not getting an error because you're using the undefined variable as a reference. Creating a reference from an undefined variable doesn't emit a notice because it's considered a valid way to define the variable.


    Originally Posted by UniqueIdeaMan
    I also added the condition like you suggested. I get no echo saying $result is holding blank data.

    You're checking the wrong return value. The return value of mysqli_stmt_fetch is what tells you if any rows were fetched or not.

    Comments on this post

    • UniqueIdeaMan agrees
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    346
    Rep Power
    1
    Originally Posted by kicken
    Yes, you do. Make your 101st check. Here's a hint:
    Code:
        mysqli_stmt_bind_param($stmt, 's', $username);


    You're not getting an error because you're using the undefined variable as a reference. Creating a reference from an undefined variable doesn't emit a notice because it's considered a valid way to define the variable.


    You're checking the wrong return value. The return value of mysqli_stmt_fetch is what tells you if any rows were fetched or not.
    Thanks for the hint Kicken!
    I fixed the variable name now!:

    PHP Code:
    mysqli_stmt_bind_param($stmt's'$user); 
    Originally, I coded like this as tutorials usually showed to check if the $stmt is fetching or not.
    PHP Code:
    if (!$stmt
    But, last night, when I saw I was getting no indication whether the rows been fetched or not, I changed it to:

    PHP Code:
    if (!$result
    Anyway, reverting it back to the following is showing the variables getting echoed apart from:

    echo "$db_first_name";
    echo "$db_surname";


    Them 2 are still not getting echoed. The rest are.
    Here's the updated code:

    PHP Code:
        if (!$stmt)
        {
            echo "Sorry! Our system is currently experiencing a problem gathering your account details!";
            exit();
        }
        else
        {
            echo "$db_id";?> <br> <?php 
            
    echo "$db_username";?> <br> <?php 
            
    echo "$db_password";?> <br> <?php 
            
    echo "$db_email";?> <br> <?php 
            
    echo "$db_first_name";?> <br> <?php 
            
    echo "$db_surname";?> <br> <?php 
            var_dump
    ($stmt);
        }
    I get this echoed:

    1
    username
    $2y$10$VGS5ONJ7L7WwGaHSkWo74eAS6MVxFSvkrNltxALZgorF7DsGTUoz6
    EDITED@EDITED.com

    object(mysqli_stmt)#2 (10) { ["affected_rows"]=> int(-1) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(1) ["field_count"]=> int(6) ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["sqlstate"]=> string(5) "00000" ["id"]=> int(1) }


    NOTE: These 2 did not get echoed:
    echo "$db_first_name";?> <br> <?php
    echo "$db_surname";?> <br>


    column_names.png

    As you can see from the attached img, the column names are spelt right.

    PHP Code:
    <?php 
        
    //Grab User details from DB.
        
    $query "SELECT ids, usernames, passwords, emails, first_names, surnames FROM users WHERE usernames = ?";
        
    $stmt mysqli_prepare($conn$query);
        
    mysqli_stmt_bind_param($stmt's'$user);
        
    mysqli_stmt_execute($stmt);
        
    $result mysqli_stmt_bind_result($stmt$db_id$db_username$db_password$db_email$db_first_name$db_surname); 
        
    mysqli_stmt_fetch($stmt);
    As for having the names plural. Well, a column has many first names and so why name it as single ? Ok, when data is being fetched a single row (single first name) gets fetched. But the labeling is for the column. Not the row. And my reasoning was that, if the column contains more than one name then let's label the column "first_names" (plural).

    I placed my updated full code on my original post again.
    Last edited by UniqueIdeaMan; October 16th, 2017 at 06:24 AM.
  18. #10
  19. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,815
    Rep Power
    4554
    Run this query directly in MySQL and post the results:

    SELECT ids, usernames, passwords, emails, first_names, surnames FROM users WHERE usernames = 1
    -- Cigars, whiskey and wild, wild women. --
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    346
    Rep Power
    1
    Originally Posted by Sepodati
    Run this query directly in MySQL and post the results:

    SELECT ids, usernames, passwords, emails, first_names, surnames FROM users WHERE usernames = 1
    Sorry, old man! I'm just reading your reply after 10 days. I can't remember if this issue has been sorted or not as I moved-on top other stuffs. But, I will get back to this topic one day and see whether the problem was sorted or not. I think it was.

IMN logo majestic logo threadwatch logo seochat tools logo