PHP MysqlI操作数据库(转)

1连接数据库.

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->//procedural style $mysqli =  mysqli_connect(‘host‘,‘username‘,‘password‘,‘database_name‘);
//object oriented style (recommended) $mysqli = new mysqli(‘host‘,‘username‘,‘password‘,‘database_name‘);
推荐下面的方式

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php //Open a new connection to the MySQL server $mysqli = new mysqli(‘host‘,‘username‘,‘password‘,‘database_name‘);
//Output any connection error if ($mysqli->connect_error) {     die(‘Error : (‘. $mysqli->connect_errno .‘) ‘. $mysqli->connect_error); }
?>

2.选择多行 mysqli_fetch_assoc() : Below is the code to fetch multiple records as an associative array. The returned array holds the strings fetched from database, where the column names will be the key used to access the internal data. As you can see below, data is displayed in an HTML table.

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php //Open a new connection to the MySQL server $mysqli = new mysqli(‘host‘,‘username‘,‘password‘,‘database_name‘);
//Output any connection error if ($mysqli->connect_error) {     die(‘Error : (‘. $mysqli->connect_errno .‘) ‘. $mysqli->connect_error); }
//MySqli Select Query $results = $mysqli->query("SELECT id, product_code, product_desc, price FROM products");
print ‘<table border="1">‘; while($row = $results->fetch_assoc()) {     print ‘<tr>‘;     print ‘<td>‘.$row["id"].‘</td>‘;     print ‘<td>‘.$row["product_code"].‘</td>‘;     print ‘<td>‘.$row["product_name"].‘</td>‘;     print ‘<td>‘.$row["product_desc"].‘</td>‘;     print ‘<td>‘.$row["price"].‘</td>‘;     print ‘</tr>‘; }  print ‘</table>‘;
// Frees the memory associated with a result $results->free();
// close connection $mysqli->close(); ?>

3.选择 fetch_array() : Function returns an array of both mysqli_fetch_row and mysqli_fetch assoc merged together, it is an extended version of the mysqli_fetch_row() function and both numeric and string can be used as keys to access the data.

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php //Open a new connection to the MySQL server $mysqli = new mysqli(‘host‘,‘username‘,‘password‘,‘database_name‘);
//Output any connection error if ($mysqli->connect_error) {     die(‘Error : (‘. $mysqli->connect_errno .‘) ‘. $mysqli->connect_error); }
//MySqli Select Query $results = $mysqli->query("SELECT id, product_code, product_desc, price FROM products");
print ‘<table border="1"‘; while($row = $results->fetch_array()) {     print ‘<tr>‘;     print ‘<td>‘.$row["id"].‘</td>‘;     print ‘<td>‘.$row["product_code"].‘</td>‘;     print ‘<td>‘.$row["product_name"].‘</td>‘;     print ‘<td>‘.$row["product_desc"].‘</td>‘;     print ‘<td>‘.$row["price"].‘</td>‘;     print ‘</tr>‘;
}   print ‘</table>‘;
// Frees the memory associated with a result $results->free(); // close connection $mysqli->close(); ?>

4.选择 fetch_object() : To fetch database result set as an objects, just use MySqli fetch_object(). The attributes of the object represent the names of the fields found within the result set.

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php //Open a new connection to the MySQL server $mysqli = new mysqli(‘host‘,‘username‘,‘password‘,‘database_name‘);
//Output any connection error if ($mysqli->connect_error) {     die(‘Error : (‘. $mysqli->connect_errno .‘) ‘. $mysqli->connect_error); }
//MySqli Select Query $results = $mysqli->query("SELECT id, product_code, product_desc, price FROM products");
print ‘<table border="1">‘; while($row = $results->fetch_object()) {     print ‘<tr>‘;     print ‘<td>‘.$row->id.‘</td>‘;     print ‘<td>‘.$row->product_code.‘</td>‘;     print ‘<td>‘.$row->product_name.‘</td>‘;     print ‘<td>‘.$row->product_desc.‘</td>‘;     print ‘<td>‘.$row->price.‘</td>‘;     print ‘</tr>‘; } 
print ‘</table>‘;
// close connection $mysqli->close(); ?>

5.选择单行

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php //Open a new connection to the MySQL server $mysqli = new mysqli(‘host‘,‘username‘,‘password‘,‘database_name‘);
//Output any connection error if ($mysqli->connect_error) {     die(‘Error : (‘. $mysqli->connect_errno .‘) ‘. $mysqli->connect_error); }
//chained PHP functions $product_name = $mysqli->query("SELECT product_name FROM products WHERE id = 1")->fetch_object()->product_name; print $product_name; //output value $mysqli->close(); ?>

6.选择行数 <?php

//Open a new connection to the MySQL server $mysqli = new mysqli(‘host‘,‘username‘,‘password‘,‘database_name‘);
//Output any connection error if ($mysqli->connect_error) {     die(‘Error : (‘. $mysqli->connect_errno .‘) ‘. $mysqli->connect_error); }
//get total number of records $results = $mysqli->query("SELECT COUNT(*) FROM users"); $get_total_rows = $results->fetch_row(); //hold total records in variable $mysqli->close(); ?>

7.选择预处理

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->$search_product = "PD1001"; //product id
//create a prepared statement $query = "SELECT id, product_code, product_desc, price FROM products WHERE product_code=?"; $statement = $mysqli->prepare($query);
//bind parameters for markers, where (s = string, i = integer, d = double,  b = blob) $statement->bind_param(‘s‘, $search_product);
//execute query $statement->execute();
//bind result variables $statement->bind_result($id, $product_code, $product_desc, $price);
print ‘<table border="1">‘;
//fetch records while($statement->fetch()) {     print ‘<tr>‘;     print ‘<td>‘.$id.‘</td>‘;     print ‘<td>‘.$product_code.‘</td>‘;     print ‘<td>‘.$product_desc.‘</td>‘;     print ‘<td>‘.$price.‘</td>‘;     print ‘</tr>‘;
}   print ‘</table>‘;
//close connection $statement->close();

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->$search_ID = 1; $search_product = "PD1001";
$query = "SELECT id, product_code, product_desc, price FROM products WHERE ID=? AND product_code=?"; $statement = $mysqli->prepare($query); $statement->bind_param(‘is‘, $search_ID, $search_product); $statement->execute(); $statement->bind_result($id, $product_code, $product_desc, $price);
print ‘<table border="1">‘; while($statement->fetch()) {     print ‘<tr>‘;     print ‘<td>‘.$id.‘</td>‘;     print ‘<td>‘.$product_code.‘</td>‘;     print ‘<td>‘.$product_desc.‘</td>‘;     print ‘<td>‘.$price.‘</td>‘;     print ‘</tr>‘;
}   print ‘</table>‘;
//close connection $statement->close();

8.插入数据库

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php //values to be inserted in database table $product_code = ‘"‘.$mysqli->real_escape_string(‘P1234‘).‘"‘; $product_name = ‘"‘.$mysqli->real_escape_string(‘42 inch TV‘).‘"‘; $product_price = ‘"‘.$mysqli->real_escape_string(‘600‘).‘"‘;
//MySqli Insert Query $insert_row = $mysqli->query("INSERT INTO products (product_code, product_name, price) VALUES($product_code, $product_name, $product_price)");
32432432 if($insert_row){     print ‘Success! ID of last inserted record is : ‘ .$mysqli->insert_id .‘<br />‘; }else{     die(‘Error : (‘. $mysqli->errno .‘) ‘. $mysqli->error); }
?>

9.插入预处理

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->//values to be inserted in database table $product_code = ‘P1234‘; $product_name = ‘42 inch TV‘; $product_price = ‘600‘;
$query = "INSERT INTO products (product_code, product_name, price) VALUES(?, ?, ?)"; $statement = $mysqli->prepare($query);
//bind parameters for markers, where (s = string, i = integer, d = double,  b = blob) $statement->bind_param(‘sss‘, $product_code, $product_name, $product_price);
if($statement->execute()){     print ‘Success! ID of last inserted record is : ‘ .$statement->insert_id .‘<br />‘; }else{     die(‘Error : (‘. $mysqli->errno .‘) ‘. $mysqli->error); } $statement->close();

10.批量插入

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->//product 1 $product_code1 = ‘"‘.$mysqli->real_escape_string(‘P1‘).‘"‘; $product_name1 = ‘"‘.$mysqli->real_escape_string(‘Google Nexus‘).‘"‘; $product_price1 = ‘"‘.$mysqli->real_escape_string(‘149‘).‘"‘;
//product 2 $product_code2 = ‘"‘.$mysqli->real_escape_string(‘P2‘).‘"‘; $product_name2 = ‘"‘.$mysqli->real_escape_string(‘Apple iPad 2‘).‘"‘; $product_price2 = ‘"‘.$mysqli->real_escape_string(‘217‘).‘"‘;
//product 3 $product_code3 = ‘"‘.$mysqli->real_escape_string(‘P3‘).‘"‘; $product_name3 = ‘"‘.$mysqli->real_escape_string(‘Samsung Galaxy Note‘).‘"‘; $product_price3 = ‘"‘.$mysqli->real_escape_string(‘259‘).‘"‘;
//Insert multiple rows $insert = $mysqli->query("INSERT INTO products(product_code, product_name, price) VALUES ($product_code1, $product_name1, $product_price1), ($product_code2, $product_name2, $product_price2), ($product_code3, $product_name3, $product_price3)");
if($insert){     //return total inserted records using mysqli_affected_rows     print ‘Success! Total ‘ .$mysqli->affected_rows .‘ rows added.<br />‘; }else{     die(‘Error : (‘. $mysqli->errno .‘) ‘. $mysqli->error); }

11.更新删除

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->//MySqli Update Query $results = $mysqli->query("UPDATE products SET product_name=‘52 inch TV‘, product_code=‘323343‘ WHERE ID=24");
//MySqli Delete Query //$results = $mysqli->query("DELETE FROM products WHERE ID=24"); if($results){     print ‘Success! record updated / deleted‘; }else{     print ‘Error : (‘. $mysqli->errno .‘) ‘. $mysqli->error;

} 12.预处理

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->$product_name = ‘52 inch TV‘; $product_code = ‘9879798‘; $find_id = 24;
$query = "UPDATE products SET product_name=?, product_code=? WHERE ID=?"; $statement = $mysqli->prepare($query);
//bind parameters for markers, where (s = string, i = integer, d = double,  b = blob) $results =  $statement->bind_param(‘ssi‘, $product_name, $product_code, $find_id);
if($results){     print ‘Success! record updated‘; }else{     print ‘Error : (‘. $mysqli->errno .‘) ‘. $mysqli->error; }

13.删除

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->//MySqli Delete Query $results = $mysqli->query("DELETE FROM products WHERE added_timestamp < (NOW() - INTERVAL 1 DAY)");
if($results){     print ‘Success! deleted one day old records‘; }else{     print ‘Error : (‘. $mysqli->errno .‘) ‘. $mysqli->error; }

http://www.blogjava.net/nkjava/archive/2015/01/20/422291.html

时间: 2024-11-05 20:39:14

PHP MysqlI操作数据库(转)的相关文章

Mysqli 操作数据库

每次用到mysql_connect连接数据库的时候都会提示: 1 Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in E:\wamp\www\myquote\includes\mysql_connect.php on line 5 作为一个高要求高完美的男人,实在受不了这提示.躲着他也不行——解决

php 通过mysqli 操作数据库mysql

目录 php mysqli 操作数据库 连接数据库 通过mysqli 创建数据库 通过mysqi 创建数据表 通过mysqli向数据表中插入信息 通过mysqli 读取数据 where语句的应用 通过mysqli删除数据 php mysqli 操作数据库 连接数据库 在php5.3版本之后,想要连接数据库有两种方案,一种是通过mysqli,另外外一种是通过pdo.本文主要说的是如果通过mysqli面向对象的写法操作数据库. 在连接数据库之前,需要先通过变量存储好数据库的基本连接信息. $serv

mysqli操作数据库

一. 建立和断开连接 与MySQL数据库交互时,首先要建立连接,最后要断开连接,这包括与服务器连接并 选择一个数据库,以及最后关闭连接.与mysqli几乎所有的特性一样,这一点可以使用面向 对象的方法来完成,也可以采用过程化的方式完成. 1.创建一个mysqli的对象 $_mysqli = new mysqli(); 2.连接MySQL的主机.用户.密码.数据库 $_mysqli->connect('localhost','root','yangfan','guest'); 3.创建带连接参数的

在PHP中使用Mysqli操作数据库

PHP的 mysqli 扩展提供了其先行版本的所有功能,此外,由于 MySQL 已经是一个 具有完整特性的数据库服务器 , 这为PHP 又添加了一些新特性 . 而 mysqli 恰恰也支持了 这些新特性. 一. 建立和断开连接 与 MySQL数据库交互时,首先要建立连接,最后要断开连接,这包括与服务器连接并 选择一个数据库 , 以及最后关闭连接 .与 mysqli 几乎所有的特性一样 , 这一点可以使用面向 对象的方法来完成,也可以采用过程化的方式完成. 1. 创建一个 mysqli 的对象 $

使用Mysqli操作数据库

一.mysql与mysqli的概念相关: 1.mysql与mysqli都是php方面的函数集,与mysql数据库关联不大. 2. 在php5版本之前,一般是用php的mysql函数去驱动mysql数据库的,比如mysql_query()的函数,属于面向过程3.在php5版本 以后,增加了mysqli的函数功能,某种意义上讲,它是mysql系统函数的增强版,更稳定更高效更安全,与mysql_query()对应的有 mysqli_query(),属于面向对象,用对象的方式操作驱动mysql数据库 二

mysqli 操作数据库(转)

从php5.0开始增加mysql(i)支持 , 新加的功能都以对象的形式添加 i表示改进的意思 功能多.效率高.稳定 编译时参数: ./configure --with-mysql=/usr/bin/mysql_config \ #使用 Mysql ClientLibrary(libmysql)构建 --with-mysqli=mysqlnd \ #使用 Mysql Native Dirver 即mysqlnd --with-pdo-mysql=mysqlnd #使用 Mysql Native

MYSQLI - mysqli操作数据库

<?php //模型类 class Model { //数据库连接 private $_conn = NULL; //where语句 private $_where = NULL; //表名称 private $_tableName = NULL; //构造方法,接收表名称 public function __construct($tabName){ //给属性赋值 $this->_tableName = $tabName; //连接数据库 $this->_conn = mysqli_c

mysqli操作mysql数据库(面向对象风格)

//1.创建MySQLi对象 echo "<meta charset='utf-8'>"; $mysqli=new MySQLi("localhost","root","root","test"); if($mysqli->connect_error){ die("连接失败".$mysqli->connect_error); } //2.操作数据库(发送sql)

PHP基础(13)使用mysqli操作MySQL数据库

mysqli提供了两种接口操作数据库MySQL 面向过程 2.  面向对象 <?php /*面向过程*/ $mysqli = mysqli_connect("localhost", "root", "123456", "mydb"); if (mysqli_connect_errno($mysqli)) {     echo "Failed to connect to MySQL: " . mysql