Home  >  Article  >  Backend Development  >  How to return query results in php

How to return query results in php

藏色散人
藏色散人Original
2020-10-09 10:43:494515browse

php method to return query results: 1. Use the mysql_result function to obtain data; 2. Use the mysql_fetch_row function to obtain data and return the query results in the form of an array; 3. Use the mysql_fetch_array function to obtain data, etc. .

How to return query results in php

Recommended: "PHP Video Tutorial"

Analysis of four types of query return results in PHP development

1.f86f76643ee9dec682387804d5c33a2c

The code is as follows:

<?php 
$connection=mysql_connect("localhost","root","password"); //连接并选择数据库服务器 
mysql_select_db("test",$connection); 
$query="insert into users(user_name)"; //在test数据库里插入一条数据 
$query.="values(&#39;tuxiaohui&#39;)"; 
$result=mysql_query($query); 
if(!$query) 
echo "insert data failed!<br>"; 
else{ 
$query="select * from users"; //查询数据 
$result=mysql_query($query,$connection); 
for($rows_count=0;$rows_count<7;$rows_count++) //用mysql_result获得数据并输出,mysql_result() 返回 MySQL 结果集中一个单元的内容。 
{ 
echo "用户ID:".mysql_result($result,$rows_count,"user_id")."<br>"; 
echo "用户名:".mysql_result($result,$rows_count,"user_name")."<br>"; 
} 
} 
?>

2.893fdb92948e62ebdc792d3cfafdfe70

The code is as follows:

<?php 
$connection=mysql_connect("localhost","root","password"); //连接并选择数据库服务器 
mysql_select_db("test",$connection); 
$query="select * from users"; 
$result=mysql_query($query,$connection); 
while($row=mysql_fetch_row($result)) 
{ 
echo "用户ID:".$row[0]."<br>"; 
echo "用户名:".$row[1]."<br>"; 
} 
?>

3.48d59c25e6dec69c8e6408d79c940a9d

The code is as follows:

<?php 
$connection=mysql_connect("localhost","root","password"); //连接并选择数据库服务器 
mysql_select_db("test",$connection); 
$query="select * from users"; 
$result=mysql_query($query,$connection); 
while($row=mysql_fetch_array($result)) 
{ 
echo "用户ID:".$row[0]."<br>"; //也可以写做$row["user_id"] 
echo "用户名:".$row[1]."<br>"; //也可以写做$row["user_name"] 
} 
?>

4.