如何使一个正确的mysqli扩展类与准备语句? [英] How to make a proper mysqli extension class with prepared statements?
问题描述
我已经搜索了一个很好的例子,但我找不到任何东西。
我试图扩展mysqli类来创建一个帮助类,它将抽象出一些复杂性。我想要完成的主要事情之一是使用预准备的语句。
我真的不知道从哪里开始,或者如何在一个类中正确处理输入和输出。另一个问题是,我无法使用预备语句作为数组输出数据。
我真的可以用一个简单的例子来指向正确的方向。 p>
谢谢!
查看 Zend_Db ,特别是 Zend_Db_Select
。事实上,你可能只是选择使用它,而不是开发自己的。示例:
//使用mysqli适配器连接到数据库
//其他支持的适配器列表请参阅
//http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.adapter-notes
$ parameters = array(
'host' =>'xx.xxx.xxx.xxx',
'username'=>'test',
'password'=>'test',
'dbname'=> ;'test'
);
try {
$ db = Zend_Db :: factory('mysqli',$ parameters);
$ db-> getConnection();
} catch(Zend_Db_Adapter_Exception $ e){
echo $ e-> getMessage();
die('Could not connect to database。');
} catch(Zend_Exception $ e){
echo $ e-> getMessage();
die('Could not connect to database。');
}
//准备语句
$ sql ='SELECT * FROM blah WHERE id =?';
$ result = $ db-> fetchAll($ sql,2);
//示例使用Zend_Db_Select
$ select = $ db-> select()
- > from('blah')
- >其中'id =?',5);
print_r($ select-> __ toString());
$ result = $ db-> fetchAll($ select);
//插入记录
$ row = array('name'=>'foo',
'created'=> time()
) ;
$ db-> insert('blah',$ row);
$ lastInsertId = $ db-> lastInsertId()?
//更新行
$ data = array(
'name'=>'bar',
'updated'=> time()
);
$ rowsAffected = $ db-> update('blah',$ data,'id = 2');
I have scoured the web for a good example but I can't find anything.
I am trying to extend the mysqli class to make a helper class that will abstract away some of the complexities. One of the main things I want to accomplish is to make use of prepared statements.
I don't really know where to start, or how to handle input and output properly in one class. Another problem is that I am unable to output data as an array while using prepared statements.
I could really use a simple example to point me in the right direction.
Thanks!
Check out the implementation of Zend_Db, and in particular, Zend_Db_Select
. In fact, you might just opt to use that instead of developing your own. Examples:
//connect to a database using the mysqli adapter
//for list of other supported adapters see
//http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.adapter-notes
$parameters = array(
'host' => 'xx.xxx.xxx.xxx',
'username' => 'test',
'password' => 'test',
'dbname' => 'test'
);
try {
$db = Zend_Db::factory('mysqli', $parameters);
$db->getConnection();
} catch (Zend_Db_Adapter_Exception $e) {
echo $e->getMessage();
die('Could not connect to database.');
} catch (Zend_Exception $e) {
echo $e->getMessage();
die('Could not connect to database.');
}
//a prepared statement
$sql = 'SELECT * FROM blah WHERE id = ?';
$result = $db->fetchAll($sql, 2);
//example using Zend_Db_Select
$select = $db->select()
->from('blah')
->where('id = ?',5);
print_r($select->__toString());
$result = $db->fetchAll($select);
//inserting a record
$row = array('name' => 'foo',
'created' => time()
);
$db->insert('blah',$row);
$lastInsertId = $db->lastInsertId();
//updating a row
$data = array(
'name' => 'bar',
'updated' => time()
);
$rowsAffected = $db->update('blah', $data, 'id = 2');
这篇关于如何使一个正确的mysqli扩展类与准备语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!