1连接数据库.
//procedural style
$mysqli = mysqli_connect('host','username','password','database_name'); //object oriented style (recommended)
$mysqli = new mysqli('host','username','password','database_name'); 推荐下面的方式 <?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_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.
<?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();
?>
.选择
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.
<?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();
?>
.选择
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.
<?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();
?>
.选择单行
<?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();
?>
.选择行数
<?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();
?>
.选择预处理
$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();
$search_ID = ;
$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();
.插入数据库
<?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('').'"'; //MySqli Insert Query
$insert_row = $mysqli->query("INSERT INTO products (product_code, product_name, price) VALUES($product_code, $product_name, $product_price)"); if($insert_row){
print 'Success! ID of last inserted record is : ' .$mysqli->insert_id .'<br />';
}else{
die('Error : ('. $mysqli->errno .') '. $mysqli->error);
} ?>
.插入预处理
//values to be inserted in database table
$product_code = 'P1234';
$product_name = '42 inch TV';
$product_price = ''; $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();
.批量插入
//product 1
$product_code1 = '"'.$mysqli->real_escape_string('P1').'"';
$product_name1 = '"'.$mysqli->real_escape_string('Google Nexus').'"';
$product_price1 = '"'.$mysqli->real_escape_string('').'"'; //product 2
$product_code2 = '"'.$mysqli->real_escape_string('P2').'"';
$product_name2 = '"'.$mysqli->real_escape_string('Apple iPad 2').'"';
$product_price2 = '"'.$mysqli->real_escape_string('').'"'; //product 3
$product_code3 = '"'.$mysqli->real_escape_string('P3').'"';
$product_name3 = '"'.$mysqli->real_escape_string('Samsung Galaxy Note').'"';
$product_price3 = '"'.$mysqli->real_escape_string('').'"'; //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);
}
.更新删除
//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;
}
.预处理
$product_name = '52 inch TV';
$product_code = '';
$find_id = ; $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;
}
.删除 //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;
}

PHP MysqlI操作数据库的更多相关文章

  1. php 通过mysqli 操作数据库mysql

    目录 php mysqli 操作数据库 连接数据库 通过mysqli 创建数据库 通过mysqi 创建数据表 通过mysqli向数据表中插入信息 通过mysqli 读取数据 where语句的应用 通过 ...

  2. 比Mysqli操作数据库更简便的方式 。PDO

    下面来说一下PDO 先画一张图来了解一下 mysqli是针对mysql这个数据库扩展的一个类 PDO是为了能访问更多数据库 如果出现程序需要访问其他数据库的话就可以用PDO来做 PDO数据访问抽象层1 ...

  3. mysqli操作数据库

    1 连接数据库:可以使用对象或函数来连接(我们这里主要用mysqli对象,附带着函数连接) //创建mysqli对象(也可以叫做资源句柄) $_mysqli = new mysqli(); //连接数 ...

  4. 在PHP中使用Mysqli操作数据库

    PHP的 mysqli 扩展提供了其先行版本的所有功能,此外,由于 MySQL 已经是一个 具有完整特性的数据库服务器 , 这为PHP 又添加了一些新特性 . 而 mysqli 恰恰也支持了 这些新特 ...

  5. 在PHP中使用MySQL Mysqli操作数据库 ,以及类操作方法

    先来操作函数部分,普遍的MySQL 函数方法,但随着PHP5的发展,有些函数使用的要求加重了,有些则将废弃不用,有些则参数必填... ================================= ...

  6. mysqli 操作数据库(转)

    从php5.0开始增加mysql(i)支持 , 新加的功能都以对象的形式添加 i表示改进的意思 功能多.效率高.稳定 编译时参数: ./configure --with-mysql=/usr/bin/ ...

  7. mysqli 操作数据库

    从php5.0开始增加mysql(i)支持 , 新加的功能都以对象的形式添加 i表示改进的意思 功能多.效率高.稳定 编译时参数: ./configure --with-mysql=/usr/bin/ ...

  8. PHP MysqlI操作数据库(转)

    1连接数据库. Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter. ...

  9. MYSQLI - mysqli操作数据库

    <?php //模型类 class Model { //数据库连接 private $_conn = NULL; //where语句 private $_where = NULL; //表名称 ...

随机推荐

  1. vs2017预览版下载

    vs2017预览版,没有限制的不过不能生成生产版本,集成最新的功能! 下载地址: https://www.visualstudio.com/zh-hans/vs/preview/

  2. 汇编入门学习笔记 (七)—— dp,div,dup

    疯狂的暑假学习之  汇编入门学习笔记 (七)--  dp.div.dup 參考: <汇编语言> 王爽 第8章 1. bx.si.di.和 bp 8086CPU仅仅有4个寄存器能够用 &qu ...

  3. 在ListView的右边添加字母列表

    在ListView的右边添加字母列表,点击某个字母时,列表就滚动到预期位置. <!-- 数字和字母栏在标题栏下边并且停靠在右边 --> <com.txrj.sms.component ...

  4. 【laravel54】如果开启了自带的时间戳(Y-h-m H:s:m),getInsertId一定要手动加上created_at 和 updated_at字段填充

    [laravel54]如果开启了自带的时间戳(Y-h-m H:s:m),getInsertId一定要手动加上created_at 和 updated_at字段填充

  5. IntelliJ Idea解决Could not autowire. No beans of 'xxxx' type found的错误提示

    本文转自:http://blog.csdn.net/u012453843/article/details/54906905 1.问题描述 在Idea的spring工程里,经常会遇到Could not ...

  6. HDUOJ---1133(卡特兰数扩展)Buy the Ticket

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  7. pandas 的数据结构Series与DataFrame

    pandas中有两个主要的数据结构:Series和DataFrame. [Series] Series是一个一维的类似的数组对象,它包含一个数组数据(任何numpy数据类型)和一个与数组关联的索引. ...

  8. java提高篇之详解内部类

    可以将一个类的定义放在另一个类的定义内部,这就是内部类. 内部类是一个非常有用的特性但又比较难理解使用的特性(鄙人到现在都没有怎么使用过内部类,对内部类也只是略知一二). 第一次见面 内部类我们从外面 ...

  9. 如何生成KeyStore

    介绍如何生成keystore cmd下: 进入到jdk的bin目录,这样的话,android.keystore文件就会生成在这个目录下,签名的时候我们需要这个文件. C:\Program Files\ ...

  10. Objective-C中的@Property具体解释

    Objective-C中的@Property具体解释 @Property (属性) class vairs 这个属性有nonatomic. strong. weak, retain. copy等等 我 ...