Get the total number of records in the data table
<?php require 'linkConfig.php';//根据连接信息连接数据库 $sql = "select count(*) from druserlist where C04='普通用户'";//查询数据表 $result = $mysqli->query($sql);//以索引的方式返回一个结果集 list($rowCount)=$result->fetch_row(); echo '记录总数:'.$rowCount; $result->close(); $mysqli->close(); ?>
It can also be obtained like this:
<?php require 'linkConfig.php';//根据连接信息连接数据库 $sql = "select count(*) from druserlist where C04='普通用户'";//查询数据表 $result = $mysqli->query($sql);//以索引的方式返回一个结果集 $row = $result->fetch_row(); echo '记录总数:'.$row[0]; $result->close(); $mysqli->close(); ?>
Other forms: ($row['total'] replaced by $ row[0] can also be used)
<?php require 'linkConfig.php';//根据连接信息连接数据库 $sql = "select count(*) as total from druserlist where C04='普通用户'";//查询数据表 $result = $mysqli->query($sql);//以索引的方式返回一个结果集 $row = $result->fetch_array(MYSQLI_BOTH); echo '记录总数:'.$row['total']; $result->close(); $mysqli->close(); ?>
I checked the PHP operation manual and found that there are too many similar functions, which means that PHP has many processing methods for the same needs.
Get the records of the database query table and return it in JSON format
<?php $sql = "select * from druserlist";//查询语句 $res = $mysqli->query($sql);//执行查询 $result = array();//准备一个空数组 while ($row = $res->fetch_assoc()){ //对结果集进行逐行取值并压入到数组中 array_push($result,$row); } echo json_encode($result);//返回前端JSON格式数据 ?>
Returned data:
[ { "C01": "00924001", "C02": "经理办01", "C03": "1", "C04": "普通用户", "C05": "1" }, { "C01": "00924002", "C02": "经理办02", "C03": "2", "C04": "普通用户", "C05": "1" }, { "C01": "00923128", "C02": "人事科01", "C03": "1", "C04": "普通用户", "C05": "人事科的张小勇" } ]
Get the paging record data of the data table and return it in JSON format (Take LayUI's paging data as an example)
<?php $page = isset($_POST['page']) ? intval($_POST['page']) : 1;//获取页数 $limit = isset($_POST['limit']) ? intval($_POST['limit']) : 10;//获取每页的显示记录数 $offset = ($page-1)*$limit;//计算起始位置 require 'linkConfig.php';//连接数据库 $sql1 = "select count(*) from druserlist"; $result1 = $mysqli->query($sql1); $rowCount = $result1->fetch_row(); $returnArr['code']=0; $returnArr['msg']=""; $returnArr['count']=$rowCount[0];//总记录数 $sql2 = "select * from druserlist order by C01 limit $offset,$limit"; $res = $mysqli->query($sql2); $result = array(); while ($row = $res->fetch_assoc()){ array_push($result,$row); } $returnArr['data']=$result; echo json_encode($returnArr);//返回JSON格式数据 $res->free(); $mysqli->close(); ?>
Returned data:
{ "code": 0, "msg": "", "count": "12", "data": [ { "C01": "00101078", "C02": "HaoR", "C03": "2", "C04": "管理员", "C05": "1" }, { "C01": "00323007", "C02": "研究中心01", "C03": "1", "C04": "管理员", "C05": "1" }, { "C01": "00616001", "C02": "财务科01", "C03": "1", "C04": "管理员", "C05": "1" }, { "C01": "00616002", "C02": "财务科02", "C03": "1", "C04": "管理员", "C05": "1" }, { "C01": "00616003", "C02": "财务科03", "C03": "1", "C04": "管理员", "C05": "1" }, { "C01": "00923127", "C02": "admin", "C03": "1", "C04": "管理员", "C05": "系统管理员" }, { "C01": "00923128", "C02": "人事科01", "C03": "1", "C04": "普通用户", "C05": "人事科的张小勇" }, { "C01": "00923129", "C02": "人事科02", "C03": "1", "C04": "管理员", "C05": "1" } ] }
can return different data styles according to the front-end paging requirements. Once you know the PHP processing, it will be easier to handle. .
PHP’s processing of mysql database query result set.
⑴ fetch_array()
<?php require 'linkConfig.php';//根据连接信息连接数据库 $sql = "select C01,C02 from druserlist where C04='普通用户'";//查询数据表 $result=$mysqli->query($sql);//以索引的方式返回一个结果集 while($row = $result->fetch_array()){ $rows[] = $row; } foreach($rows as $row){ echo $row['C01'].' '.$row['C02'].'<br>'; } $result->close(); $mysqli->close(); ?>
Results returned line by line:
00924001 Manager Office 01
00924002 Manager Office 02
00923128 Personnel Department 01
Return JSON format:
<?php require 'linkConfig.php';//根据连接信息连接数据库 $sql = "select C01,C02 from druserlist where C04='普通用户'";//查询数据表 $result=$mysqli->query($sql);//以索引的方式返回一个结果集 while($row = $result->fetch_array()){ $rows[] = $row; } echo json_encode($rows);//返回JSON格式数据 $result->close(); $mysqli->close(); ?>
Returned results:
[ { "0": "00924001", "1": "经理办01", "C01": "00924001", "C02": "经理办01" }, { "0": "00924002", "1": "经理办02", "C01": "00924002", "C02": "经理办02" }, { "0": "00923128", "1": "人事科01", "C01": "00923128", "C02": "人事科01" } ]
It can be seen from the above output that the output is in the form of numbers and field names. According to the PHP operation manual, you can get the record value by giving the parameters of the fetch_array() function, that is, the following three outputs are the same.
<?php require 'linkConfig.php';//根据连接信息连接数据库 $sql = "select C01,C02 from druserlist where C04='普通用户'";//查询数据表 $result=$mysqli->query($sql);//以索引的方式返回一个结果集 //第一种输出 while($row = mysqli_fetch_array($result, MYSQLI_NUM)){ echo $row[0].' '.$row[1].'<br>'; } //第二种输出 while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){ echo $row['C01'].' '.$row['C02'].'<br>'; } //第三种输出 while($row = mysqli_fetch_array($result, MYSQLI_BOTH)){ echo $row[0].' '.$row['C02'].'<br>'; } $result->close(); $mysqli->close(); ?>
The three outputs are the following results:
00924001 Manager Office 01
00924002 Manager Office 02
00923128 Personnel Department 01
⑵ fetch_row()
The above output can be performed using the following method:
<?php require 'linkConfig.php';//根据连接信息连接数据库 $sql = "select C01,C02 from druserlist where C04='普通用户'";//查询数据表 $result=$mysqli->query($sql);//以索引的方式返回一个结果集 while ($row = $result->fetch_row()) { printf ("%s %s <br>", $row[0], $row[1]); } $result->close(); $mysqli->close(); ?>
⑶fetch_assoc()
The above output can be performed using the following method :
<?php require 'linkConfig.php';//根据连接信息连接数据库 $sql = "select C01,C02 from druserlist where C04='普通用户'";//查询数据表 $result=$mysqli->query($sql);//以索引的方式返回一个结果集 while ($row = $result->fetch_assoc()) { printf ("%s %s <br>", $row["C01"], $row["C02"]); } $result->close(); $mysqli->close(); ?>
⑷ fetch_all()
Similar to fetch_array, there are parameter selections, namely: MYSQLI_ASSOC, MYSQLI_NUM or MYSQLI_BOTH. The default is MYSQLI_BOTH.
<?php require 'linkConfig.php';//根据连接信息连接数据库 $sql = "select C01,C02 from druserlist where C04='普通用户'";//查询数据表 $result=$mysqli->query($sql);//以索引的方式返回一个结果集 $rows=$result->fetch_all(MYSQLI_NUM); $r=0; while ( $r < mysqli_num_rows($result) ) { printf ("%s %s <br>", $rows[$r][0], $rows[$r][1]); $r++; } $result->close(); $mysqli->close(); ?>
<?php require 'linkConfig.php';//根据连接信息连接数据库 $sql = "select C01,C02 from druserlist where C04='普通用户'";//查询数据表 $result=$mysqli->query($sql);//以索引的方式返回一个结果集 $rows=$result->fetch_all(MYSQLI_ASSOC); $r=0; while ( $r < mysqli_num_rows($result) ) { printf ("%s %s <br>", $rows[$r]['C01'], $rows[$r]['C02']); $r++; } $result->close(); $mysqli->close(); ?>
<?php require 'linkConfig.php';//根据连接信息连接数据库 $sql = "select C01,C02 from druserlist where C04='普通用户'";//查询数据表 $result=$mysqli->query($sql);//以索引的方式返回一个结果集 $rows=$result->fetch_all(MYSQLI_BOTH); $r=0; while ( $r < mysqli_num_rows($result) ) { printf ("%s %s <br>", $rows[$r][0], $rows[$r]['C02']); $r++; } $result->close(); $mysqli->close(); ?>
The output of the above three pieces of code is the same.
The above is the detailed content of How to obtain record data from MySQL database in PHP. For more information, please follow other related articles on the PHP Chinese website!

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

The relationship between SQL and MySQL is the relationship between standard languages and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor