PHP ?? ? ???? ?? ?? ?? ?? ??(1)
?? ?????? ?? ??? conn.php ??? ????? require 'conn.php' ??? ????? ?????.
require ?? ?? ???? ?? ??? ??? ??? ??? ???? ? ?????.
??? ?????? ?? 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"); ?>
mysql ??? ???? ???? ?? ?? filelist.php? ?? ???, ????? HTML ??? ??? ?? JSON ???? ???? ?????
?? require ?? ???? conn.php ?????? ??? ????, SQL ??? SELECT? ?? ?????? ???? ???? ?? ???? ?????
???? ???? ????. while ?? ??? $data[] ??? ????, $data[] ??? ???? JSON ???? ?????. ?? ??? ??? ?????. -html ??? ?.
filelist.php ?? ??? ??? ????.
<?php require 'conn.php'; $result = mysqli_query($link,"SELECT * FROM downloads"); //返回的記錄數(shù) if(mysqli_num_rows($result)){ //mysqli_num_rows() 函數(shù)返回結(jié)果集中行的數(shù)量 while($row=mysqli_fetch_assoc($result)){ $data[] = array( 'id' => $row['id'], 'file' => $row['filename'], 'downloads'=> $row['downloads'] ); } echo json_encode($data); //json_encode—返回一個(gè)值的JSON格式 } ?>
mysqli_num_rows() ??? ?? ??? ? ?? ?????.
json_encode—JSON ??? ?? ?????