PHP development and implementation of download count statistics function module (1)
First put the database connection code into the conn.php file It is convenient to use require 'conn.php' call.
The require statement is used to insert useful code written in other files into the execution flow
The following is the database file conn.php
<?php header("Content-type:text/html;charset=utf-8"); $link = mysqli_connect('localhost','username','password','test'); mysqli_set_charset($link, "utf8"); if(!$link) { die("连接失败:".mysqli_connect_error()); } header("Content-Type: text/html; charset=utf-8"); ?>
Create one that is filelist.php, used to read the data in the mysql data table, and output the data in JSON format to call the front-end html page
First use the require statement to call the conn.php database file, and pass the SQL statement SELECT reads out all the data in the database downloads table
Save the id, filename and other while loop output in the data table into an array $data[], and then save the data in the $data[] array echo is in JSON format, which is convenient for calling the front-end html page.
filelist.php file content is as follows:
<?php require 'conn.php'; $result = mysqli_query($link,"SELECT * FROM downloads"); //返回的记录数 if(mysqli_num_rows($result)){ //mysqli_num_rows() 函数返回结果集中行的数量 while($row=mysqli_fetch_assoc($result)){ $data[] = array( 'id' => $row['id'], 'file' => $row['filename'], 'downloads'=> $row['downloads'] ); } echo json_encode($data); //json_encode—返回一个值的JSON格式 } ?>
mysqli_num_rows() function returns the number of rows in the result set
json_encode—returns the JSON format of a value