Php simple database connection-PHP source code

Source: Internet
Author: User
Ec (2); & lt ;? Php & nbsp; dd & nbsp; new & nbsp; LampDb (localhost, root, 2184237, note, utf8); & nbsp; $ SQL & nbsp; & quot; select & nbsp; * & nbsp; from & nbsp; mynote & quot; script ec (2); script

  1. Dd =NewLampDb ('localhost', 'root', '123', 'note', 'utf8 ');
  2. $ SQL = "select * from mynote ";
  3. $ Handle = $ dd-> query ($ SQL );
  4. $ Aa = $ dd-> fetchOneRow ($ handle );
  5. Print_r ($ aa );
  6. Echo" ";
  7. $ Ddd = $ dd-> fetchRows ($ handle );
  8. For($ N = 0; $ n <count ($ ddd); $ n ++ ){
  9. Echo $ ddd [$ n] ['id']. "title". $ ddd [$ n] ['subobject'];
  10. Echo"
    ";
  11. }
  12. Echo" ";
  13. Echo "nums =". $ dd-> fetchRowNums ($ handle );
  14. ?>
  15. Php
  16. Class LampDb
  17. {
  18. Private $ conn;
  19. // Connect to the database
  20. Function _ construct ($ host, $ user, $ pass, $ dbname, $ charset)
  21. {
  22. $ This-> conn = mysql_connect ($ host, $ user, $ pass );
  23. Mysql_query ('set names $ charset ');
  24. $ Db = mysql_select_db ($ dbname, $ this-> conn );
  25. }
  26. // Query statement
  27. Function query ($ SQL)
  28. {
  29. $ Handle = mysql_query ($ SQL, $ this-> conn );
  30. Return $ handle;
  31. }
  32. // Query a record
  33. Function fetchOneRow ($ handle)
  34. {
  35. $ Aa = mysql_fetch_assoc ($ handle );
  36. Return $ aa;
  37. }
  38. // Query multiple records
  39. Function fetchRows ($ handle, $ I = 0)
  40. {
  41. While ($ bb = mysql_fetch_assoc ($ handle )){
  42. $ Bbb [$ I] = $ bb;
  43. $ I ++;
  44. }
  45. Return $ bbb;
  46. }
  47. // Obtain the number of records returned by a query statement
  48. Function fetchRowNums ($ handle)
  49. {
  50. // $ Handle = $ this-> query ($ SQL );
  51. $ Num = mysql_num_rows ($ handle );
  52. Return $ num;
  53. }
  54. }
  55. ?>

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.