The role of php pear db in website construction

Source: Internet
Author: User
Tags dsn php website

In PHP website development, it supports various database engines, such as Mysql, Mssql, Pgsql, and sqlite, and provides different functions as interfaces for various database systems, it brings a lot of convenience to PHP website developers.

But it also brings about the platform portability problem. As the underlying database changes, the PHP code must also change. There are various solutions to this problem, such as using the php adodb class, php pear db class, or self-writing the php db class to aggregate function operations of various databases, today we will share with you how to install and use the php pear db class to implement access to different databases.

Preparations

1. before accessing the database using the php pear db class, you need to install the php pear class, and then download and install the DB class through the PEAR install db class.

2. Install related databases as needed, such as Mysql, Mssql, Pgsql, and Sqlite. Find Dynamic Extensions in PHP. INI, introduce the DLL file of the corresponding data, and restart Apache.

Note: Because DedeAMPZ is used, you must install it in the DedeAMPZWebRootDefault directory when installing php pear; otherwise, DB is introduced. failedopening required' DB will be reported during php. the php' error indicates that the DB class (cocould not find pear db library) cannot be found, because DedeAMPZ may limit the related directories.

Php pear db Class Example

 
 
  1. < ?  
  2. require_once("DB.php");  
  3. $userName = 'root';  
  4. $password = '123456';  
  5. $hostName = 'localhost';  
  6. $dbName = 'test';  
  7. $dsn = "mysql://$userName:
    $password@$hostName/$dbName";  
  8. $dbCon = DB::connect($dsn);  
  9. if (DB::isError($dbCon)) {  
  10. die ($dbCon->getMessage());  
  11. }  
  12. $sql = "CREATE TABLE leapsoul (".  
  13. "`id` INT( 11 ) UNSIGNED NOT NULL ,".  
  14. "`name` VARCHAR( 30 ) CHARACTER 
    SET gbk COLLATE gbk_chinese_ci NOT NULL ,".  
  15. "`age` INT( 2 ) NOT NULL ,".  
  16. "`birthday` VARCHAR( 30 ) CHARACTER 
    SET gbk COLLATE gbk_chinese_ci NOT NULL ,".  
  17. "`sex` INT( 1 ) NOT NULL ,".  
  18. "PRIMARY KEY ( `id` )".  
  19. ") ENGINE = MYISAM CHARACTER SET gbk 
    COLLATE gbk_chinese_ci";  
  20. $result = $dbCon->query($sql);  
  21. if (DB::isError($result)) {  
  22. die ($result->getMessage());  
  23. }  
  24. $sql = "insert into leapsoul(id,name,
    age,birthday,sex) values(1,'leapsoul',1,
    '2009-05-13',1),(2,'leapsoul',1,'2009-05
    -13',1),(3,'leapsoul',1,'2009-05-13',1)";  
  25. $result = $dbCon->query($sql);  
  26. if (DB::isError($result)) {  
  27. die ($result->getMessage());  
  28. }  
  29. $dbCon->setFetchMode(DB_FETCHMODE_ASSOC);  
  30. $sql = "select * from leapsoul";  
  31. $result = $dbCon->query($sql);  
  32. if (DB::isError($result)) {  
  33. die ($result->getMessage());  
  34. }  
  35. for($i=0;$i<$result->numRows();$i++)  
  36. {  
  37. $info = &$result->fetchRow();  
  38. echo "name:".$info['name'];  
  39. echo "birthday:".$info['birthday']."<br>";  
  40. }  
  41. $result->free();  
  42. $dbCon->disconnect();  
  43. }  

The above is the detailed usage of the php pear db class.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.