14c08-1.php
来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 36 行
PHP
36 行
<?php// Open the database:if (!($db = new PDO('sqlite2:tools.db'))) { // Handle errors $errinfo = $db->errorInfo(); die('SQL ERROR: Open failed: ' . $errinfo[2]);}// Prepare a SQL insert: assume we received the description from the user$desc = 'Jointer';$escaped = str_replace("'", "''", $desc);$sql = "insert into tools (desc, qty) values ('{$escaped}', 0)";// Do the insertif (!($db->query($sql))) { // Give an error statement: $errinfo = $db->errorInfo(); die('SQL INSERT ERROR: ' . $errinfo[2] . " - Query was: {$sql}");}// Select data from the table:$sql = 'select id, desc from tools order by id asc';if (!($result = $db->query($sql))) { // Give an error statement: $errinfo = $db->errorInfo(); die('SQL SELECT ERROR: ' . $errinfo[2] . " - Query was: {$sql}");}// Read all the data back in as associative arrayswhile ($row = $result->fetch()) { echo "{$row['id']}. {$row['desc']}<br />\n";}// Now close the connectionunset($db);?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?