PHP simple database operation instance

Source: Internet
Author: User
This article mainly introduces simple PHP database operation instances and supports addition, deletion, modification, query, and chain operations. it is very suitable for the development and use of small projects and provides complete instances at the end, for more information about PHP simple database operations, see the example in this article. We will share this with you for your reference. The details are as follows:

Databases are essential for project development. However, it is often a headache for the complexity of Database SQL statements. Provides a database operation class (Model) for your use. Supports addition, deletion, modification, and query, and chain operations. the code contains less than 100 lines, which is very small and convenient and suitable for quick deployment and use of small projects.

/*** @ Authot: summer *** @ E-mail: wenghang1228@me.com *** @ Data: 2015-02-06 *** @ Project: database operation class Model *** @ Version: 1.0 ** @ Copyright: Summer Wind ---- record summer's passion for technology and life ** @ URL: http://www.xtwind.com **/Class Model {public $ field; public $ tabname; public $ where; public $ order; public $ limit; // Constructor, link to database, assign function _ construct ($ tabname) {mysql_connect (HOST, USER, PASSWORD); mysql_select_db (DBNAME); mysql_query ("set names utf8") to the table "); $ this-> tabname = $ tabname;} // function field ($ field) {$ this-> $ field = $ field; return $ this ;} // combine the where condition function where ($ where) {$ this-> where = "where ". $ where; return $ this;} // function order ($ order) of the combined order sorting condition {$ this-> order = "order ". $ order; return $ this;} // function limit ($ limit) {$ this-> limit = "limit ". $ limit; return $ this;} // combine and execute the select statement function select ($ all = "") {if ($ all) {$ SQL = "select {$ all} from {$ this-> tabname} order by id ";} else {$ SQL = "select {$ this-> filed} from {$ this-> tabname} {$ this-> where} {$ this-> order} {$ this-> limit }";} $ rst = mysql_query ($ SQL); while ($ row = mysql_fetch_assoc ($ rst) {$ rows [] = $ row;} return $ rows ;} // combine and execute the insert statement function insert ($ post) {// database insert operation, receiving array foreach ($ post as $ key => $ value) {$ keys [] = $ key; $ vals [] = "'". $ valu. "'" ;}$ keyStr = join (",", $ keys); $ valStr = join (",", $ vals ); $ SQL = "insert into {$ this-> tabname} ($ keystr) values ()"; if (mysql_query ($ SQL) {return mysql_insert_id ();} else {return false ;}// combine and execute the delect statement function delect () {$ SQL = "delect from {$ this-> tabname} {$ this-> where}"; if (mysql_query ($ SQL) {return mysql_affected_rows ();} else {return false ;}// combine and execute the updata statement function update ($ post) {foreach ($ psot as $ key => $ value) {$ sets [] = "{$ key} = '{$ val}'" ;}$ setStr = join (",", $ sets ); $ SQL = "update {$ this-> tabname} set {$ setStr} {$ this-> where}"; if (mysql_query ($ SQL )) {return mysql_affected_rows () ;}else {return false ;}// function find () {if ($ this-> order) {$ SQL = "select * from {$ this-> tabname} {$ this-> order} limit 1 ";} else {$ SQL = "select * from {$ this-> tabname} order by id limit 1" ;}$ rst = mysql_query ($ SQL ); while ($ row = mysql_fetch_assoc ($ rst) {$ rows [] = $ row;} return $ rows;} // obtain the total number of rows function total () {$ SQL = "select count (*) from {$ this-> tabname}"; $ rst = mysql_query ($ SQL); if ($ rst) {$ row = mysql_fetch_row ($ rst); return $ row [0];} else {return false ;}}// Model class Object Factory function M ($ tabname) {return new Model ($ tabname);} define ("HOST", "localhost"); define ("USER", "root"); define ("PASSWORD ", "123456"); define ("DBNAME", "test") $ user = new Model ("user"); $ user-> field ("id, name ") -> where ("id = 3")-> order ("id desc")-> limit ("3")-> select ();

I hope this article will help you with PHP programming.

For more articles about simple PHP database operations, refer to PHP Chinese network!

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.