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>
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: