14c01-2.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 32 行

PHP
32
字号
<?php
// Open the database connection
if (!($db = mysqli_connect("localhost, 'guest', 'secret', 'dbname')))
{
// Handle errors
die('SQL ERROR: Connection failed: ' . mysqli_error($db));
}
// Prepare a SQL insert: assume we received the description from the user
$desc = 'Mortising Machine';
$escaped = mysqli_escape_string($desc);
$sql = "insert into tools (descr, qty) values ('{$escaped}', 0)";
// Do the insertion:
if (!(mysqli_query($db, $sql)))
{
// Give an error statement:
die('SQL INSERT ERROR: ' . mysqli_error($db). " - Query was: {$sql}");
}
// Select data from the table:
$sql = 'select id, desc from tools order by id asc';
if (!($result = mysqli_query($db, $sql)))
{
// Give an error statement:
die('SQL SELECT ERROR: ' . mysqli_error($db). " - Query was: {$sql}");
}
// Read all the data back in as associative
arrayswhile ($row = mysqli_fetch_assoc($result))
{
echo "{$row['id']}.{$row['descr']}<br />\n";
}
// Now close the connection
mysqli_close($db);
?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?