Please i need some help php SELECT FROM WHERE

902

My problem is when i want to display someone's information from database using his Username:

i get this error this is Notice: Undefined variable: fname in /opt/lampp/htdocs/aa/hhh.php on line 27 . .

<?php

include 'con_to_db.php';

$result = mysqli_query($dbcon, "SELECT * FROM Members WHERE Username='youba'" );

while($row = mysqli_fetch_array($result))
  {

$fname = $row['Username'];
$age = $row['Age'];
}
?>
<html>
<body>
<h1> this is <?php echo $fname?> </h1>
</body>
</html>

but when i use the id i get the information from database without any problems:

<?php

    include 'con_to_db.php';

    $result = mysqli_query($dbcon, "SELECT * FROM Members WHERE id='70'" );

    while($row = mysqli_fetch_array($result))
      {

    $fname = $row['Username'];
    $age = $row['Age'];
    }
    ?>
    <html>
    <body>
    <h1> this is <?php echo $fname?> </h1>
    </body>
    </html>
345

Answer

Solution:

First of all, it's not an error but a notice. However, this particular notice does hint at an error in the code, though not a syntax error, but a logical one: the body of your while loop does not get executed - i.e. you forgot to check if you get any result in the first place. (Also, if you would get more than one result, only the last one would be displayed).

Please check your database to see why the username is not there, though you obviously expect it to be.

Also, I assume (hope!) that your id field is numeric in the database, in which case you should not write quotes:

$result = mysqli_query($dbcon, "SELECT * FROM Members WHERE id=70" );

People are also looking for solutions to the problem: PHP multiple-inheritance

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.