Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old February 4th, 2004, 09:22 PM
al042077 al042077 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Omaha, NE
Posts: 23 al042077 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 sec
Reputation Power: 0
Send a message via Yahoo to al042077
Building a DB Class

As a means to be more efficient as well as learning more about OO/class design and use in PHP, I'm working on a database class. The idea is that I could change one line in my settings file to set which database to use.
php Code:
Original - php Code
  1. $DB_TYPE='mysql'

I then name each respective class the same and create objects by:
php Code:
Original - php Code
  1. $db = new $DB_TYPE;

It seems to be connecting fine, but I'm having a lot of trouble with the queries. I'm trying to create a function to run a SELECT query and return the results to a class variable according to whether I wanted a numerical index, associative, or as objects. Here are the three files:
php Code:
Original - php Code
  1. ----test_settings.php----
  2. <?php
  3. $DB_TYPE = 'mysql';
  4. $DB_HOST = 'localhost';
  5. $DB_DATABASE = 'test';
  6. $DB_USERNAME = 'user';
  7. $DB_PASSWORD = 'pass';
  8. ?>
  9.  
  10. ----test.php----
  11. <html>
  12. <head>
  13.     <title>TEST</title>
  14. </head>
  15. <body>
  16. <?php
  17. require_once('test_settings.php');
  18. require_once("_classes/class_$DB_TYPE.php");
  19. $db = new $DB_TYPE("$DB_HOST","$DB_DATABASE","$DB_USERNAME","$DB_PASSWORD");
  20. $db->setstrQuery("SELECT host,user,password FROM user");
  21. $db->strSelect();
  22. while($db->rsNumArray) {
  23.     echo $db->rsNumArray[0] ." : ". $db->rsNumArray[1] ."<br />";
  24. }
  25. ?>
  26. </body>
  27. </html>
  28.  
  29. ----_classes/class_mysql.php---
  30. <?php
  31. /*#################################################  ############################
  32. This class manages a database instance which handles all database operations.
  33. It is currently configured for a MySQL 4.x database.
  34. Need to first include a configuration file with connection information.
  35.   $DB_HOST --> host name or address on which to connect
  36.   $DB_DATABASE --> name of database to connect to
  37.   $DB_USERNAME --> username to connect with
  38.   $DB_PASSWORD --> password to connect with
  39. */
  40. class mysql {
  41.     var $hostConn = -1;
  42.     var $databaseConn = -1;
  43.     var $strQuery = '';    // string for the query
  44.     var $rsPref = 0;    // indicates how to return rs
  45.     var $rs;
  46.     var $rsNumArray;
  47.     var $rsAssocArray;
  48.     var $rsObject;
  49.     var $rsNumRows = -1;    // number of rows returned
  50.     var $rsAffRows = -1;    // number of rows affected/inserted
  51. /*#################################################  ############################
  52. mysql Constructor
  53. Establishes connection to server and database.
  54. */
  55.     function mysql($h,$d,$u,$p) {
  56.         $this->hostConnect($h,$u,$p);
  57.         $this->databaseConnect($d);
  58.     }
  59. /*#################################################  ############################
  60. Connect to host server
  61. */
  62.     function hostConnect($h,$u,$p) {
  63.         $this->hostConn = mysql_connect($h,$u,$p) or die("Could not connect to host.");
  64.     }
  65. /*#################################################  ############################
  66. Connect to database
  67. */
  68.     function databaseConnect($d) {
  69.         $this->databaseConn = mysql_select_db($d) or die("Could not connect to database.");
  70.     }
  71. /*#################################################  ############################
  72. Prepare a string for a query.
  73. */
  74.     function strPrepare() {
  75.         if (!get_magic_quotes_gpc() )
  76.             $this->strQuery = mysql_escape_string($this->strQuery);
  77.     }
  78. /*#################################################  ############################
  79. Run SELECT query.
  80. */
  81.     function strSelect() {
  82.         if (!$this->strQuery) {
  83.             return 0;
  84.     }
  85.         $this->rs = mysql_query($this->strQuery) or die(mysql_error() );
  86.         switch ($this->rsPref) {
  87.             // Numerical Index, returns items as $row[0]
  88.             case "0" :
  89.                 $this->rsNumArray = mysql_fetch_row($this->rs);
  90.                 break;
  91.             // Associative Index, returns items as $row['column']
  92.             case "1" :
  93.                 $this->rsAssocArray = mysql_fetch_array($this->rs);
  94.                 break;
  95.             // Objects
  96.             case "2" :
  97.                 $this->rsObject = mysql_fetch_object($this->rs);
  98.                 break;
  99.             default :
  100.                 break;
  101.         }
  102.         $this->rsNumRows = mysql_fetch_rows($this->rs);
  103.     }
  104. /*#################################################  ############################
  105. ACCESSOR METHODS
  106. */
  107.     function getstrQuery() {
  108.         return $this->strQuery;
  109.     }
  110.     function setstrQuery($strQuery) {
  111.         $this->strQuery = $strQuery;
  112.     }
  113. }
  114. ?>

So, once I run strSelect(), how would I reference the results?

Reply With Quote
  #2  
Old February 5th, 2004, 03:33 AM
al042077 al042077 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Omaha, NE
Posts: 23 al042077 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 sec
Reputation Power: 0
Send a message via Yahoo to al042077
RE: Building a DB Class

I'm pretty cool... or at least for 30 seconds more, before I hit another jam.
I figured out that deal. In case anyone else wants to use this:
I made the query kick teh results to $this->rs, but kept the mysql_fetch_array() outside of the class.

If anyone knows a way to put that inside the class, please let me know. Thanks!!

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > Building a DB Class


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT