rob
August 3rd, 2005, 03:42 PM
There's a wonderful database abstraction layer for PHP called ADODB. You can find details about it here:
http://adodb.sourceforge.net/
Using it is super simple. Simply put the adodb folder in your web project, and make a connection and do queries like this:
<?php
// Config values -- I usually put these in a seperate file
$CFG['db_host'] = "localhost";
$CFG['db_type'] = "mysql";
$CFG['db_user'] = "your_username";
$CFG['db_pass'] = "your_pass";
$CFG['db_name'] = "your_db_name";
include "adodb/adodb.inc.php";
// Connect to the database (I usually put this part in a seperate file too, and include it so I don't have to put this everywhere)
$db = ADONewConnection( $CFG['db_type'] );
$db->Connect( $CFG['db_host'], $CFG['db_user'], $CFG['db_pass'], $CFG['db_name'] );
// OPTIONAL: Set associative mode.. this way you can refer to column names instead of numbers -- I recommend this strongly
$db->SetFetchMode( ADODB_FETCH_ASSOC );
// Setup the SQL statement
$sql = "SELECT * FROM `your_table`";
// Fetch the recordset object
$rs = $db->Execute( $sql );
// Loop through the recordset, outputing the values from the 'name' column
while ( ! $rs->EOF ) {
echo $rs->fields['name'];
$rs->MoveNext(); // VERY IMPORTANT -- or else infinite loop
}
// Once we're done with the recordset object, close it to preserve resources
$rs->Close();
?>
ADODB makes things faster, and you'll code better PHP as well.
Give it a shot and see if you like it!
http://adodb.sourceforge.net/
Using it is super simple. Simply put the adodb folder in your web project, and make a connection and do queries like this:
<?php
// Config values -- I usually put these in a seperate file
$CFG['db_host'] = "localhost";
$CFG['db_type'] = "mysql";
$CFG['db_user'] = "your_username";
$CFG['db_pass'] = "your_pass";
$CFG['db_name'] = "your_db_name";
include "adodb/adodb.inc.php";
// Connect to the database (I usually put this part in a seperate file too, and include it so I don't have to put this everywhere)
$db = ADONewConnection( $CFG['db_type'] );
$db->Connect( $CFG['db_host'], $CFG['db_user'], $CFG['db_pass'], $CFG['db_name'] );
// OPTIONAL: Set associative mode.. this way you can refer to column names instead of numbers -- I recommend this strongly
$db->SetFetchMode( ADODB_FETCH_ASSOC );
// Setup the SQL statement
$sql = "SELECT * FROM `your_table`";
// Fetch the recordset object
$rs = $db->Execute( $sql );
// Loop through the recordset, outputing the values from the 'name' column
while ( ! $rs->EOF ) {
echo $rs->fields['name'];
$rs->MoveNext(); // VERY IMPORTANT -- or else infinite loop
}
// Once we're done with the recordset object, close it to preserve resources
$rs->Close();
?>
ADODB makes things faster, and you'll code better PHP as well.
Give it a shot and see if you like it!