mysql_connect('localhost','root','') or die("Connection error"); //Link database
mysql_select_db('database') or die("Fail to select database.");//Select database
$sql="select * from `mytable`";
$result=mysql_query($sql);
echo '
".$rw." | ";
?>
method one:
$link = mysql_connect("192.168.1.1", "myuser", "mypass"); --Connect to the database
if(FALSE == $link)
{
echo "Connect database failed!";
}
$query =
"select * from a order by id";
mysql_select_db('test', $link);--Select database
$result = mysql_query($query);--Execute query statement
if(FALSE == $result)
{
echo "Querry failed!";
}
$i = 0;
$j = 0;
while($i {
$meta_c=0;
if($meta_c=mysql_fetch_row($result))--Get the result set of each row
{
while($j {
echo $meta_c[$j;
}
echo "
";
} // while;
$j=0;
}
mysql_free_result($result);--Release the result set
mysql_close($link);--Close the connection
?>
Method Two:
$link = mysql_connect("192.168.1.1", "myuser", "mypass"); --Connect to the database
if(FALSE == $link)
{
echo "Connect database failed!
";
}
mysql_select_db('test', $link);--Select database
$result = mysql_query("select * from a order by id");--Execute the query statement
if(FALSE == $result)
{
echo "Querry failed!";
}
while($row = mysql_fetch_array($result, MYSQL_NUM))--Get the value in the array, because the result set is stored in a two-dimensional array
{
foreach ($row as $col_value)--take values one by one
{
echo " $col_value ";
}
}
mysql_free_result($result);--Release the result set
mysql_close($link);)--Close the connection
?>
The above is the detailed content of Write a program in PHP to connect to the database and output all records of the table. For more information, please follow other related articles on the PHP Chinese website!