PHP database operation code (add, delete, modify, check)
The basic process of database manipulation is: 1. Connect to the database server 2. Select database 3. Execute SQL statements 4. Processing result sets 5. Print operation information The related functions used are •resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]] ) Connect to the database server •resource mysql_pconnect ([string server [, string username [, string password [, int client_flags]]]] ) Connect to the database server, long connection •int mysql_affected_rows ([resource link_identifier]) Gets the number of record rows affected by the latest INSERT, UPDATE or DELETE query associated with link_identifier. • bool mysql_close ( [resource link_identifier] ) Returns TRUE if successful, FALSE if failed. •int mysql_errno ([resource link_identifier]) returns the error number of the previous MySQL function, or 0 (zero) if there is no error. •string mysql_error ([resource link_identifier]) Returns the error text of the previous MySQL function, or '' (empty string) if there is no error. If no connection resource number is specified, the last successfully opened connection is used to extract error information from the MySQL server. •array mysql_fetch_array ( resource result [, int result_type] ) Returns an array based on the rows fetched from the result set, or FALSE if…
Node.js operates mysql (add, delete, modify, check)_node.js-js tutorial
I feel good about studying Node recently. I made a CRUD by myself. Although it is a bit crude, the idea is clear. In fact, all projects are CRUD, which helps beginners quickly master Node First This example shows a set of additions, deletions, modifications and queries quickly built based on Node+Express+node-mysql. The view template is jade, which is basically a technology that can be used now, and there are very few examples on the market. It’s useful but not new, so I wrote one myself Basic work First of all, we prepare some basic things. Because I use mysql, I can install mysql myself. I can download installation packages for various operating systems from the official website. The example is just one table. The following is the table creation statement for this table SET NAMES utf8; SET FOREIGN_KEY_CHECKS = 0 —————————- — Table structure for `user` —————————- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, `age` int(4) DEFAULT NULL, `info` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8; SET FOREIGN_KEY_CHECKS = 1; Go to GIT to download my project npm install installs…
MySQL add, delete, modify, query tool PHP class
In the past, the development project did not use a framework and directly developed a very practical mysql tool class for object-oriented development. <?php 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 operates a row of query results *@param sql *@return array Returns false if failed; */ public function queryRow($sql) { $rs = $this->queryRows($sql); if(!empty($rs[0])){ return $rs[0]; } return false; } /** *General query method select operates the query result on one data *@param sql *@return array Returns 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…
Simple PHP database operation code (add, delete, modify, check)_PHP tutorial
The basic process of database manipulation is: 1. Connect to the database server 2. Select database 3. Execute SQL statement 4. Processing result sets 5. Print operation information The related functions used are •resource mysql_connect ([string server [, string username [, string password [, bool new_link [, int client_flags]]]]] ) Connect to the database server•resource mysql_pconnect ( [string server [, string username [, string password [, int client_flags]]]]) Connect to the database server, long connection•int mysql_affected_rows ([resource link_identifier]) Get the latest INSERT, UPDATE or associated with link_identifier The number of rows affected by the DELETE query. bool mysql_close ([resource link_identifier]) Returns TRUE if successful, FALSE if failed. •int mysql_errno ([resource link_identifier]) returns the error number of the previous MySQL function, or 0 (zero) if there is no error. •string mysql_error ([resource link_identifier]) returns the error text of the previous MySQL function, or ” (empty string) if there is no error. If no connection resource number is specified, the last successfully opened connection is used to extract error information from the MySQL server. array mysql_fetch_array ( resource result [, int result_type] ) Returns an array based on the rows fetched from the result set, or FALSE if there are no more rows.…
PHP database operation code (add, delete, modify, check)_PHP tutorial
The basic process of database manipulation is: 1. Connect to the database server 2. Select database 3. Execute SQL statements 4. Processing result sets 5. Print operation information The related functions used are •resource mysql_connect ([string server [, string username [, string password [, bool new_link [, int client_flags]]]]] ) Connect to the database server •resource mysql_pconnect ([string server [, string username [, string password [, int client_flags]]]] ) Connect to the database server, long connection •int mysql_affected_rows ([resource link_identifier]) Gets the number of record rows affected by the latest INSERT, UPDATE or DELETE query associated with link_identifier. • bool mysql_close ( [resource link_identifier] ) Returns TRUE if successful, FALSE if failed. •int mysql_errno ([resource link_identifier]) returns the error number of the previous MySQL function, or 0 (zero) if there is no error. •string mysql_error ([resource link_identifier]) Returns the error text of the previous MySQL function, or '' (empty string) if there is no error. If no connection resource number is specified, the last successfully opened connection is used to extract error information from the MySQL server. •array mysql_fetch_array ( resource result [, int result_type] ) Returns an array based on the rows fetched from the result set, or FALSE if there…
PHP lightweight database operation class Medoo add, delete, modify, query examples_PHP tutorial
Introduction to Medoo Medoo is an ultra-lightweight PHP SQL database framework developed by Li Yanzhuo, the founder of the social networking site Catfan and the open source project Qatrix. It provides a simple, easy-to-learn, and flexible API to improve the efficiency and performance of developing web applications, and the size is less than 8KB. Features Lightweight, only one file Easy to learn, the data structure is clear at a glance Supports multiple SQL syntaxes and complex query conditions Supports a variety of databases, including MySQL, MSSQL, SQLite, etc. Safe, prevents SQL injection Free, based on MIT license Sample code Add The code is as follows: $database = new medoo ( “my_database” ); $last_user_id = $database->insert ( “account”, [ “user_name” => “foo”, “email” => “[email protected]”, “age” => 25, “lang” => [ “en”, “fr”, “jp”, “cn” ] ] ); Delete The code is as follows: $database = new medoo ( “my_database” ); $database->delete(“account”, [ “AND” => [ “type” => “business” “age[ 18 ] ]); Edit The code is as follows: $database = new medoo ( “my_database” ); $database->update ( “account”, [ “type” => “user”, // All age plus one “age[+]” => 1, // All level subtract 5 “level[-]” => 5, “lang” =>…
PHPMySql add, delete, modify, check, phpmysql add, delete, modify_PHP tutorial
PHP MySql add, delete, modify, check, phpmysql add, delete, modify mysql_connect() connects to the database mysql_select_db selects the database mysql_fetch_assoc() gets the result set mysql_query() executes sql statement Examples are as follows: <?php $con=@mysql_connect(‘localhost’,’root’,’root’);//Connect to the database mysql_select_db(‘test’, $con);//Select database $userInfo=mysql_query(“select * from user”,$con);//Create sql statement echo “ “; while( $row=mysql_fetch_assoc($userInfo))//Get the data of each row of the result set { echo “ “; echo “ “; echo $row[‘id’];//Get the id in the row echo “ “; echo “ “; echo $row[‘username’] ; echo “ “; echo “ “; echo $row[‘password’]; echo “ “; echo “ “; } echo “ “; mysql_free_result($userInfo);//Release the result set mysql_close($ con); //Close the database /*delete $d=@mysql_connect(‘localhost’,’root’,’root’); mysql_select_db(‘test’,$d) ; mysql_query(“delete from user where id=1”,$d); mysql_close($d); */ /*insert $i =@mysql_connect(‘localhost’,’root’,’root’); mysql_select_db(‘test’,$i); mysql_query(“insert into user(username,password) values(‘ad ‘,’ad’)”,$i); mysql_close($i); */ //update $u=@mysql_connect(‘localhost’,’ root’,’root’); mysql_select_db(‘test’,$u); mysql_query(“update user set username=’aaa’ where username=’00′”,$u); echo “update user set username=’ax’ where id=2”; mysql_close($u);?> Thank you for your support! You can contact me for communication. [email protected] Technorati tags: PHP, Mysql, add, delete, modify and check http://www.bkjia.com/PHPjc/1135474.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1135474.htmlTechArticlePHP MySql add, delete, modify, query, phpmysql add, delete, modify mysql_connect() connect to the database mysql_select_db select the database mysql_fetch_assoc() get the result set mysql_query() execute the sql statement…
PHP operates sqlite classes. Add, delete, modify, check, pdo link
Direct code:Note: Be sure to write the database save path db = new PDO(‘sqlite:’.dirname(__FILE__ ).’\log.db’); $this->table_name=$tab; $this->tab_init(); } public function tab_init() { # Table initialization, create table $this->db->exec(” CREATE TABLE log( id integer PRIMARY KEY autoincrement, urls varchar(200), ip varchar(200), datetimes datetime default (datetime(‘now’, ‘localtime’)) )”); } public function insert($tab_name,$ key_list,$value_list) { // echo “INSERT INTO “.$tab_name.” (“.$key_list.”) values(“.$value_list.”)”; $result=$this->db->exec(” INSERT INTO “.$tab_name.” (“.$key_list.”) values(“.$value_list.”)”); if (!$result) { return false; } // echo “{{{INSERT INTO “. $tab_name.” (“.$key_list.”) values(“.$value_list.”)}}}}”; $res=$this->db->beginTransaction();//Transaction return } public function total ($tab_name,$tj=”)//Find the total number of records { $sth = $this->db->prepare(‘SELECT count(id) as c FROM ‘.$tab_name.’ ‘.$tj); $sth->execute(); $result = $sth->fetchAll(); return $result[0][‘c’]; } public function update() { # Modify } function delete($value=”) { # Delete } public function query($tab_name,$tj=”)//Table name and conditions { $sth = $this->db->prepare(‘SELECT * FROM ‘.$tab_name.’ ‘.$tj ); // echo ‘SELECT * FROM ‘.$tab_name.’ ‘.$tj; $sth->execute(); $result = $sth->fetchAll(); return $result; }}// $db= new SqliteDB();// $res=$db->insert(‘log’,’ip,urls,datetimes’,'”127.0.0.1″,”www.baidu.com”,”2012-12-12 00 :00:00″‘);//Add case// $res=$db->query(‘log’);//Query case// $res=$db->total(‘log’);// Query case // print_r($res);// foreach ($res as $key => $row) {// echo $row[‘urls’];// }?> Modify and find your own perfection. ! If you don’t understand, call qq 1186969412
Determine whether PHP operation mysql (add, delete, modify, check) is successful
I have recently been using a CI framework, but the database operations there are not as convenient as ThinkPhp. I don’t know the feedback information of the database operations, so I have to use native methods to determine whether the database operation is successful Determine whether PHP operates mysql (add, delete, modify, check). Success mainly relies on two functions 1. The mysql_num_rows(data) function returns the number of rows in the result set 2. The mysql_affected_rows() function returns the record rows affected by the previous MySQL operation. Number mysql_num_rows(data) is mainly used for select operations. The return value has three situations 1. Returning 0 means that the select value is 0 and no results were obtained p> 2. The return value is NULL, indicating that there is an error in the previous select statement 3. The return value is greater than 0, indicating the number of rows that meet the conditions Mysql_affected_rows() is mainly Used for insert, update, and delete operations 1. A return value of 0 means that the number of affected rows is 0 2. A return value of -1 means There was a problem with the previous sql statement 3. The return value greater than 0 indicates the…