其实今天没有欲望..-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();

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to set up keyboard startup on Gigabyte's motherboard. First, if it needs to support keyboard startup, it must be a PS2 keyboard! ! The setting steps are as follows: Step 1: Press Del or F2 to enter the BIOS after booting, and go to the Advanced (Advanced) mode of the BIOS. Ordinary motherboards enter the EZ (Easy) mode of the motherboard by default. You need to press F7 to switch to the Advanced mode. ROG series motherboards enter the BIOS by default. Advanced mode (we use Simplified Chinese to demonstrate) Step 2: Select to - [Advanced] - [Advanced Power Management (APM)] Step 3: Find the option [Wake up by PS2 keyboard] Step 4: This option The default is Disabled. After pulling down, you can see three different setting options, namely press [space bar] to turn on the computer, press group

1. Processor When choosing a computer configuration, the processor is one of the most important components. For playing games like CS, the performance of the processor directly affects the smoothness and response speed of the game. It is recommended to choose Intel Core i5 or i7 series processors because they have powerful multi-core processing capabilities and high frequencies, and can easily cope with the high requirements of CS. 2. Graphics card Graphics card is one of the important factors in game performance. For shooting games such as CS, the performance of the graphics card directly affects the clarity and smoothness of the game screen. It is recommended to choose NVIDIA GeForce GTX series or AMD Radeon RX series graphics cards. They have excellent graphics processing capabilities and high frame rate output, and can provide a better gaming experience. 3. Memory power

SPDIFOUT connection line sequence on the motherboard. Recently, I encountered a problem regarding the wiring sequence of the wires. I checked online. Some information says that 1, 2, and 4 correspond to out, +5V, and ground; while other information says that 1, 2, and 4 correspond to out, ground, and +5V. The best way is to check your motherboard manual. If you can't find the manual, you can use a multimeter to measure it. Find the ground first, then you can determine the order of the rest of the wiring. How to connect motherboard VDG wiring When connecting the VDG wiring of the motherboard, you need to plug one end of the VGA cable into the VGA interface of the monitor and the other end into the VGA interface of the computer's graphics card. Please be careful not to plug it into the motherboard's VGA port. Once connected, you can

Glodon Software is a software company focusing on the field of building informatization. Its products are widely used in all aspects of architectural design, construction, and operation. Due to the complex functions and large data volume of Glodon software, it requires high computer configuration. This article will elaborate on the computer configuration recommendations of Glodon Software from many aspects to help readers choose a suitable computer configuration processor. Glodon Software requires a large amount of data calculation and processing when performing architectural design, simulation and other operations. Therefore, the requirements for the processor are higher. It is recommended to choose a multi-core, high-frequency processor, such as Intel i7 series or AMD Ryzen series. These processors have strong computing power and multi-thread processing capabilities, and can better meet the needs of Glodon software. Memory Memory is affecting computing

Which version of the graphics card driver is best to use? 1. There is no absolute best version. It is most important to choose the version that suits your computer; 2. Because the applicability and stability of the graphics card driver version are related to the computer hardware environment and system configuration; 3. You can check the detailed information of the computer and graphics card on the official website, select the appropriate driver version based on the information, or refer to the reviews of other users. It is recommended to back up the system before installing the driver to avoid unexpected situations. Graphics card driver version 472.19 series is an excellent choice. Currently, the driver compatibility of version 472 is the best. Installing version 472 of the driver can also maximize the performance of the graphics card. The NVIDIA graphics card driver Win7 installation version, numbered 2, 472.19, is a product with remarkable quality.

DeepSeek released a technical article on Zhihu, introducing its DeepSeek-V3/R1 inference system in detail, and disclosed key financial data for the first time, which attracted industry attention. The article shows that the system's daily cost profit margin is as high as 545%, setting a new high in global AI big model profit. DeepSeek's low-cost strategy gives it an advantage in market competition. The cost of its model training is only 1%-5% of similar products, and the cost of V3 model training is only US$5.576 million, far lower than that of its competitors. Meanwhile, R1's API pricing is only 1/7 to 1/2 of OpenAIo3-mini. These data prove the commercial feasibility of the DeepSeek technology route and also establish the efficient profitability of AI models.

Please recommend which 1155-pin CPU is the best. The current 1155-pin CPU with the highest performance is Intel Corei7-3770K. It has 4 cores and 8 threads, a base frequency of 3.5GHz, and supports TurboBoost2.0 technology, which can reach up to 3.9GHz. In addition, it is equipped with 8MB of level 3 cache and is an excellent processor with the LGA1155 pin, the most powerful CPU Intel Core i73770K. The LGA1155 interface is the interface type used by second and third generation Core processors. The best performing one is Intel Core i73770K. The parameters of this processor are as follows: 1. Applicable type: desktop; 2. CPU series: Core i7; 3. CPU

I am planning to go backpacking in Tibet. ① How many liters of bag should I carry? Please tell me what you think is the best configuration. I am 170 and have good physical strength. The first time I went hiking, the amount was 60 liters or more. The amount of hiking was less than 60 liters. The entire journey was by car. You don’t need a backpack, a suitcase is more convenient. If you really need to carry something with you, a 25-40 liter bag is more than enough. Necessary supplies for Tibet travel: sunglasses, sun hat, sunscreen, skin cream, lip balm, long-sleeved top, Sweater; for special travel or travel to Ali, northern Tibet, and Sichuan-Tibet line, it is recommended to bring: sleeping bag (cold protection), sheets (dirty protection), down jacket, travel shoes or hiking shoes, slippers, toothbrush, toothpaste, towel, rolling paper , paper underwear, disinfectant wipes, flashlight, waterproof matches, knives, rope. Can a computer be carried in the front bag? Can a computer be carried in the front bag? Some backpacks have it.
