通过:parm的方式来占位 - 插入操作
避免因内容中含有其他符号而引起sql操作报错
$sql=<<
INSERT INTO archives (user_id, title, content, content_desc, time)
VALUES(:user_id, :title, :content, :content_desc, :time);
EOF;
$stmt = $db->prepare($sql); // 传入带有占位符的sql语句
$stmt->bindValue(':user_id', $user_id); // 绑定占位符与参数数据
$stmt->bindValue(':title', $title);
$stmt->bindValue(':content', $content);
$stmt->bindValue(':content_desc', $content_desc);
$stmt->bindValue(':time', $time);
$result = $stmt->execute(); // 执行
pdo实现一次输出所有数据
php:
$db = new PDO("sqlite:./db/blog.db");
$results = $db->query("SELECT * FROM archives ORDER BY id )->fetchAll();
// 用json进行数据交互
$jsondata=json_encode($results);
print_r($jsondata);
js:
function(data){
datas=JSON.parse(data);
for(i in datas){
// 其他操作
}