SunQuest
 
           General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old May 12th, 2003, 12:53 AM
lido lido is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Posts: 6 lido User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Help with my class

I have just recently started trying to use classes and one of the first scripts i made was a class to connect to a database. I have made a few scrpts now that rely on that script and I thought it was working fine but now i have found that it isnt working at all. actually i dont even think its doing anything. Can someone have a look and see if they can find anything wrong with what im doing or with teh class itself.


I use this code to initiate it.
PHP Code:
 $conx = new Connection;
$conx->conx() 



PHP Code:
<?

Class Connection
{

var 
$dbhost;
var 
$dbuser;
var 
$dbpass;
var 
$dbase;

function 
Connection()
{
$this->dbhost "localhost";
$this->dbuser "epic";
$this->dbpass "easy11";
$this->dbase "phpnation_net";
}

function 
conx()
{
$result mysql_connect("$this->dbhost""$this->dbuser""$this->dbpass");
if (!
$result)
return 
"Unable to connect to MySQL Database";
if (!
mysql_select_db("$this->dbase"))
return 
"Unable to select Database";

return 
$result;
}
}
?>

Reply With Quote
  #2  
Old May 12th, 2003, 01:48 AM
jpenn jpenn is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Washington, DC
Posts: 317 jpenn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 3 sec
Reputation Power: 6
PHP Code:
class Connection
{
    var 
$dbhost;
    var 
$dbuser;
    var 
$dbpass;
    var 
$dbase;
    var 
$error;

    function 
Connection$cfg )
    {
        
/*--------------------------------------------
        Set Att's
        --------------------------------------------*/
        
$this->dbhost $cfg['host'];
        
$this->dbuser $cfg['user'];
        
$this->dbpass $cfg['pass'];
        
$this->dbase $cfg['dbase'];
    }

    function 
conx()
    {
        
$result = @mysql_connect$this->dbhost$this->dbuser$this->dbpass ) or $this->errormysql_error() );
        @
mysql_select_db$this->dbase ) or $this->error'unable to select db' );
    }

    function 
error$string )
    {
        
$this->error[] = $string;
    }

    function 
check_connection()
    {
        return( 
$this->error );
    }
}

/*--------------------------------------------
Set Vars
--------------------------------------------*/
$cfg['host'] = "localhost";
$cfg['user'] = "epic";
$cfg['pass'] = "easy11";
$cfg['dbase'] = "phpnation_net";
/*--------------------------------------------
Initiate Object - pass connection vars
--------------------------------------------*/
$conx = new Connection$cfg );
/*--------------------------------------------
Connect To DB
--------------------------------------------*/
$conx->conx();
/*--------------------------------------------
Check Connection State
--------------------------------------------*/
if ( $conx->check_connection() )
{
    exit( 
print_r$conx->check_connection() ) );

__________________
~ Joe Penn

We work for free to help make this a valuable resource on the internet. Do you appreciate the help - did we provide help that will help you prosper and help that has contributed to sharpening your current skill set?

Show your appreciation and purchase something from our Amazon Wishlist's - it's simple and a great way to say thank you.




Reply With Quote
  #3  
Old May 12th, 2003, 03:22 AM
lido lido is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Posts: 6 lido User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks joe

Reply With Quote
  #4  
Old May 12th, 2003, 01:05 PM
jpenn jpenn is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Washington, DC
Posts: 317 jpenn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 3 sec
Reputation Power: 6
Lol - forgot to explain it.

I changed it around a little so you can define your connection params outside of the class - like in a require()'d file or something - makes it a little more transportable. Also, the error supression (@) will suppress any connection errors so they don't output to the screen - the errors in return will be send to the error method which will place them in a array. You can then use that array to see if any errors occured and adjust accordingly...

Reply With Quote
  #5  
Old May 12th, 2003, 03:43 PM
Vince Vince is offline
LordNitrous
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: England
Posts: 137 Vince User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
woa, this forum certainly ranks high as a support community you guys are great! - also are PHP classes a good thing to learn for a PHP beginner (me) and if not wat is the next thing I shud learn? considering I dont know much...
__________________
You don't deserve to read my signature...yeah! That's it, keep walking...yeah walk away from me! That's it! Get out of my pub, you're barred!

Reply With Quote
  #6  
Old May 12th, 2003, 04:26 PM
jpenn jpenn is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Washington, DC
Posts: 317 jpenn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 3 sec
Reputation Power: 6
Well - OO is a great thing to learn as OO in practice can be used in other languages also. As for beginers, the best thing would be to get a grip or feel on all the built in functions php has to offer, build applications with funtions first - then once you get the hang of functions, you will have te basics to creating methods in OO. Once you have the basics down and are able to be effecient, then it is time to step to the next level...

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Help with my 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 | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway