使用phpBB3.0 DBAL

DBAL: Database Abstraction Layer system.意为:数据库抽象层

phpBB的DBAL是phpBB团队自己开发,用以与数据库进行更友好的交互。
1. 初始化数据库连接
    1.1.不使用config.php初始化数据库连接
    1.2.使用config.php初始化数据库连接
2.数据查询
3.建立查询语句
    3.1 sql_build_array的使用
4. Inserting Data
5. Updating Data
6. Removing Data
7. Managing the Cache



1. 初始化数据库连接
1.1.不使用config.php初始化数据库连接
<?php 
/** 
*
* @package phpBB3
* @version $Id: v3_dbal.xml 44 2007-07-25 11:06:55Z smithy_dll $
* @copyright (c) 2005 phpBB Group 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
*
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
$dbms = 'mysql';

include($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);

$db = new $sql_db();
// we're using bertie and bertiezilla as our example user credentials. You need to fill in your own ;D
$db->sql_connect('localhost', 'bertie', 'bertiezilla', 'phpbb', '', false, false);

                    
?>

1.1.使用config.php初始化数据库连接
<?php 
/** 
*
* @package phpBB3
* @version $Id: v3_dbal.xml 44 2007-07-25 11:06:55Z smithy_dll $
* @copyright (c) 2005 phpBB Group 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
*
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'config.' . $phpEx);
include($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);

$db = new $sql_db();

$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, false);

// We do not need this any longer, unset for safety purposes
unset($dbpasswd);


?>


2.数据查询
<?php 
/** 
*
* @package phpBB3
* @version $Id: v3_dbal.xml 44 2007-07-25 11:06:55Z smithy_dll $
* @copyright (c) 2005 phpBB Group 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
*
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'common.' . $phpEx);    

$integer = 0;
$data = "This is ' some data";

$sql = 'SELECT *
    FROM ' . POSTS_TABLE . ' 
    WHERE post_id = ' . (int) $integer . " 
        AND post_text = '" . $db->sql_escape($data) . "'";
$result = $db->sql_query($sql);
?>

下面的例子阐述如何使用sql_query_limit
<?php 
/** 
*
* @package phpBB3
* @version $Id: v3_dbal.xml 44 2007-07-25 11:06:55Z smithy_dll $
* @copyright (c) 2005 phpBB Group 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
*
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'common.' . $phpEx);    

$integer = 0;
$data = "This is ' some data";

// Variable for query_limit
// Start with item 10
$start = 10;
// Select 5 rows
$number = 5

$sql = 'SELECT *
    FROM ' . POSTS_TABLE . ' 
    WHERE post_id = ' . (int) $integer . " 
        AND post_text = '" . $db->sql_escape($data) . "'";
$result = $db->sql_query_limit($sql, $number, $start);
?>


3. 建立查询语句
3.1 sql_build_array的使用
<?php 
/** 
*
* @package phpBB3
* @version $Id: v3_dbal.xml 44 2007-07-25 11:06:55Z smithy_dll $
* @copyright (c) 2005 phpBB Group 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
*
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'common.' . $phpEx);

//Array with the data to insert
$data = array(
    'username'     => 'Bertie',
    'email'     => '[email protected]',
);

// First doing a select with this data.
// Note: By using the SELECT type, it uses always AND in the query.
$sql = 'SELECT user_password
    FROM ' . USERS_TABLE . '
    WHERE ' . $db->sql_build_array('SELECT', $data);
$result = $db->sql_query($sql);

// And doing an update query: (Using the same data as for SELECT)
$sql = 'UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $data);
$db->sql_query($sql);

// And as last, a insert query
$sql = 'INSERT INTO ' . USERS_TABLE . ' ' . $db->sql_build_array('INSERT', $data);
$db->sql_query($sql);
?>

你可能感兴趣的:(sql,c,xml,PHP,OpenSource)