Site basis using the database

Recently, our class project underway week, the content is done alone a complete electronic business website, encountered in this database, specially in this summarize, the total is divided into three parts, the front end (javascript), the server (php), database;

The front part:

var xhr=new XMLHttpRequest();
xhr.addEventListener ( "load", loadHandler); // listen for events to execute the function
xhr.open ( "get", "http: // localhost: 7800 / php / list.php id =?" + pid) // send way is open, things will be sent in? Behind, way behind the sending address is PHP files
xhr.setRequestHeader ( "content-type", "application / x-www-form-urlencoded") // set header
xhr.send (); // send
 
function loadHandler(e){
if (xhr.status == 200 && xhr.readyState === 4); // Ajax: readyState (state value) and the status (status codes)
math (xhr.response) // function name returned by the database data
}

   PHP section

<?php
header("content-type:text/html;charset=utf-8");
header("Access-Control-Allow-Origin:*");
openDataBase (); // open the database
function openDataBase(){
$ Sql ​​= mysqli_connect ( "localhost", "root", "root", "list", "3306"); // database address, account number, password, database name, port number
if function (mysqli_connect_errno ()) {// perform connection failure
echo "connection failed";
return;
}
insertUser($sql);
}
function insertUser($sql){
$ Goodsid = $ _GET [ "id"]; // getting a property value passed in
$ R = mysqli_query ($ sql, "SELECT * FROM list where id = $ goodsid"); // find data in a database according to the attribute
$arr=array();
while($arr1 = mysqli_fetch_assoc($r)){
array_push($arr,$arr1);
}
echo json_encode ($ arr); // return data
}

 Database section:

Please establish a database in PHPstudy according to their needs exception Navicat Premium software also has the ability to create databases (To open phpstudy)

Reproduced in: https: //www.cnblogs.com/wwmm1996/p/10982068.html

Guess you like

Origin blog.csdn.net/weixin_34162695/article/details/93430579