Accessing the database using an object-oriented approach

Source: Internet
Author: User

Accessing the database using an object-oriented approach
New Mysqli ("Server name", "User name", "Password", "database Name");

Determine if the connection is successful
1.mysqli_connect_error ()
2.!mysqli_connect_error () or Die ("error!");

Execute SQL statement returns result set object
The Mysqli_query () function executes a query against the database:
Returns a Mysqli_result object when executing a select query; returns TRUE or FALSE when other statements are executed

Reading data from the result set object
Mysqli_fetch_all () returns all values in a two-dimensional array
Mysqli_fetch_array () returns the data that the current pointer points to
MYSQLI_FETCH_ASSOC () Returns the data pointed to by the current pointer in the form of an associative array
Mysqli_fetch_object () returns the object
Mysqli_fetch_row () returns the current data in the form of an indexed array

Common mysqli functions
Mysqli_affected_rows () returns the number of rows affected by the previous mSQL operation
Mysqli_connect_error () returns the error description of the last connection error
Mysqli_field_count () returns the number of columns to which the most recent query was obtained
MYSQLI_INSERT_ID () returns the ID generated by the insert operation in the previous step

Encapsulation of database operations classes
The first letter of the file name is capitalized. class.php

SQL statements:
1. Add Data:
INSERT into Brand values (' b001 ', ' BMW 5 '); #第一种方式
INSERT into Brand values (' b002 '); #第二种方式

INSERT into Pinpai values (' ', ' VW '); #处理自增长列

2. Most simple query
SELECT * FROM Pinpai #查询所有数据
SELECT * from Pinpai where Ids = 1; #根据条件查询

3. Modify the data

Update Pinpai Set Name = ' Volkswagen ' where Ids = 4; #修改某一条数据
Update Car set name= ' Harley ', time= ' 2012-3-4 ', price=16,brand= ' b002 ' where code= ' c001 '

4. Delete data

Delete from Brand #删除所有数据
Delete from Pinpai where Ids = 4; #根据条件删除数据

5. Fuzzy query
SELECT * from chinastates where areaname like '% ' #查询以中开头的
SELECT * from chinastates where areaname like '% City% ' #查询包含城的信息
SELECT * from chinastates where areaname like ' _ City% ' #查询城在第二个位置出现的数据

6. Sort queries
SELECT * from Car ORDER BY Code DESC #desc降序 ASC Ascending
SELECT * from Car ORDER by Brand
SELECT * from Car ORDER by Brand,powers #按照两个列排序

7. Statistical queries (aggregation functions)
Select COUNT (Code) from Car #查询总条数
Select Max from Car #查询最大值
Select min (Price) from Car #查询最小值
Select AVG (price) from Car #查询平均值
Select SUM (Price) from Car #查询总和

8. Group queries
Select Code,brand,count (*) from Car GROUP by Brand #根据系列分组查看每组的数据条数
SELECT * FROM Car GROUP by Brand have Count (*) >2 #查询分组之后数据条数大于2的

9. Paging Query
SELECT * from Car limit 5,5 #跳过几条数据取几条数据

10. Go to re-query
Select distinct Nation from Info

Accessing the database using an object-oriented approach

Related Article

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.