PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
ol4pr0
Forum Regular
Posts: 926 Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador
Post
by ol4pr0 » Wed Dec 08, 2004 3:28 pm
Oke i have this class
had me an other one. but that didnt work so came up with this.
However i cant seem to figure out why it wont connect.
Code: Select all
<?php
class database {
var $link;
var $errors = array();
function connect( $host, $name, $pass, $db ) {
$link = mysql_connect( $host, $name, $pass );
if ( ! $link ) {
$this->setError("Couldn't connect to database server");
return false;
}
if ( ! mysql_select_db( $db, $this->link ) ) {
$this->setError("Couldn't select database: $db");
return false;
}
$this->link = $link;
return true;
}
// and some more ofcourse
Executing it.
Code: Select all
$dl = new database( );
$dl->connect( "***", "****", "", "test" ) or die ("cant connect");
#$dl->connect( "localhost", "root", "", "test" ) or die ( $dl->getError() );
# as this didnt return a error i put it cant connect manually to see where it went wrong.
Last edited by
ol4pr0 on Wed Dec 08, 2004 4:14 pm, edited 1 time in total.
protokol
Forum Contributor
Posts: 353 Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:
Post
by protokol » Wed Dec 08, 2004 3:31 pm
Look at line 13. You'll see that you are accessing $this->link before setting the value
Change it to $link and you'll be cool.
kettle_drum
DevNet Resident
Posts: 1150 Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England
Post
by kettle_drum » Wed Dec 08, 2004 3:33 pm
Code: Select all
$link = mysql_connect( $host, $name, $pass );
//needs to be
$this->link = mysql_connect( $host, $name, $pass );
//or change
if ( ! mysql_select_db( $db, $this->link ) ) {
//to
if ( ! mysql_select_db( $db, $link ) ) {
protokol
Forum Contributor
Posts: 353 Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:
Post
by protokol » Wed Dec 08, 2004 3:35 pm
I would use the following because he probably doesn't want the link set yet until he actually gets a valid connection:
Code: Select all
<?php
if ( ! mysql_select_db( $db, $link ) ) {
?>
ol4pr0
Forum Regular
Posts: 926 Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador
Post
by ol4pr0 » Wed Dec 08, 2004 3:39 pm
Hmm tried both. and still no luck
a regular connection without having it inside a class works fine.
[edit]
My bad. thanks both of you.
Works nicely now
Last edited by
ol4pr0 on Wed Dec 08, 2004 3:45 pm, edited 1 time in total.
protokol
Forum Contributor
Posts: 353 Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:
Post
by protokol » Wed Dec 08, 2004 3:43 pm
Post your new code so we can debug further.