PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

php如何返回查询结果

藏色散人
藏色散人 原创
2020-10-09 10:43:49 4256浏览

php返回查询结果的方法:1、使用mysql_result函数来获取数据;2、使用mysql_fetch_row函数来获取数据,并以数组的形式返回查询结果;3、使用mysql_fetch_array函数来获取数据等等。

推荐:《PHP视频教程》 

PHP开发中四种查询返回结果分析

1. 

代码如下:

<?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. 

代码如下:

<?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. 

代码如下:

<?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.

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。