Introductory example Failed to read data from table in php mysql, please give me some advice
1. Problem: I cannot read a table in mysql using php. Sometimes the page is blank, and sometimes there is a small section in php. I have been tinkering with the code for a day, please give me some guidance! Grateful! !
2. In mysql, database: test; table name: apple; (fields: firstname, lastname)
3.index.php (tried to connect to the database successfully)
Code:
<?php
$dbh = @mysql_connect("localhost","root","");
if(!$dbh){die("error");}
@mysql_select_db("test", $dbh);
$sql = "SELECT * FROM apple";
?>
<?
$rs = mysql_query($sql, $dbh);
while($row = mysql_fetch_array($rs))
echo $row[id];
echo $row[name];
echo '
';
?>
<?
@mysql_close($dbh);
?>
Please help me all heroes, thank you! !
——Solution——————–
Last of this post Edited by PhpNewnew on 2012-05-27 17:51:30
The log error is just a reminder that the default icon does not exist, it does not matter.
Read the code carefully
while($row = mysql_fetch_array($rs))
echo $row[id];
echo $row[name];
echo ‘
‘;
?>
Add upper brackets here
while($row = mysql_fetch_array($rs)) {
echo $row[id];
echo $row[name];
echo ‘
‘;
}
?>
Refresh after saving to see
In addition, you didn’t say that the database is
(Field: firstname,lastname)
How come $row[‘id’]? $row[‘name’]? came out