ajax request, php backend

What happened on the Internet after entering the address on the browser and seeing the page?

https://blog.csdn.net/chj285401200/article/details/79668950

A simple understanding of the front end:

1. DNS解析(将域名转换为IP地址的过程)
2. 向服务器发送一次HTTP请求(要一个东西)
3. 服务器会在它的磁盘上找个一个对应的文件给到客户端(一般来说是.html,.jpg.png.css.js等等)
4. 浏览器将根据已接收的文件将代码或图片解析成可见的页面。

http protocol

1. Stateless (there is no correlation between the first request and the next request).

2. The server is not allowed to actively push to the client

3. Often used in web data transmission

phpnow development integration kit

  • PHP
  • Apache (webService software)
  • MYSQL

ajax: http scripting-use js code to control http requests. Based on the real network environment.Parameters without quotes

Ajax request steps:

  1. Instantiate an XMLHttpRequest object
  • let http=new XMLHttpRequest();
  1. Plan a request open (method, url, async) false means synchronous, true means asynchronous, default;
  • http.open(“get”,“http://127.0.0.1”,true);
  1. Use send() to send the request
  • http.send();
  1. Accept the content returned by the server, the http.responseText backend returns the data
  • http.onreadystatechange=function(){

    ​ //The client must have received the response from the server.

    ​ //readyState status code:

    0: The request is not initialized
    1: The server has established a connection
    2: The request has been received
    3: The request is being processed
    4: The request has been completed and the response is ready

    ​ if(http.readyState===4){

    ​ //Data processing

    }

    }

Status code: 200, 304 succeeded, the client failed at the beginning of 400, and the server failed at the beginning of 500.

HTTP status code and ReadyState status code

https://blog.csdn.net/qq_39125445/article/details/82754400

mysql:

Database structure:

* 库 - Excel文件
* 表 - 文件内的表
* 列 - 表头
* 行 - 一条数据

The design principle of the table in the database: any table should have an id field, and the value of this field is unique, and the field should be the primary key in the table.

SQL statement:

<!-- 查询 -->

	SELECT * FROM 表名
	SELECT * FROM 表名 WHERE 字段名="某值"
	SELECT * FROM 表名 WHERE 字段名="某值" AND 字段名="某值"
	SELECT * FROM 表名 WHERE 字段名="某值" OR 字段名="某值"
	SELECT * FROM 表名 ORDER BY 字段名 根据某个的字段的值进行排序

	<!-- 删 -->

	DELETE FROM 表名 WHERE 字段名="某值";
	DELETE FROM 表名

	<!-- 增 -->

	INSERT INTO 表名 (字段名,字段名,字段名...) VALUES ("值1","值2","值3"...)

	<!-- 改 -->

	UPDATE 表名 SET 字段名="新值" WHERE 字段名="某值"
	UPDATE 表名 SET 字段名="新值",字段名="新值" WHERE 字段名="某值"

JSON: It is a lightweight data exchange format, which is mostly used for data exchange between front and back ends.

  • JSON string: The format is often transmitted when the backend sends data to the frontend. (Generally sent by the backend)
    • let json=[{"name":"wdw","age":18}]
  • JSON objects: usually what the front end hopes to get, arrays or objects nested between them. (What the front end expects to receive)
    • let Json=[{“name”:“wdw”,“age”:18}]

Conversion between JSON string and JSON object

  • JSON.parse (string in JSON format): is parsing, converting JSON strings into JSON objects without side effects.
  • JSON.stringify (JSON object): De-parse, convert JSON object into JSON string

php: is a powerful server-side scripting language for creating dynamic interactive sites

<?php

header("Content-Type:text/html;charset=utf-8");//解决返回值字符编码问题
//php用$声明变量,使用的时候也要带上$
// 连接mysql
$con=mysql_connect("localhost","root","123456");
//连库失败之后报错误信息
if(!$con){
    
    
    //返回给前台错误信息
    die("Could not connect:". mysql_error());
}else{
    
    
    //连接数据库
    mysql_select_db("student",$con);
    //解决字符编码问题
    mysql_query("set character set 'utf8'");//读库 
    mysql_query("set names 'utf8'");//写库
    //接受前端发过来的参数
    $name=$_GET["name"];
    //查询数据库
    //此时查询过来的$result是php世界里的关联数据。
    $result=mysql_query('SELECT * FROM students WHERE name="'.$name.'"');
    //使用字符串拼接将$result变成前端所认识的数组或者是对象(JSON字符串或者JSON对象)
    //mysql_fetch_array(data,array_type)函数从结果集中取得一行作为关联数组,或数字数组,或二者兼有,返回根据从结果集取得的行生成的数组,如果没有更多行则返回 false。
    //data可选。规定要使用的数据指针。该数据指针是 mysql_query() 函数产生的结果。
    //可选。规定返回哪种结果。可能的值:MYSQL_ASSOC - 关联数组,MYSQL_NUM - 数字数组,MYSQL_BOTH - 默认。同时产生关联和数字数组
    $response="[";
    while($row=mysql_fetch_array($result)){
    
    
        $response=$response.'{'.'"id":'.$row["id"].','.'"name":"'.$row["name"].'"'.','.'"sex":'.$row["sex"].','.'"age":'.$row["age"].','.'"birth":"'.$row["birth"].'"},';  
    }
    //截取字符串,处理成前端需要的json字符串
    $response=substr($response,0,strlen($response)-1).']';
    if(strlen($response)==1){
    
    
        echo "[]";
    }else{
    
    
        echo $response;
    }
    
}


?>

Guess you like

Origin blog.csdn.net/yanyuyanyan/article/details/112298094