Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement simple Database Operation Class by PHP

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "PHP how to achieve a simple database operation class", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "PHP how to achieve a simple database operation class" this article.

The details are as follows:

In the project development, the database is an essential thing. However, most of the time, it is a headache for the complexity of database SQL statements. Provide a database operation class (model Model) that I use for everyone to use. Support to add, delete, change, check, support chain operation, less than 100 lines of code, very small and convenient, very suitable for the rapid deployment of small projects.

/ * @ Authot: summer** @ E-mail: wenghang1228@me.com** @ Data: 2015-02-06 operations * @ Project: database operations Model** @ Version:1.0** @ copyright: summer Wind-record summer's pursuit of technology and love of life * * @ website: http://www.xtwind.com**/class Model {public $field; public $tabname; public $where; public $order; public $limit / Constructor, link database, assign function _ construct ($tabname) {mysql_connect (HOST,USER,PASSWORD); mysql_select_db (DBNAME); mysql_query ("set names utf8"); $this- > tabname=$tabname;} / / combined field function field ($field) {$this- > $field=$field; return $this;} / / combined where condition function where ($where) {$this- > where= "where". $where; return $this } / / combined order sorting condition function order ($order) {$this- > order= "order by". $order; return $this;} / combined limit limit number 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 the 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 delect statements function delect () {$sql= "delect from {$this- > tabname} {$this- > where}"; if (mysql_query ($sql)) {return mysql_affected_rows ();} else {return false }} / / combine and execute updata statements 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 }} / / fetch a row of data 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 } / / get 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 (); these are all the contents of the article "how PHP implements simple database operations". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 203

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report