pdo mysql_PDO MySQL
PDO MySQL
如果文章有成千上萬(wàn)篇,該怎樣保存?
數(shù)據(jù)保存有多種方式,比如單機(jī)文件、單機(jī)數(shù)據(jù)庫(kù)(SQLite)、網(wǎng)絡(luò)數(shù)據(jù)庫(kù)(MySQL、MariaDB)等等。根據(jù)項(xiàng)目來(lái)選擇,做Web一般采用MySQL,本書(shū)也以MySQL為例。
自學(xué):1天。
假設(shè)這個(gè)在線(xiàn)閱讀項(xiàng)目叫做“reader”,需要一個(gè)“文章”表“articles”。建庫(kù)建表代碼如下:
CREATE DATABASE `reader` DEFAULT CHARACTER SET utf8;
USE `reader`;
CREATE TABLE `articles` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`author` varchar(20) DEFAULT NULL,
`title` varchar(50) DEFAULT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
然后修改add_article_submit.php,把文章保存到數(shù)據(jù)庫(kù)中即可。代碼如下:
$input = $_POST;
$dsn = 'mysql:host=127.0.0.1;port=3306;dbname=reader;charset=utf8';
$user = 'root';
$password = '1';
$db = new PDO($dsn, $user, $password); //連接數(shù)據(jù)庫(kù)
$sql = 'INSERT INTO `articles` (`author`, `title`, `content`) VALUES (' . '\'' . $input['author'] . '\',\'' . $input['title'] . '\',\'' . $input['content'] . '\');';
$stmt = $db->query($sql); //執(zhí)行SQL
$id = $db->lastInsertId(); //獲得自增id
if (!empty($id)) {
$notice = '保存成功';
} else {
$notice = '出錯(cuò)了';
}
$d = array();
$d['notice'] = array(
'msg' => '保存成功',
);
require_once __DIR__ . '/notice.html';
然后首頁(yè)index.php把這些文章查出來(lái)即可,代碼如下:
$dsn = 'mysql:host=127.0.0.1;port=3306;dbname=reader;charset=utf8';
$user = 'root';
$password = '1';
$db = new PDO($dsn, $user, $password);
$sql = 'SELECT `id`, `author`, `title`, `content` FROM `articles` LIMIT 10';
$stmt = $db->query($sql);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$articles = $stmt->fetchAll();
$d = array();
$d['articles'] = $articles;
require_once __DIR__ . '/index.html';
然后單篇閱讀頁(yè)get_article.php,根據(jù)id,查出一篇即可,這樣比查出來(lái)所有文章合理多了。代碼如下:
$input = $_GET;
$d = array();
if (!isset($input['id']) || empty($input['id'])) {
$d['notice'] = array(
'msg' => '出錯(cuò)了:缺少參數(shù)',
);
require __DIR__ . '/notice.html';
exit;
}
$dsn = 'mysql:host=127.0.0.1;port=3306;dbname=reader;charset=utf8';
$user = 'root';
$password = '1';
$db = new PDO($dsn, $user, $password);
$sql = 'SELECT `author`, `title`, `content` FROM `articles` WHERE id=' . $input['id'] . ' LIMIT 1';
$stmt = $db->query($sql);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$r = $stmt->fetchAll();
if (empty($r)) {
$d['notice'] = array(
'msg' => '出錯(cuò)了:查無(wú)此文',
);
require __DIR__ . '/notice.html';
exit;
}
$d = array();
$d['article'] = $r[0];
require_once __DIR__ . '/get_article.html';
現(xiàn)在可以發(fā)表、查看首頁(yè)、單篇閱讀,功能都實(shí)現(xiàn)了。
總結(jié)一下
我的技術(shù)水平
| 語(yǔ)義化 | 讓內(nèi)容動(dòng)起來(lái) | 單機(jī)文件 | GET、POST | PC + Windows | 0.2 | |
| 表現(xiàn)與業(yè)務(wù)分離 | charset | Unicode | 0.3 | |||
| PDO | MySQL | 0.4 |
已解決的問(wèn)題
如何用PHP操作MySQL數(shù)據(jù)庫(kù)?
使用PDO即可。
PDO、php_mysqli和php_mysql的區(qū)別是什么?
請(qǐng)自學(xué)了解。注意:php_mysql已廢棄。
沒(méi)錢(qián),對(duì)現(xiàn)在的年輕人意味著什么?
luckystar神探:來(lái),講個(gè)故事。 從前有個(gè)騷年叫小明。不是姚小明,不是黃小明,也不是郭小明。就是一個(gè)普普通通的、扎人堆里找不出來(lái)的小明。 男,二十多歲,家境普通。口頭禪是「還不是因?yàn)槔献痈F」。 高中時(shí),和… http://zhi.hu/1P8D(分享自知乎)
待解決的問(wèn)題
index.php、get_article.php等多個(gè)文件里都連了數(shù)據(jù)庫(kù),如果密碼變了,每個(gè)地方都要改,怎么辦?
且聽(tīng)下回分解。
訪問(wèn)index.php是正常網(wǎng)頁(yè),但訪問(wèn)index.html看到了什么?
請(qǐng)進(jìn)行實(shí)驗(yàn)。且聽(tīng)下回分解。
單引號(hào)能保存嗎?會(huì)導(dǎo)致什么后果?
請(qǐng)按照截圖進(jìn)行實(shí)驗(yàn)。且聽(tīng)下回分解。
單引號(hào)實(shí)驗(yàn)保存1
單引號(hào)實(shí)驗(yàn)保存2
總結(jié)
以上是生活随笔為你收集整理的pdo mysql_PDO MySQL的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 铁路12306app怎么看退票记录? 铁
- 下一篇: mysql查询语句4,MySQL(4):