14c03-2.php

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

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

⌨️ 快捷键说明

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