SQLite3::prepare()
(PHP 5 >= 5.3.0, PHP 7)
Prepares an SQL statement for execution
说明
public SQLite3::prepare(string $query): SQLite3Stmt
Prepares an SQL statement for execution and returns an SQLite3Stmt object.
参数
- $query
The SQL query to prepare.
返回值
Returns an SQLite3Stmt object on success 或者在失败时返回FALSE.
范例
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());
?>
参见
- SQLite3Stmt::paramCount() Returns the number of parameters within the prepared statement
- SQLite3Stmt::bindValue() Binds the value of a parameter to a statement variable
- SQLite3Stmt::bindParam() Binds a parameter to a statement variable
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'];
}
}
?>
