Home > Computer Tutorials > Computer Knowledge > Write a program in PHP to connect to the database and output all records of the table

Write a program in PHP to connect to the database and output all records of the table

王林
Release: 2024-01-22 11:19:22
forward
1076 people have browsed it

Write a program in PHP to connect to the database and output all records of the table

PHP writing program can connect to the database and output all the records in the table to set the data

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 '

';

while($row=mysql_fetch_array($result))

{

echo '

';

foreach($row as $rw){

echo "

";

}

echo '

';

}

echo '

".$rw."
';

?>

How does native php output the last executed sql of mysql

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!

source:docexcel.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template