First I would like to suggest about the use of this package . That would be useful only when you are developing application from start. Its all about your journey of programming from your first step to the last step. Recently I came to the use of database other than MySql with PHP.
How many databases you know?
What else, every database have different way for connection, different function, different way to perform DB tasks. MDB2 package is really a good way to work with different databases. Sometimes our application need logic beyond the power of existing database, then what will you do? Will you change your database? Then what about all the classes or files that you have built to handle data!!!!
MDB2 is the package which gives you the facility to tackle this problem. Install the driver for that particular DB using PEAR command and change the name of driver in your database file. What else, now use the power of your new database.
Following is the simple example of that:
// Use the details of your database at the place of given dummy values :)
$db_driver="mysql"; // only need to change the db driver if other need to be used
$user="user;
$password="password";
$host="host";
$database_name="database_name";
$url=$db_driver."://".$user.":".$password."@".$host."/".$database_name;
require_once("MDB2.php");
$con = MDB2::factory($url);
if(PEAR::isError($con)) {
die("Error while connecting : " . $congetMessage());}
else{echo "Connection is done"."
";}
$sql = "SELECT * FROM users";
$resultset = $con->query($sql);if(PEAR::isError($resultset))
{ die('Failed to issue query, error message : ' . $resultset->getMessage());}
while($row = $resultset->fetchRow(MDB2_FETCHMODE_ASSOC))
{foreach($row as $field => $value)
{ echo "$field / $value \n";}}?>
Saturday, September 27, 2008
Sunday, September 21, 2008
Another Example for PHP Unit test Using PEAR
//ini_set('include_path','.:/root/PEAR'); Don't forget to set include_path where PEAR is installed in php.ini file or in .htacess for Apache server or explicitly in your coding
require_once 'PHPUnit/Framework.php';
require_once "PHPUnit/Framework/TestSuite.php";
require_once "PHPUnit/TextUI/TestRunner.php";
class ArrayTest extends PHPUnit_Framework_TestCase
{
public function testNewArrayIsEmpty()
{
// Create the Array fixture.
$fixture = array();
// Assert that the size of the Array fixture is 0.
assertEquals(1, sizeof($fixture));
}
}
$suite = new PHPUnit_Framework_TestSuite();
$suite->addTest(new
ArrayTest('testNewArrayIsEmpty'));
PHPUnit_TextUI_TestRunner::run($suite);
?>
"FYI, you have to create your own test cases according to your code and you will use classes and methods provided by PEAR's PHPUnit Package."
require_once 'PHPUnit/Framework.php';
require_once "PHPUnit/Framework/TestSuite.php";
require_once "PHPUnit/TextUI/TestRunner.php";
class ArrayTest extends PHPUnit_Framework_TestCase
{
public function testNewArrayIsEmpty()
{
// Create the Array fixture.
$fixture = array();
// Assert that the size of the Array fixture is 0.
assertEquals(1, sizeof($fixture));
}
}
$suite = new PHPUnit_Framework_TestSuite();
$suite->addTest(new
ArrayTest('testNewArrayIsEmpty'));
PHPUnit_TextUI_TestRunner::run($suite);
?>
"FYI, you have to create your own test cases according to your code and you will use classes and methods provided by PEAR's PHPUnit Package."
Friday, September 19, 2008
Introduction to PEAR
PEAR, the PHP Extension and Application Repository, is a bountiful resource for any PHP developer. Within its confines lie the tools that you need to do your job more quickly and efficiently. You need to be able to quickly assess and determine which PEAR package is the right one for the task you are up against. Then you need to know the API and see some examples of how to best use it.
Now PHP is continuously moving towards its heights. If you love oops, you will love PHP5 and of course PEAR.
I am giving the simple example of using PEAR::DB package:
Using that package, we don't need to think of the database server we are using, we only need to pass an array as shown above to the static function connect(), it will make connection to the database server that can be MySQL, PostgreSQL, and Oracle.
To use PEAR, you have to install PEAR. To install PEAR and know about available packages, the URL is pear.php.net
We will continue with these series, till then enjoy and live programming.
Now PHP is continuously moving towards its heights. If you love oops, you will love PHP5 and of course PEAR.
I am giving the simple example of using PEAR::DB package:
// Include the pear class
require_once("DB.php");
$dsn=array('phptype' =>'mysql','hostspec' => 'localhost',
'database' => 'test_db',
'username' => 'test_user',
'password' => 'test_password'
);
$dbh = DB::connect($dsn);
$stmt = "SELECT id, name FROM examples ORDER BY id";
$result = $dbh->simpleQuery($stmt, DB_FETCHMODE_ASSOC);
if ($dbh->numRows($result) > 0) {
$data = (object) $dbh->fetchRow($result, DB_FETCHMODE_ASSOC);
echo "id => $data->id
\n";
echo "name => $data->name
\n";
}
Using that package, we don't need to think of the database server we are using, we only need to pass an array as shown above to the static function connect(), it will make connection to the database server that can be MySQL, PostgreSQL, and Oracle.
To use PEAR, you have to install PEAR. To install PEAR and know about available packages, the URL is pear.php.net
We will continue with these series, till then enjoy and live programming.
Subscribe to:
Posts (Atom)