Home  >  Article  >  Backend Development  >  An example of php calling database class

An example of php calling database class

WBOY
WBOYOriginal
2016-07-25 09:04:38936browse
  1. /**

  2. desc: Database class call instance Assume your data class is db_class.php
  3. link: bbs.it-home.org
  4. date: 2013/2/24
  5. */
  6. require_once("db_class.php");

  7. //1. Create a class, And connect to the database

  8. $db = new mssql("dns=aaa;uid=sa;pwd=sa;dbname=test");

  9. //2. Connect to the database

  10. $conn = $ db->config("dns=aaa;uid=sa;pwd=sa;dbname=test");

  11. //3. Select database

  12. $dbname = $db-> select_db("test");

  13. //4.Set to allow debugging

  14. $db->debug = true;

  15. //5.Execute a SQL statements that do not return results

  16. $db->execute("insert into test01(name) values('This is a test!')");
  17. //$db->exec("");< /p>
  18. //6. Execute a SQL statement that returns results

  19. $rs = $db->query("select * from test01");

  20. // 7. Display the results in row format

  21. echo "
    Display the result set in row format
    ";
  22. while($r = $db->fetch_row($rs)){
  23. echo $r[0 ].":".$r[1]."
    ";
  24. }

  25. //8. Display the results in array format

  26. $rs2 = $db->query ("select * from test01");

  27. echo "
    Display the result set in array format
    ";

  28. while($r = $db->fetch_array( $rs2)){
  29. echo $r["id"] . ":" . $r["name"] . "
    ";
  30. }

  31. //X. Release

  32. $db->db_close();
  33. ?>

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn