MYSQLI - mysqli
PHP MysqlI操作数据库
$mysqli = mysqli_connect('host','username','password','database_name');
//object oriented style (recommended)
$mysqli = new mysqli('host','username','password','database_name');
推荐下面的方式
//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.
//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.
//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.
//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.选择单行
//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
$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.选择预处理
//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_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.插入数据库
//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.插入预处理
$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.批量插入
$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.更新删除
$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.预处理
$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.删除
$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;
}
MYSQLI - mysqli的更多相关文章
- MYSQLI - mysqli操作数据库
<?php //模型类 class Model { //数据库连接 private $_conn = NULL; //where语句 private $_where = NULL; //表名称 ...
- mysqli报错(HY000/2002)
Warning: mysqli::mysqli(): (HY000/2002): 没有那个文件或目录 解决:把localhost 换成127.0.0.1就好了.
- SQL注入实验,PHP连接数据库,Mysql查看binlog,PreparedStatement,mysqli, PDO
看到有人说了判断能否sql注入的方法: 简单的在参数后边加一个单引号,就可以快速判断是否可以进行SQL注入,这个百试百灵,如果有漏洞的话,一般会报错. 下面内容参考了这两篇文章 http://blog ...
- mysqli和mysql和pdo查询
mysql mysql_connect($db_host, $db_user, $db_password); mysql_select_db($dn_name); $result = mysql_ ...
- PHP中MySQL、MySQLi和PDO的用法和区别
PHP的MySQL扩展(优缺点) 设计开发允许PHP应用与MySQL数据库交互的早期扩展.mysql扩展提供了一个面向过程 的接口: 并且是针对MySQL4.1.3或更早版本设计的.因此,这个扩展虽然 ...
- PHP中MySQL、MySQLi和PDO的用法和区别【原创】
对于一个初学PHP的自己,对数据库的连接有着很大的疑惑,从Java转到PHP.数据库连接变了,以前只知道JDBC连接数据库,或者直接用框架调用,对于的PHP的数据库连接方式,及其应用.不是很了解,于是 ...
- PHP连接到mysql的方法--mysqli和PDO
php连接到mysql数据库,经典的方式就是使用mysql_connect(),具体代码如下: mysql_connect($db_host, $db_user, $db_pass) or die(m ...
- MYSQL MYSQLI PDO
PHP的MySQL扩展(优缺点) 设计开发允许PHP应用与MySQL数据库交互的早期扩展.mysql扩展提供了一个面向过程 的接口: 并且是针对MySQL4.1.3或更早版本设计的.因此,这个扩展虽然 ...
- Linux装mysqli.so
php 5.2.3+mysqli 安装与常见错误 总结 php 5.2.3+mysqli 安装与常见错误 总结 记得原来在编译php的已经已经加上参数--with-mysql=/usr/local ...
随机推荐
- 下 面 这 条 语 句 一 共 创 建 了 多 少 个 对 象 : String s="a"+"b"+"c"+"d";
javac 编译可以对字符串常量直接相加的表达式进行优化, 不必要等到运行期去进行加法运算处理, 而是在编译时去掉其中的加号, 直接将其编译成一个这些常量相连的结果.题目中的第一行代码被编译器在编译时 ...
- IWebBrowser隐藏滚动条
刚才在项目里看到一个IWebBrowser2,竟然需要通过MoveWindow的方式把滚动条遮挡,如果要缩小IWebBrowser2控件的显示大小呢?这种方法至少我用不习惯,起码也得从源头解决这样的问 ...
- eclipse maven SLF4J: Failed to load class org.slf4j.impl.StaticLoggerBinder
现象:运行eclipse maven build,console 有红色日志如下: SLF4J: Failed to load class "org.slf4j.impl.StaticLog ...
- codeforces 375D . Tree and Queries 启发式合并 || dfs序+莫队
题目链接 一个n个节点的树, 每一个节点有一个颜色, 1是根节点. m个询问, 每个询问给出u, k. 输出u的子树中出现次数大于等于k的颜色的数量. 启发式合并, 先将输入读进来, 然后dfs完一个 ...
- 【算法】求多个数组中的交集(Java语言实现)
简介: 最近在工作中遇到一个问题,需要离线比较两张Mongodb表的差异:大小差异,相同的个数. 所以,我将导出的bson文件转成了json文件(2G以上),一条记录正好是一行. 问题: 因此我将以上 ...
- Round B APAC Test 2017
https://code.google.com/codejam/contest/5254487 A. Sherlock and Parentheses Problem Sherlock and Wat ...
- 模拟美萍加密狗--Rockey2虚拟狗(四)
目录(?)[+] 首先,抱怨一下.学校个破网,似乎把我端口封了,死活分不上IP,也许是是我MAC改的太频繁了,有盗号嫌疑…… 然后,正文开始…… 其实虚拟狗几天前就写完了,可这几天上不了网 ...
- Java中volatile的作用以及用法
volatile让变量每次在使用的时候,都从主存中取.而不是从各个线程的“工作内存”. volatile具有synchronized关键字的“可见性”,但是没有synchronized关键字的“并发正 ...
- Flex 百度地图API使用
今天想看一下Flex中关于地图方面的使用,刚开始看了google map api, 感觉用起来挺麻烦,关键是英文不好,文档读起来费劲,还有密钥神马的~ 那我就试验一下百度地图的接口,文档是中文的. 首 ...
- Extjs 3.0 htmleditor实现插入图片功能
首先感谢前辈们的无私奉献.贴出参考地址 http://zhidao.baidu.com/link?url=Q0ZM405OFNy_xAHSut9TepRJxgXCxFayQttrQz1N82dlA1_ ...