·历史归档2 分钟阅读
solarphp中SQL基础
愚者·布尔软件 (Bullsoft) 创始人
主要涉及到Solar_Sql包 上篇文章中我们有提到solar初始化时会自动注册Solar_Sql类,所以我们只需要使用Solar_Registry,就可以实例化Solar_Sql类。
$sql = Solar_Registry::get('sql');
Solar_Sql是基于PDO的,能够适用MySQL, PostgreSQL, SQLite,而对于使用者来讲只需要使用Sloar_Sql的功能,不用关心各种不同数据库的细节。 配置Sloar_Sql对象
$config['Solar_Sql']['adapter'] = 'Solar_Sql_Adapter_Mysql';
$config['Solar_Sql_Adapter_Mysql'] = array(
'host' => 'localhost',
'user' => 'root',
'pass' => '123456',
'name' => 'solar',
);
那么你就可以像这样使用它了:
$rs = $sql->query("select * from message");
$this->arr = $rs->fetchAll();
查旬的结果是pdoStatement格式,获取数据可以用下面这些方式:
Solar_Sql_Adapter::fetchAll() //returns a sequential array of all rows. The rows themselves are associative arrays (keys are the column names).
Solar_Sql_Adapter::fetchAssoc() //returns an associative array of all rows (key is the first column).
Solar_Sql_Adapter::fetchCol() //returns a sequential array of all values in the first column.
Solar_Sql_Adapter::fetchOne() //returns the first row as an associative array (keys are the column names).
Solar_Sql_Adapter::fetchPairs() //returns an associative array where each key is the first column and each value is the second column.
Solar_Sql_Adapter::fetchValue() //returns the value of the first row in the first column.
To quote values so they can be used safely in SQL commands, use …
For direct-execution of commands, use…
To fetch results, use …
- Solar_Sql_Adapter::fetchAll()
- Solar_Sql_Adapter::fetchAssoc()
- Solar_Sql_Adapter::fetchCol()
- Solar_Sql_Adapter::fetchOne()
- Solar_Sql_Adapter::fetchPairs()
- Solar_Sql_Adapter::fetchPdo()
- Solar_Sql_Adapter::fetchSql()
- Solar_Sql_Adapter::fetchValue()
To conveniently manipulate data in individual tables, use…
For auto-increment values and database-portable sequences, use…
- Solar_Sql_Adapter::lastInsertId()
- Solar_Sql_Adapter::createSequence()
- Solar_Sql_Adapter::dropSequence()
- Solar_Sql_Adapter::nextSequence()
For database-portable transactions, use…
For database-portable table/column/index modification, use…