PHP:处理数据库查询数据

注:

DB_num_rows(result5)可以替换为mysqli_num_rows(result5)

DB_fetch_array(result5)可以替换为mysqli_fetch_assoc(result5)

一、查询单个数据

代码解析

1、SQL语句

查询表www_users中当userid等于变量$_SESSION['UserID']时的depart_code值

php 复制代码
$sql = "
		select  depart_code
		from www_users
		where userid = '" . $_SESSION['UserID'] . "'
";

2、 运行SQL

执行了一个数据库查询并返回查询结果。

在这个函数中,$sql 是包含 SQL 查询语句的字符串,$db 是数据库连接对象或者与数据库相关的参数。具体的函数实现可能会根据你的代码库或框架而有所不同。

php 复制代码
$result = DB_query($sql, $db);

3、处理数据库查询的结果集,一行一行处理

从查询结果中获取一行数据,并以关联数组的形式返回。

在这个函数中,$result 是数据库查询的结果集。具体的函数实现可能会根据你的代码库或框架而有所不同。

php 复制代码
$row = DB_fetch_array($result)

4、处理单个数据

在 PHP 中使用数据库查询获取单个值时,通常也需要通过查询数组的方式来获取。即使只需要一个单独的值,你仍然需要执行查询并将结果保存到变量中,然后使用适当的函数从结果中提取所需的值。

php 复制代码
$departCode = $row['depart_code'];

完整代码

php 复制代码
$sql = "
		select  depart_code
		from www_users
		where userid = '" . $_SESSION['UserID'] . "'
	";
$result = DB_query($sql, $db);
if ($row = DB_fetch_array($result)) {
	$departCode = $row['depart_code'];
	echo "<script>alert('" . $departCode . "')</script>";
} else {
	prnMsg(_('找不到相应数据!'), 'error');
}

二、查询多行数据并展示到表格

效果

代码解析

1、SQL语句

php 复制代码
$sql5 = "
	select  *
	from www_users
";
$sql5 .= " order by depart_code desc";

2、运行SQL

php 复制代码
$result5 = DB_query($sql5, $db);

3、判断查询结构是否存在

DB_num_rows($result5)返回查询结果集中的行数。如果行数小于等于0那么表示没有数据,反之则有

php 复制代码
DB_num_rows($result5) > 0

4、html写入表格,对SQL查询的数据进行循环

  • row = DB_fetch_array(result5):给变量row赋值为DB_fetch_array(result5)的值
  • DB_fetch_array($result5):处理数据库查询的结果集,一行一行处理
  • $row['depart_code']:每行都输出一个depart_code的数据
php 复制代码
<?php
while ($row = DB_fetch_array($result5)) {
?>
    <tr>
        <td><?php echo $row['userid']; ?></td>
        <td><?php echo $row['depart_code']; ?></td>
    </tr>
<?php
}
?>

完整代码

html 复制代码
<?php
$sql5 = "
	select  *
	from www_users
";
$sql5 .= " order by depart_code desc";
$result5 = DB_query($sql5, $db);
//检查查询结构是否为空
if (DB_num_rows($result5) > 0) {
?>
        <style>
                table {
                        border-collapse: collapse;
                }

                td {
                        text-align: center;
                }
        </style>
        <table border="1">
                <tr>
                        <th>userid</th>
                        <th>depart_code</th>
                </tr>
                <?php
                while ($row = DB_fetch_array($result5)) {
                ?>
                        <tr>
                                <td><?php echo $row['userid']; ?></td>
                                <td><?php echo $row['depart_code']; ?></td>
                        </tr>
        <?php
                }
        }
        ?>
        </table>
相关推荐
emo了小猫2 小时前
Mybatis #{} 和 ${}区别,使用场景,LIKE模糊查询避免SQL注入
数据库·sql·mysql·mybatis
潘yi.7 小时前
NoSQL之Redis配置与优化
数据库·redis·nosql
zdkdchao7 小时前
hbase资源和数据权限控制
大数据·数据库·hbase
伤不起bb7 小时前
NoSQL 之 Redis 配置与优化
linux·运维·数据库·redis·nosql
leo__5207 小时前
PostgreSQL配置文件修改及启用方法
数据库·postgresql
Yungoal9 小时前
php & apache构建 Web 服务器
服务器·php·apache
南風_入弦9 小时前
优化09-表连接
数据库·oracle
浩浩测试一下9 小时前
Authpf(OpenBSD)认证防火墙到ssh连接到SSH端口转发技术栈 与渗透网络安全的关联 (RED Team Technique )
网络·网络协议·tcp/ip·安全·网络安全·php
Snk0xHeart10 小时前
极客大挑战 2019 EasySQL 1(万能账号密码,SQL注入,HackBar)
数据库·sql·网络安全
孤寂大仙v10 小时前
【计算机网络】网络层IP协议与子网划分详解:从主机通信到网络设计的底层逻辑
tcp/ip·计算机网络·php