PHP PEAR DB Class in the site construction of the role of _php tutorial

Source: Internet
Author: User
Tags dsn mssql pear php website

In the development of PHP Web site, because it supports various database engines, such as Mysql,mssql,pgsql,sqlite, and provides different functions as interface for various database systems, it brings many conveniences to PHP website developers.

But it also brings the problem of platform portability, and the PHP code must change as the underlying database changes. For this problem, there are various solutions, such as the use of PHP ADODB class, PHP PEAR DB class, or write PHP DB class, the various functions of the database aggregation together, today and we share how to install the use of PHP PEAR DB class, to achieve different database access capabilities.

Preparatory work

1, before using the PHP PEAR DB class to access the database, you need to install PHP PEAR, and then through the PEAR to download the installation DB class, that is, PEAR install DB

2, according to the need to install relevant databases, such as Mysql,mssql,pgsql,sqlite, while in php.ini find dynamic Extensions, the introduction of the corresponding data DLL file, and restart Apache.

Note: Since I am using Dedeampz, install PHP pear must be installed in the Dedeampzwebrootdefault directory, or in the introduction of db.php will be reported failedopening required ' db.php ' ERROR, The DB class could not be found (could not find Pear DB Library) because Dedeampz might restrict the associated directory.

PHP PEAR DB Class Use 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 (one) UNSIGNED not NULL,".
  14. "' Name ' VARCHAR (CHARACTER)
    SET GBK COLLATE gbk_chinese_ci not NULL, ".
  15. "' Age ' INT (2) is not NULL,".
  16. "' Birthday ' VARCHAR (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. }

These are the details of how PHP PEAR DB classes are used.


http://www.bkjia.com/PHPjc/446030.html www.bkjia.com true http://www.bkjia.com/PHPjc/446030.html techarticle in the development of PHP Web site, because it supports a variety of database engine, such as Mysql,mssql,pgsql,sqlite, and various database systems are provided with different functions as interface, to the PHP site ...

  • Related Article

    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.