php返回查询结果的方法:1、使用mysql_result函数来获取数据;2、使用mysql_fetch_row函数来获取数据,并以数组的形式返回查询结果;3、使用mysql_fetch_array函数来获取数据等等。
推荐:《PHP视频教程》
PHP开发中四种查询返回结果分析
1.
代码如下:
立即学习“PHP免费学习笔记(深入)”;
<?php $connection=mysql_connect("localhost","root","password"); //连接并选择数据库服务器 mysql_select_db("test",$connection); $query="insert into users(user_name)"; //在test数据库里插入一条数据 $query.="values('tuxiaohui')"; $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免费学习笔记(深入)”;
<?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免费学习笔记(深入)”;
<?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.
以上就是php如何返回查询结果的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号