Home > Backend Development > PHP Problem > How to return query results in php

How to return query results in php

藏色散人
Release: 2023-03-06 15:04:01
Original
4584 people have browsed it

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.

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>"; 
} 
} 
?>
Copy after login

2.

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>"; 
} 
?>
Copy after login

3.

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"] 
} 
?>
Copy after login

4.

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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template