Home  >  Article  >  Backend Development  >  Analysis of four query return results in PHP development_PHP tutorial

Analysis of four query return results in PHP development_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:32:56968browse

1.

Copy code The code is as follows:

$connection=mysql_connect("localhost","root","password"); //Connect and select the database server
mysql_select_db("test",$connection) ;
$query="insert into users(user_name)"; //Insert a piece of data in the test database
$query.="values('tuxiaohui')";
$result=mysql_query($ query);
if(!$query)
echo "insert data failed!
";
else{
$query="select * from users"; //Query data
$result=mysql_query($query,$connection);
for($rows_count=0;$rows_count<7;$rows_count++) //Use mysql_result to obtain data and output, mysql_result() returns a unit in the MySQL result set content.
{
echo "User ID:".mysql_result($result,$rows_count,"user_id")."
";
echo "User name:".mysql_result($result,$ rows_count,"user_name")."
";
}
}
?>

2.
Copy code The code is as follows:

$connection=mysql_connect("localhost","root","password"); //Connect and select the database server
mysql_select_db("test",$connection);
$ query="select * from users";
$result=mysql_query($query,$connection);
while($row=mysql_fetch_row($result))
{
echo "User ID: ".$row[0]."
";
echo "Username: ".$row[1]."
";
}
?>

3.