solarphp中SQL基础

主要涉及到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 ...

To conveniently manipulate data in individual tables, use...

For auto-increment values and database-portable sequences, use...

For database-portable transactions, use...

For database-portable table/column/index modification, use...