HTML and Php+mysql complete front-end interaction

Source: Internet
Author: User
Tags php create database
1.php Connection database and basic Operation configuration
(1) PHP CREATE DATABASE "Connect"

Syntax: Object mysqli_connect ("Domain name", "DB Account", "DB password", "DB Library name") Example: $con = Mysqli_connect (' localhost ', ' root ', ', ' frankdb ');

(2) A solution that contains garbled Chinese characters when inserting data into DB
Grammar:

Mysqli_query ($con, "Set names UTF8");

Note: Setting success returns 1, and it does not necessarily have to save the returned results, depending on the situation.
(3) Set client side and server side to keep character encoding consistent
Grammar:

Mysqli_query ($con, "set Character_set_client=utf8");    Mysqli_query ($con, "set Character_set_results=utf8");

(4) Execute SQL statement

Syntax: $ result = $DB Connection->query (SQL statement); Example: var_dump ($result = $con->query ($sql));

2. Using the SQL statement basic "template"
A. Establishing a connection
B. Decide whether to connect
C. Setting the Encoding
D. Creating SQL statements
E Execution Result Bar number
G. Patchwork results
Return of H.json


<?php//a.sql Query Statement unconditional query, that is, directly write 1 can//$sql = ' SELECT * from which table where condition '; a conditional query that writes a query condition after the where, if more than one condition needs to be connected with and OR OR. $sql = "Select Stuname from Stud where stuscore= ' and stugender= ' female '";//$sql = "Select Stuname from Stud where Stu        Score= ' or stugender= ' female ';               $con =mysqli_connect (' localhost ', ' root ', ' ', ' studb ') if ($con) {echo ' <pre> ';               Echo ' Database connected successfully, waiting for instructions ... ';               Mysqli_query ($con, ' Set names UTF8 ');               Mysqli_query ($con, ' Set Character_set_client=utf8 ');               Mysqli_query ($con, ' Set Character_set_results=utf8 ');               $sql = "SELECT * from stud where 1";               $result = $con->query ($sql);                   if ($result >num_rows>0) {$info =[];                     for ($i =0; $row = $result->fet_assoc (); $i + +) {$info [$i]= $row;                  } Echo Json_encode ($info); }}else{                    Echo ' <pre> '; Echo ' Data connection failed, please reconnect ';}

B. INSERT statement (add statement)
Two types of notation:

(1) $sql = "INSERT into table name (Field 1, Field 2, ...)  Values (value 1, value 2, ...);( 2) $sql = ' INSERT into table name (' Value 1 ', ' Value 2 ',...) ';
$con =mysqli_connect (' localhost ', ' root ', ' ', ' studb ');                 if ($icon) {                        echo ' <pre> ';                        Echo ' Database connected successfully, waiting for instructions ... ';                        Mysqli_query ($con, ' Set names UTF8 ');                        Mysqli_query ($con, ' Set Character_set_client=utf8 ');                        Mysqli_query ($con, ' Set Character_set_result=utf8 ');                                                $sql = "INSERT INTO  stud  stuname,stugender,stuage,stunum,stuscore" values (' Lucy ', ' female ', ' 14 ', ' 123456789 ') , ' n ') ";                        $sql = "INSERT INTO  stud  values (' Lucy ', ' female ', ' + ', ' 123456789 ', ' n ')";                        $result = $con->query ($sql);                            if ($result) {                               echo ' add success ';                           } else{                               Echo ' add failed ';                         }

C. Modifying statements (UPDATE statements) update

$sql = "Update  table name set field 1 = ' new value 1 ', Field 2 = ' new value 2 ', ... where  condition";
$con =mysqli_connect (' localhost ', ' root ', ' ', ' studb ');   if ($con) {                      echo "<pre>";                echo "Database connection succeeded, waiting for instruction ...";                        Mysqli_query ($con, ' Set names UTF8 '); Mysqli_query ($con, ' Set Character_set_client=utf8 '); Mysqli_query ($con, ' Set Character_set_results=utf8 '); $sql = "Update  stud  set  stuscore= ' where stuname= ' Lily '"; $result =$ Con->query ($sql); Var_dump ($result);  } else{        echo "database connection Failed!!! ";}

D. Deleting a statement delete

$sql = "Delete from table name where condition"; $con =mysqli_connect (' localhost ', ' toot ', '; ')                        Studb '); if ($con) {echo "<pre>", echo "database connection succeeded, wait for instruction ...";//mysqli_query ($con, ' Set names UTF8 '); Mysqli_query ($con, ' Set Character_set_client=utf8 '); Mysqli_query ($con, ' Set Character_set_results=utf8 ');//$sql = "Delete from stud where Stuname= ' Lucy '; $result = $con->query ($sql); Var_dump ($result);} Else{echo "Database connection Failed!!! ";}    ?                                  > Ajax & PHP <! DOCTYPE html>

Related recommendations:

Php+mysql to develop a simple online problem-solving system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.