建庫, 建表
create database demo_db character set utf8;
use demo_db;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(25) NOT NULL,
`age` tinyint(1) NOT NULL,
`address` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
複製代碼
插入測試數據
INSERT INTO `demo_db`.`users`(`id`, `name`, `age`, `address`) VALUES (1, '張三', 18, '北京東城區');
INSERT INTO `demo_db`.`users`(`id`, `name`, `age`, `address`) VALUES (2, '李四', 22, '上海徐彙區');
INSERT INTO `demo_db`.`users`(`id`, `name`, `age`, `address`) VALUES (3, '王五', 23, '鄭州金水區');
INSERT INTO `demo_db`.`users`(`id`, `name`, `age`, `address`) VALUES (4, '趙六', 24, '北京西城區');
複製代碼
mysqli教程
小彩蛋(已經鏈接數據庫, 可是忘記了密碼)
- 找到mysql下的user表
- 算出來新密碼對應的加密值
- 刷新權限
flush PRIVILEGES;
- 測試鏈接
使用mysqli操做數據庫
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$conn = mysqli_connect($servername, $username, $password);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "鏈接成功";
複製代碼
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "demo_db";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "鏈接成功";
mysqli_query($conn,"SET NAMES utf8");
$sql = "select * from users";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
print_r($row);
}
} else {
echo "0 結果";
}
複製代碼
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "demo_db";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "鏈接成功";
mysqli_query($conn,"SET NAMES utf8");
$sql = "insert into users(name,age,address) values('孫七',21,'北京朝陽區')";
$result = mysqli_query($conn, $sql);
if ($result) {
echo "新記錄插入成功";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
複製代碼
- 批量插入數據
- 使用
.=
把多條sql語句鏈接起來, 注意每條語句, 分號結束
- 使用
mysqli_multi_query()
函數
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "demo_db";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "鏈接成功";
mysqli_query($conn,"SET NAMES utf8");
$sql = "insert into users(name,age,address) values('周八',22,'北京海淀區');";
$sql .= "insert into users(name,age,address) values('吳九',23,'北京石景山區');";
$sql .= "insert into users(name,age,address) values('鄭十',24,'北京雄安新區');";
$result = mysqli_multi_query($conn, $sql);
if ($result) {
echo "新記錄插入成功";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
複製代碼
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "demo_db";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "鏈接成功";
mysqli_query($conn,"SET NAMES utf8");
$sql = "update users set age = 28 where name = '張三'";
$result = mysqli_query($conn, $sql);
if ($result) {
echo "修改爲功";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
複製代碼
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "demo_db";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "鏈接成功";
mysqli_query($conn,"SET NAMES utf8");
$sql = "delete from users where name = '張三'";
$result = mysqli_query($conn, $sql);
if ($result) {
echo "刪除成功";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
複製代碼
把原來的博客內容保存數據庫
CREATE TABLE `blogs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`date` date NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
複製代碼
修改原來的代碼
<?php
require 'vendor/autoload.php';
use QL\QueryList;
$ql = new QueryList();
function get_tilte_date($url) {
global $ql;
return $ql->get($url)->rules([
'date' => ['header > div > a > time.entry-date.published', 'text'],
'title' => ['header > h2 > a', 'text'],
'url' => ['header > h2 > a', 'href'],
])->queryData();
}
function get_content($url) {
global $ql;
return $ql->get($url)->find('article.post.type-post.status-publish.format-standard.hentry.category-uncategorized')->text();
}
function save_data($content_array) {
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "demo_db";
$conn = mysqli_connect($servername, $username, $password, $dbname);
mysqli_query($conn,"SET NAMES utf8");
$sql = '';
foreach ($content_array as $key => $value) {
$title = $value['title'];
$date = $value['date'];
$content = $value['content'];
$sql .= "insert into blogs(title,date,content) values(\"{$title}\",\"{$date}\",'{$content}');";
}
$result = mysqli_multi_query($conn,$sql);
var_dump($result);
if ($result) {
echo "寫入成功";
}else{
echo "寫入失敗!!!";
}
}
function start($url) {
$data = get_tilte_date($url);
foreach ($data as $key => $value) {
$data[$key]['content'] = get_content($value['url']);
}
save_data($data);
}
start("https://wordpress-edu-3autumn.localprod.forc.work/");
複製代碼
做業
下一節