SQLite3::prepare
(PHP 5 >= 5.3.0)
SQLite3::prepare — Prepares an SQL statement for execution
Description
Prepares an SQL statement for execution and returns an SQLite3Stmt object.
Parameters
-
query
-
The SQL query to prepare.
Return Values
Returns an SQLite3Stmt object on success or FALSE
on failure.
Examples
Example #1 SQLite3::prepare() example
<?php
unlink('mysqlitedb.db');
$db = new SQLite3('mysqlitedb.db');
$db->exec('CREATE TABLE foo (id INTEGER, bar STRING)');
$db->exec("INSERT INTO foo (id, bar) VALUES (1, 'This is a test')");
$stmt = $db->prepare('SELECT bar FROM foo WHERE id=:id');
$stmt->bindValue(':id', 1, SQLITE3_INTEGER);
$result = $stmt->execute();
var_dump($result->fetchArray());
?>
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Расширения для работы с базами данных отдельных производителей
- SQLite3
- Функция SQLite3::busyTimeout() - Sets the busy connection handler
- SQLite3::changes
- Функция SQLite3::close() - Closes the database connection
- Функция SQLite3::__construct() - Instantiates an SQLite3 object and opens an SQLite 3 database
- Функция SQLite3::createAggregate() - Registers a PHP function for use as an SQL aggregate function
- Функция SQLite3::createCollation() - Registers a PHP function for use as an SQL collating function
- Функция SQLite3::createFunction() - Registers a PHP function for use as an SQL scalar function
- SQLite3::enableExceptions
- Функция SQLite3::escapeString() - Returns a string that has been properly escaped
- Функция SQLite3::exec() - Executes a result-less query against a given database
- Функция SQLite3::lastErrorCode() - Returns the numeric result code of the most recent failed SQLite request
- Функция SQLite3::lastErrorMsg() - Returns English text describing the most recent failed SQLite request
- Функция SQLite3::lastInsertRowID() - Returns the row ID of the most recent INSERT into the database
- Функция SQLite3::loadExtension() - Attempts to load an SQLite extension library
- Функция SQLite3::open() - Opens an SQLite database
- SQLite3::openBlob
- Функция SQLite3::prepare() - Подготавливает SQL-запрос для выполнения
- Функция SQLite3::query() - Выполняет SQL-запрос
- Функция SQLite3::querySingle() - Executes a query and returns a single result
- Функция SQLite3::version() - Returns the SQLite3 library version as a string constant and as a number
Коментарии
Without checking the return value of "prepare" if "exec" is used then it will cause Fatal Error.
"PHP Fatal error: Call to a member function execute() on a non-object "
To avoid this error,need to check return value as following:
<?php
$db = new SQLite3('school.db');
if($stmt = $db->prepare('SELECT id,student_name FROM classTen '))
{
$result = $stmt->execute();
$names=array();
while($arr=$result->fetchArray(SQLITE3_ASSOC))
{
$names[$arr['id']]=$arr['student_name'];
}
}
?>