search
Homephp教程php手册其实今天没有欲望..-MySQLi,欲望..-MySQLi

其实今天没有欲望..-MySQLi,欲望..-MySQLi

  hi

中午爽爽的游了会儿泳,但是下午把一拳超人看完了,竟然萌生不出学习的欲望了。。。强迫自己更新点东西吧,一会儿看书去。

1、MySQLi

二、MySQLi基于OOP的编程

2.1 使用解析

--基本

MySQLi是一个拓展类库,本质上是个类(?)。

一般流程和MySQL一样:连接,选库,字符集设定,SQL语句执行,关闭连接

--链接库例子

/*
* 连接和选库
*/
$mysqli=new mysqli('localhost', 'root', '');
print_r($mysqli);echo "
";

echo $mysqli->select_db('test');echo "
";

$mysqli2=new mysqli();
print_r($mysqli2->connect('localhost', 'root', ''));echo "
";

print_r($mysqli3=new mysqli('localhost', 'root', '','test'));echo "
";

三种不同的方法,这里的方法都是利用mysqli的类属性来做的;当然也可以利用mysqli的命令来链接;

$con=mysqli_connect(HOST,USERNAME,PASSWORD)

结果中有一些信息

mysqli Object
  (
  [affected_rows] => 0
  [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $
  [client_version] => 50011
  [connect_errno] => 0
  [connect_error] =>
  [errno] => 0
  [error] =>
  [error_list] => Array
  (
  )
   
  [field_count] => 0
  [host_info] => localhost via TCP/IP
  [info] =>
  [insert_id] => 0
  [server_info] => 5.6.17
  [server_version] => 50617
  [stat] => Uptime: 968 Threads: 1 Questions: 24 Slow queries: 0 Opens: 70 Flush tables: 1 Open tables: 63 Queries per second avg: 0.024
  [sqlstate] => 00000
  [protocol_version] => 10
  [thread_id] => 11
  [warning_count] => 0
  )

这些属性都是可以通过对象的属性来得到的,比如

echo $mysqli->client_info;echo "
";

或者是通过相对应的方法得到,这些东西查手册可以看到。

header('content-type:text/html;charset=utf-8');
//1.建立到MySQL数据的连接
// $mysqli=new mysqli('localhost','root','root');
// //print_r($mysqli);
// //2.打开指定的数据库
// $mysqli->select_db('test');
// $mysqli=new mysqli();
// $mysqli->connect('127.0.0.1','root','root');
// print_r($mysqli);

//建立连接的同时打开指定数据库
$mysqli=@new mysqli('localhost','root','root','test');
//print_r($mysqli);
//$mysqli->connect_errno:得到连接产生的错误编号
//$mysqli->connect_error:得到连接产生的错误信息
if($mysqli->connect_errno){
die('Connect Error:'.$mysqli->connect_error);
}
print_r($mysqli);
echo '


';
echo '客户端的信息:'.$mysqli->client_info.'
';
echo $mysqli->get_client_info().'
';
echo '客户端的版本:'.$mysqli->client_version.'
';
echo '
';
echo '服务器端信息:'.$mysqli->server_info.'
';
echo $mysqli->get_server_info();
echo '
';
echo '服务器版本:'.$mysqli->server_version.'
';

echo '


';

--字符集例子

//1.建立到MySQL的连接
$mysqli=@new mysqli('localhost','root','root','test');
if($mysqli->connect_errno){
die('Connect Error:'.$mysqli->connect_error);
}
//2.设置默认的客户端编码方式utf8
$mysqli->set_charset('utf8');

//3.执行SQL查询
$sql= CREATE TABLE IF NOT EXISTS mysqli(
id TINYINT UNSIGNED AUTO_INCREMENT KEY,
username VARCHAR(20) NOT NULL
);
EOF;
$res=$mysqli->query($sql);
var_dump($res);

/*
SELECT/DESC/DESCRIBE/SHOW/EXPLAIN执行成功返回mysqli_result对象,执行失败返回false
对于其它SQL语句的执行,执行成功返回true,否则返回false
*/
//关闭连接
$mysqli->close();

需要注意的是数据库中的是utf8,而不是utf-8;

2.2 插入记录操作

增。

--connect.php

因为连接数据库的一系列操作是常用的,对于这种,我们简便的方法就是封装起来,到处调用

require_once 'connect.php';

connect.php

/*
* 连接和选库(头)文件
*/
$mysqli=new mysqli('localhost', 'root', '','test');
if($mysqli->connect_errno){
die('Connect Error:'.$mysqli->connect_error);
}else{
echo '客户端的信息:'.$mysqli->client_info.'
';
}
$mysqli->set_charset('utf8');

--增

/*
* 数据库插入数据
*/

require_once 'connect.php';

$sql="insert mysqli(username) value('Tom')";
echo $mysqli->query($sql);

这里执行的是单条的sql语句。

或者完善一点,添加一个判断,并输出错误信息。

if($res){
echo $mysqli->insert_id;
}else{
echo 'ERROR '.$mysqli->error;
}

或者,插入多条记录

$sql="insert mysqli(username) value('Sdaf'),('Andy')";

2.3 更新记录

更新。

$sql="update test set id=id+10";
$mysqli->query($sql);

2.4 删除

$sql="delete from mysqli where id>=2";

--

特别说明,affected_rows返回的情况有三种:

-1  sql语句有问题;

0 没有受影响的语句;

>=0 受影响的条数。

--小结

header('content-type:text/html;charset=utf-8');
$mysqli=new mysqli('localhost','root','root','test');
if($mysqli->connect_errno){
die('CONNECT ERROR:'.$mysqli->connect_error);
}
$mysqli->set_charset('utf8');

//执行SQL查询
//添加记录
//执行单条SQL语句,只能执行一条SQL语句
// $sql="INSERT user(username,password) VALUES('king','king');";
// $sql.="DROP TABLE user;";
$sql="INSERT user(username,password) VALUES('queen1','queen1'),('queen2','queen2'),('queen3','queen3'),('queen4','queen4')";
$res=$mysqli->query($sql);
if($res){
//得到上一插入操作产生的AUTO_INCREMENT的值
echo '恭喜您注册成功,您是网站第'.$mysqli->insert_id.'位用户
';
//得到上一步操作产生的受影响记录条数
echo '有'.$mysqli->affected_rows.'记录被影响';
}else{
//得到上一步操作产生的错误号和错误信息
echo 'ERROR '.$mysqli->errno.':'.$mysqli->error;
}
echo '


';

//将表中年龄+10
$sql="UPDATE user SET age=age+10";
$res=$mysqli->query($sql);
if($res){
echo $mysqli->affected_rows.'条记录被更新';
}else{
echo "ERROR ".$mysqli->errno.':'.$mysqli->error;
}
echo '


';

//将表中id$sql="DELETE FROM user WHERE id$res=$mysqli->query($sql);
if($res){
echo $mysqli->affected_rows.'条记录被删除';
}else{
echo "ERROR ".$mysqli->errno.':'.$mysqli->error;
}
//关闭到MySQL的连接
$mysqli->close();

2.5 查

需要注意的是,用的是select,所以返回的是结果集,是可以打印出来的print_r或var_dump。

所以这里就要说一下,返回的结果集的选择了。

header('content-type:text/html;charset=utf-8');
$mysqli=new mysqli('localhost','root','root','test');
if($mysqli->connect_errno){
die('CONNECT ERROR:'.$mysqli->connect_error);
}
$mysqli->set_charset('utf8');
$sql="SELECT id,username,age FROM user";
$mysqli_result=$mysqli->query($sql);
//var_dump($mysqli_result);
if($mysqli_result && $mysqli_result->num_rows>0){
//echo $mysqli_result->num_rows;
//$rows=$mysqli_result->fetch_all();//获取结果集中所有记录,默认返回的是二维的
//索引+索引的形式
//$rows=$mysqli_result->fetch_all(MYSQLI_NUM);
//$rows=$mysqli_result->fetch_all(MYSQLI_ASSOC);
//$rows=$mysqli_result->fetch_all(MYSQLI_BOTH);
// $row=$mysqli_result->fetch_row();//取得结果集中一条记录作为索引数组返回
// print_r($row);
// echo '


';
// $row=$mysqli_result->fetch_assoc();//取得结果集中的一条记录作为关联数组返回
// print_r($row);
// echo '
';
// $row=$mysqli_result->fetch_array();//二者都有的
// print_r($row);

// echo '
';
// $row=$mysqli_result->fetch_array(MYSQLI_ASSOC);
// print_r($row);

// echo '
';
// $row=$mysqli_result->fetch_object();
// print_r($row);
// echo '
';
// //移动结果集内部指针
// $mysqli_result->data_seek(0);
// $row=$mysqli_result->fetch_assoc();
// print_r($row);

// print_r($rows);

while($row=$mysqli_result->fetch_assoc()){
//print_r($row);
//echo '


';
$rows[]=$row;
}
print_r($rows);

//释放结果集
$mysqli_result->free();


}else{
echo '查询错误或者结果集中没有记录';
}
$mysqli->close();

 

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools