MySQL add, delete, modify, query tool PHP class
Release: 2016-07-25 08:43:37
Original
1107 people have browsed it
- In the past, the development project did not use a framework, and it was a very practical mysql tool class that was directly developed as object-oriented.
-
- header("content-type:text/html;charset=utf-8");
- class DBUtils{
-
-
- /**
- *General update method insert update delete operation
- *@param sql
- *@return bool true false
- */
- public function update($sql){
- $link = $this->getConn();
- mysql_query($sql);
- //If an error occurs
- if(DEBUG){
- echo mysql_error();
- }
- $rs = mysql_affected_rows($link);
- $rs = $rs > 0;
- mysql_close($link);
- return $rs;
- }
-
- /**
- *General query method select operation
- *@param sql
- *@return array
- */
- public function queryRows($sql){
- //Create connection, encoding , database
- $link = $this->getConn();
- //Send sql
- $rs = mysql_query($sql);
- //If an error occurs
- if(DEBUG){
- echo mysql_error();
- }
-
-
- $rows = array();
- while($row = mysql_fetch_array($rs)){
- $rows[] = $row;//pdemo7.php
- }
- //
- mysql_free_result($rs);
- mysql_close($link);
- return $rows;
- }
-
-
- /**
- *General query method select operation query result one row of data
- *@param sql
- *@return array Return false if failed;
- */
- public function queryRow($sql){
- $rs = $this->queryRows($sql);
- if (!empty($rs[0])){
- return $rs[0];
- }
- return false;
- }
-
- /**
- *General query method select operation Query result is one data
- *@param sql
- *@return array Return false if failed;
- * Example: select count(*) from user;
- */
- public function queryObj($sql){
- $rs = $this->queryRows($sql);
- //var_dump($rs);
- if(!empty($rs[0][0])){
- return $rs[0][0];
- }
- return false;
- }
-
-
- private function getConn(){
- $link = mysql_connect('127.0.0.1','root','');
- mysql_query("set names utf8");
- mysql_select_db(" news");
- return $link;
- }
-
- }
Copy code
|
MySQL, PHP
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31