vForums Support Banner



 

Welcome Guest! Please Login or Register
vForums Support :: Programming & Coding :: Programming Discussion :: Check out my PHP class - View Topic
Topic Rating: *****
Printable View
Dylan
Junior Member
**

[Avatar]

Posts: 136
Status: Offline
Gender: Male
Joined:  
Reputation: 0%  


pmwwwmsnaimxfire
Check out my PHP class (24th Mar 08 at 1:15am UTC)
See if I can improve my PHP class
Ok so i've been reading about classes and I made my first class. I don't want to form bad habbits so I want you guys to tell me if I'm doing it right or if theres a better way to do something that I"m doing?

Code:
 
  1. <?php
  2. class Database    {
  3.  
  4.     function __construct(){
  5.     $dbh=mysql_connect ("localhost", "board_bobito2", "bobito2") or die ($this->sql_error());
  6.     mysql_select_db ("board");
  7.     }
  8.     
  9.     protected    function sql_error() {
  10.     echo mysql_error();
  11.     }
  12.     
  13.     function do_query($query) {
  14.     return mysql_query($query);
  15.     }
  16.     
  17.     function get_assoc($query) {
  18.     return mysql_fetch_assoc($query);
  19.     }
  20.     
  21.     function get_array($query) {
  22.     return mysql_fetch_array($query);
  23.     }
  24.     
  25.     function get_rows($query) {
  26.     return mysql_num_rows($query);
  27.     }
  28.     
  29. }
  30.  
  31.  
  32. ?>
  33.  
 



And this is how I'm doing a query and displaying it.
Code:
 
  1. <?php
  2. include('query.php');
  3. $db =new Database;
  4. $query =$db->do_query("SELECT * FROM support_users WHERE username='admin'");
  5. $row =$db->get_assoc($query);
  6.  
  7. echo $row['username'];
  8. ?>
 


Also I'm going to create a new file for each class and it's subclasses so like all classes relating to users will be in the users.php file which is in the framework folder. Do you guys do something similar?


Image
Paddy
Full Member
***
Insane Clown

[Avatar]

Posts: 288
Status: Offline
Gender: Male
Location: Buffalo, New York
Age: 34
Joined:  
Reputation: 7%  


pmwwwtwitterskypeaimgtalk
Re: Check out my PHP class (24th Mar 08 at 2:24am UTC)
Yes.

http://www.cakephp.org

I just don't bother writing the nitty-gritty myself. {Tongue Out}

By the way, you probably want the database connection information in its own class, and have a wrapper function for the querying.

~Artemis
Dylan
Junior Member
**

[Avatar]

Posts: 136
Status: Offline
Gender: Male
Joined:  
Reputation: 0%  


pmwwwmsnaimxfire
Re: Check out my PHP class (24th Mar 08 at 2:29am UTC)
 
Yes.

http://www.cakephp.org

I just don't bother writing the nitty-gritty myself. {Tongue Out}

By the way, you probably want the database connection information in its own class, and have a wrapper function for the querying.

~Artemis


You really like cake don't you {Smile}

Whats a wrapper function?


Image
Paddy
Full Member
***
Insane Clown

[Avatar]

Posts: 288
Status: Offline
Gender: Male
Location: Buffalo, New York
Age: 34
Joined:  
Reputation: 7%  


pmwwwtwitterskypeaimgtalk
Re: Check out my PHP class (24th Mar 08 at 2:32am UTC)
Yes I do. ^_^

Basically, it'd be like creating a method of the database class called query that would just call mysql_query. It performs an already-existing function with little to no modification. The benefit is, all your calls are to this class' method, so you can update how you query things easily, and if you want to, you can easily fill in required but static fields (e.g., the host, username, password, etc.) in the method, meaning you don't have to type them hundreds of times, and get screwed when one changes.

~Artemis
 Printable View

All times are GMT+0 :: The current time is 8:14am
Page generated in 0.7505 seconds
This Forum is Powered By vForums (v2.4)
Create a Forum for Free | Find Forums