PHP ==> Database

Source: Internet
Author: User
Tags md5

== PHP is working with SQL database.

  = = Database deconstruction:

Database server ==> database ==> table ==> record ==> field ==> information

It can be understood as follows:

Database Server ====> a book

Database ====> A page of paper in the book

Table ====> A table drawn on paper

Record each row on the ====> table

field ====> each column on the table

Information ====> the contents of each table

= = Database Common operations

  = = Add data

1, connecting to the database (if using a server, change localhost to the server's IP)
Span style= "font-size:14pt" >                          &N Bsp             address        account   password     Corresponding database
$db = new Mysqli (' root ', ' 168168 ', '


3. Perform insert data operation: Execute an SQL statement that inserts data
Insert fixed format: "insert into table name (Field 1, Field 2, Field 3, Field 4) VALUES (" Content 1 "," Content 2 "," Content 3 ","content 4 ",)"
$sql = 'INSERT into Users (username,passwd,tel,addtime) VALUES (". $username. '", "'. MD5 ($PASSWD). '", "'. $tel. '". Date (' y-m-d h:i:s '). ')';
    The content must correspond with the field one by one

4. Execute SQL statement to insert data into the database
$r = $db->query ($sql);

5. Close the database
$db->close ();

= = query data

1. Connect to the database (if using a server, change localhost to the server's IP)
Address account password corresponding database
$db = new mysqli (' localhost ', ' root ', ' 168168 ', ' user ');

2. The encoding of the setting data is not between UTF8:UTF and 8-
$db->query (' SET namds UTF8 ');

3. Perform insert data operation: Execute an SQL statement that inserts data

{The field to be queried, if you want to query all fields, fill in the * number } table name judgment Statement, Judge query that record, do not fill in the query all

$sql = 'SELECT id,username,passwd,tel,addtime from users WHERE Username = "'. $_post[' username ']." ';

4. Execute SQL statement query data
$r = $db->query ($sql);

5, the results of the query by associative array to save the way
$arr = $r->fetch_array (mysqli_assoc);

6. The corresponding value can be accessed by means of array access subscript

$arr [' username ']==> get username

7. Close the database
$db->close ();

= = Delete data

1. Connect to the database (if using a server, change localhost to the server's IP)
Address account password corresponding database
$db = new mysqli (' localhost ', ' root ', ' 168168 ', ' user ');

2. The encoding of the setting data is not between UTF8:UTF and 8-
$db->query (' SET namds UTF8 ');

3. Perform insert data operation: Execute an SQL statement that inserts data

Keyword keyword table name determine the criteria to delete that record, it must be the only condition

$sql = 'DELETE from users WHERE id = '. ( int) $id;

Execute DELETE statement
$r = $db->query ($sql);

4. Close the database
$db->close ();

= = Modify Data

1. Connect to the database (if using a server, change localhost to the server's IP)
Address account password corresponding database
$db = new mysqli (' localhost ', ' root ', ' 168168 ', ' user ');

2. The encoding of the setting data is not between UTF8:UTF and 8-
$db->query (' SET namds UTF8 ');

3. Perform insert data operation: Execute an SQL statement that inserts data

Keyword table name modification field = "New content" field = "new content" field = "New Content" the one that must be changed

$sql = 'UPDATE users SET username= '. $username. ' ",passwd=" '. MD5 ($PASSWD). ' ", tel= "'. $tel. ' WHERE id = '. ( int) $id. ' ";

Execute DELETE statement
$r = $db->query ($sql);

4. Close the database
$db->close ();

PHP ==> Database

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.