<?php
$link = mysqli_connect('127.0.0.1', 'root', 'root');
if (!is_null(mysqli_connect_error())) {
die(mysqli_connect_error());
}
mysqli_select_db($link, 'school');
mysqli_set_charset($link, 'utf8');
$sql = "insert into user values(1, '張三','男',21),(2,'李思','女',22)";
$result = mysqli_query($link, $sql);
if ($result) {
echo "執行成功! 成功插入了 " . mysqli_affected_rows($link) . " 條數據!";
} else {
echo "執行失敗!";
}
mysqli_close($link);
複製代碼
<?php
$link = mysqli_connect('127.0.0.1', 'root', 'root');
if (!is_null(mysqli_connect_error())) {
die(mysqli_connect_error());
}
mysqli_select_db($link, 'school');
mysqli_set_charset($link, 'utf8');
$sql = "insert into user values(3, '張三','男',33)";
$result = mysqli_query($link, $sql);
if ($result) {
echo "執行成功! 成功插入了 " . mysqli_affected_rows($link) . " 條數據!<br> 插入的id爲".mysqli_insert_id($link);
} else {
echo "執行失敗!";
}
mysqli_close($link);
複製代碼
<?php
$link = mysqli_connect('127.0.0.1', 'root', 'root');
if (!is_null(mysqli_connect_error())) {
die(mysqli_connect_error());
}
mysqli_select_db($link, 'school');
mysqli_set_charset($link, 'utf8');
$sql = "update user set password = 'password' where id > 4";
$result = mysqli_query($link, $sql);
if ($result) {
echo "執行成功! 成功更新了 " . mysqli_affected_rows($link) . " 條數據!";
} else {
echo "執行失敗!";
}
mysqli_close($link);
複製代碼
<?php
$link = mysqli_connect('127.0.0.1', 'root', 'root');
if (!is_null(mysqli_connect_error())) {
die(mysqli_connect_error());
}
mysqli_select_db($link, 'school');
mysqli_set_charset($link, 'utf8');
$sql = "delete from user where id > 4";
$result = mysqli_query($link, $sql);
if ($result) {
echo "執行成功! 成功刪除了 " . mysqli_affected_rows($link) . " 條數據!";
} else {
echo "執行失敗!";
}
mysqli_close($link);
複製代碼
查詢條件很簡單的查詢, 就叫簡單查詢, 沒有條件或者條件單一...php
<?php
$link = mysqli_connect('127.0.0.1', 'root', 'root');
if (!is_null(mysqli_connect_error())) {
die(mysqli_connect_error());
}
mysqli_select_db($link, 'school');
mysqli_set_charset($link, 'utf8');
$sql = "select * from user";
$result = mysqli_query($link, $sql);
if ($result) {
$data = mysqli_fetch_array($result);
var_dump($data);
} else {
echo "執行失敗!";
}
mysqli_close($link);
複製代碼
mysqli_fetch_array()
mysqli_fetch_array()
執行一次, 向後移一位while($row = mysqli_fetch_array($result)){
var_dump($row);
}
複製代碼
<?php
$link = mysqli_connect('127.0.0.1', 'root', 'root');
if (!is_null(mysqli_connect_error())) {
die(mysqli_connect_error());
}
mysqli_select_db($link, 'school');
mysqli_set_charset($link, 'utf8');
$sql = "select * from user";
$result = mysqli_query($link, $sql);
if ($result) {
while($row = mysqli_fetch_array($result)){
var_dump($row);
}
} else {
echo "執行失敗!";
}
mysqli_close($link);
複製代碼
mysqli_fetch_row()
mysqli_fetch_array($link,MYSQLI_NUM)
mysqli_fetch_all()
<?php
$link = mysqli_connect('127.0.0.1', 'root', 'root');
if (!is_null(mysqli_connect_error())) {
die(mysqli_connect_error());
}
mysqli_select_db($link, 'school');
mysqli_set_charset($link, 'utf8');
$sql = "select * from user";
$result = mysqli_query($link, $sql);
if ($result) {
$data = mysqli_fetch_all($result);
var_dump($data);
} else {
echo "執行失敗!";
}
mysqli_close($link);
複製代碼
MYSQLI_ASSOC
, 來返回關聯數組mysqli_num_rows()
獲取查詢到的行數mysqli_num_fields()
獲取查詢到的列數create table booktype(
typeid int not null primary key,
typename varchar(20) null
);
create table book(
bookid char(10) not null PRIMARY key,
bookname VARCHAR(20) not null,
typeid INT,
bookauthor VARCHAR(20),
bookpublisher VARCHAR(50),
bookprice DOUBLE,
borrowsum int,
FOREIGN key(typeid) REFERENCES booktype(typeid)
);
insert into booktype values
(1,'天然科學'),
(2,'數學'),
(3,'計算機'),
(4,'建築水利'),
(5,'旅遊地理'),
(6,'勵志/自我實現'),
(7,'工業技術'),
(8,'基礎醫學'),
(9,'室內設計'),
(10,'人文景觀');
insert into book values
('TP39/1712','Java程序設計',3,'陳永紅','機械工業出版社',35.5,30),
('013452','離散數學',2,'張小新','機械工業出版社',45.5,10),
('TP/3452','JSP程序設計案例',3,'劉城清','電子工業出版社',42.8,8),
('TH/2345','機械設計手冊',7,'黃明凡','人民郵電出版社',40,10),
('R/345677','中醫的故事',8,'李奇德','國防工業出版社',20.0,5);
複製代碼
mysqli_error()
獲取sql語句執行的錯誤信息mysqli_errno()
獲取錯誤碼<?php
$error_arr = [
1054=>'很是抱歉, 可能您的輸入有誤, 請檢查'
];
$link = mysqli_connect('127.0.0.1','root','root');
if (!is_null(mysqli_connect_error())) {
die(mysqli_connect_error());
}
mysqli_select_db($link,'library');
$sql = "select sids from student";
$result = mysqli_query($link,$sql);
if($result){
echo "成功";
}else{
echo $error_arr[mysqli_errno($link)];
}
mysqli_close($link);
複製代碼