I want to fetch data from a mysql table using php. Please, can anyone tell me what is wrong with this code? What is the correct code to fetch data from a mysql database:
Variables in php are case sensitive. Please replace your while loop with following:
while ($rows = mysql_fetch_array($query)):
$name = $rows['Name'];
$address = $rows['Address'];
$email = $rows['Email'];
$subject = $rows['Subject'];
$comment = $rows['Comment']
echo "$name<br>$address<br>$email<br>$subject<br>$comment<br><br>";
endwhile;
Select a database with identifier mysql_select_db("form1",$connect);
Are you getting syntax error? If please put a ; next to $comment = $rows['Comment'].
Also the variables should be case sensitive here
<table border="1px">
<tr>
<th>Student Name</th>
<th>Email</th>
<th>password</th>
</tr>
<?php
If(mysql_num_rows($result)>0)
{
while($rows=mysql_fetch_array($result))
{
?>
<?php echo "<tr>";?>
<td><?php echo $rows['userName'];?> </td>
<td><?php echo $rows['email'];?></td>
<td><?php echo $rows['password'];?></td>
<?php echo "</tr>";?>
<?php
}
}
?>
</table>
<?php
}
?>
Try
$query = mysql_query("SELECT * FROM users WHERE name = 'Admin' ")or die(mysql_error());
and check if this throw any error.
Then use while($rows = mysql_fetch_assoc($query)):
And finally display it as
echo $name . "<br/>" . $address . "<br/>" . $email . "<br/>" . $subject . "<br/>" . $comment . "<br/><br/>" . ;
Do not user mysql_*
as its deprecated.
Code:
while ($rows = mysql_fetch_array($query)):
$name = $rows['Name'];
$address = $rows['Address'];
$email = $rows['Email'];
$subject = $rows['Subject'];
$comment = $rows['Comment']
echo "$name<br>$address<br>$email<br>$subject<br>$comment<br><br>";
endwhile;
Your syntax is wrong... The correct coding is:
<?php
mysql_connect("localhost","root","");
mysql_select_db("form1");
$query = mysql_query("SELECT * FROM users WHERE name = 'Admin' ");
while($rows = mysql_fetch_array($query))
{
$rows = $rows['Name'];
$address = $rows['Address'];
$email = $rows['Email'];
$subject = $rows['Subject'];
$comment = $rows['Comment']
echo $rows.'</br>'.$address.'</br>'.$email.'</br>'.$subject.'</br>'.$comment;
}
?>