writing a mysql connection class<

liunx

Guest
Can anyone help me out on this one? I'm using php-4.3.4 for linux with mysql if that matters. Right now I'm just trying to get the bare minimum to work, and even that's causing me problems, I don't want to litter up my code with selects and stuff while I'm still trying to figure out the connection (hence the 'if' statement). In case your wondering why I'm doing this in classes instead of functionally, I'm starting a java course in school soon and OOP concepts are still pretty over my head so I want to get a bit of a heads up in school before I start. I'm already coding php for fun now so I figure why not start learning a little to begin with. One quickie about the script below, I'm pretty sure but the "function open_connection", thats a constructor am I right? Or is it a method... I'm a little lost on that, and what the difference is between the two. Are methods the ones you have to call like this:


$var = new class1;
$var->method1;


Am I right there? I have the 'php cookbook' guiding me and their a little shady on the details of that imo. Anyways, thats not as important a question to me as getting the code below to work. That would be nicer. Thanks

class open_connection
{
function open_connection()
{
$connection = mysql_connect("user","localhost","passwd");
mysql_select_db("test",$connection);
}
}

$open_db = new open_connection(); // connect

if($open_db)
echo "open";
else
echo "not";

?>


Thanks :)well if you want to check to see if the connection is live the you have to test it in the function

class open_connection
{
function connect()
{
$this->Link_ID = @mysql_connect("user","localhost","passwd");
if (!$this->Link_ID) {
echo" Connection fialed"
exit;
}
mysql_select_db("test",$connection);
}
}

$open_db = new open_connection(); // start new class id
$open_db->connect(); // do the connection

?>

see, I check for the connection in the function. I am not big on the names either but the function I believe is the method. I gave the method a varaible so I can use in another function like an error function if you may. thses varaibles are called objects. these objects can be used any where in this class.

make sense?Thats great Scoutt, I understand 90% of what you said, and putting the test inside the method made sense the second I saw it. One quick question, can you explain what you did with the @ symbol in the following line? Thanks for the help Scoutt.

$this->Link_ID = @mysql_connect("user","localhost","passwd");the @ surpresses any errors to be shown, that is the only reason you use it is if you have a 'or die()' or another function that catches the error liek I have shown.i see :cool:
 
Back
Top