PHP: Handling database query data

Note:

DB_num_rows($result5) can be replaced by mysqli_num_rows($result5)

DB_fetch_array($result5) can be replaced by mysqli_fetch_assoc($result5)

1. Query single data

Code analysis

1.SQL statement

Query the part_code value when userid is equal to the variable $_SESSION['UserID'] in the query table www_users

$sql = "
		select  depart_code
		from www_users
		where userid = '" . $_SESSION['UserID'] . "'
";

2. Run SQL

Executed a database query and returned the query results.

In this function, $sql is a string containing the SQL query statement, and $db is the database connection object or database-related parameters. The specific function implementation may vary depending on your code base or framework.

$result = DB_query($sql, $db);

 3. Process the result set of the database query, row by row.

Gets a row of data from the query results and returns it as an associative array.

In this function, $result is the result set of the database query. The specific function implementation may vary depending on your code base or framework.

$row = DB_fetch_array($result)

4. Process single data 

When using a database query to obtain a single value in PHP, you usually also need to obtain it by querying an array. Even if you only need a single value, you still need to execute the query and save the result into a variable, then use an appropriate function to extract the required value from the result.

$departCode = $row['depart_code'];

Complete code

$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');
}

2. Query multiple rows of data and display it in the table

Effect

Code analysis

1.SQL statement

$sql5 = "
	select  *
	from www_users
";
$sql5 .= " order by depart_code desc";

2. Run SQL

$result5 = DB_query($sql5, $db);

3. Determine whether the query structure exists

DB_num_rows($result5) returns the number of rows in the query result set. If the number of rows is less than or equal to 0, it means there is no data, otherwise there is

DB_num_rows($result5) > 0

4. Write html to the table and loop the data queried by SQL

  • $row = DB_fetch_array($result5): Assign the variable $row to the value of DB_fetch_array($result5)
  • DB_fetch_array($result5): Process the result set of the database query, row by row.
  • $row['depart_code']: Each row outputs a part_code data
<?php
while ($row = DB_fetch_array($result5)) {
?>
    <tr>
        <td><?php echo $row['userid']; ?></td>
        <td><?php echo $row['depart_code']; ?></td>
    </tr>
<?php
}
?>

Complete code

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

Guess you like

Origin blog.csdn.net/weixin_46001736/article/details/134682257