Page 1 of 1

when MySQL dba_close($dba)

Posted: Sun May 30, 2010 12:29 pm
by jwinslow
Hello all,
I have just begun php programming and I have a question about when to close a database link. I have searched on google, I have a book "Teach Yourself PHP in 24 Hours" gives some solid examples, W3C school.org also but I cannot seem to find a defined usage of closing the connection. The examples I have examined instruct you to close a link so that other connections can interact with the database, it is not multi-threaded which is causing me some confusion.

Perhaps I should tell you what I am doing. I am working on developing a rather large project which is OOD(object oriented design)/OOP(object oriented programming) and I am breaking up functionality classes into objects such as:

config.php - stores the connection variables
openlink.php -opens the connection to the database -

Code: Select all

 include('config.php');
dbconnect.php - creates the database if one is not created yet, and connects to it -

Code: Select all

include('openlink.php');
createtables.php - creates the necessary tables if they are not already created -

Code: Select all

include('dbconnect.php');
query.php - populates the tables with the primary keys. -

Code: Select all

include('createtables.php');
So far these are the only classes that i have. And my question is would I execute the

Code: Select all

dba_close($myconnection);
AFTER all this is created?

As far as the design pattern I have, I would like all scripts that interact with the database except the openlink.php to fire only once using the include_once() I forget exactly that method.

Should I close the connection after creating the tables for other classes to be called to interact with the MySQL database?
:crazy:

Re: when MySQL dba_close($dba)

Posted: Sun May 30, 2010 1:29 pm
by jwinslow
Here is my primary connection file, which creates the database if one does not exists, adding multiple connection checkpoints and status messages basically for me since Im still learning, but it only seems to output:

connected to host"
database created"
0


The 0 I am assuming is one of the error messages but I am not sure which one it is since I never simply said to output just an error, it should have also output a message to tell me which error it was throwing.

The file for the most part works, it creates the database. The file after this one will create the tables and the next file after that will populate the data. But before I continue I really would like to know what is going on with this and why the 0. Thanks in advance.

Code: Select all

<?php
#include the connection variables
include ('config.php');

#create the link and pass connection variables into parameters
$link = mysql_connect($hostname, $username, $password);
if ($link)
{
	echo "connected to host<br/>";
}
#check if the link didn't connect
if (!$link)
{
	echo "cannot connect to host" + mysql_error();
}

#attempt to connect to the database
mysql_query($dbname, $link);

#create the database if one is not created
if (!mysql_query("CREATE DATABASE JwDev_db", $link)) 
{
	echo "Database does not exist, creating database <br/>";
	mysql_query($dbname, $link);
}

#echo database was already created if one exists
if (mysql_query("CREATED DATABASE JwDev_db", $link))
{
	echo "Database was already created<br/>";
}

#connect to the database and create one if one does not exist
mysql_select_db($dbname, $link) or die ("Could not Select Database" + mysql_error());

#status for selecting the database
if (mysql_select_db($dbname, $link))
{
	echo $dbname + " : was selectd<br/>";
}
?>

Re: when MySQL dba_close($dba)

Posted: Sun May 30, 2010 1:30 pm
by phdatabase

Re: when MySQL dba_close($dba)

Posted: Sun May 30, 2010 1:39 pm
by phdatabase
I don't see anything really wrong except for PHP's concatenation operator is a '.' not '+'

Re: when MySQL dba_close($dba)

Posted: Sun May 30, 2010 2:00 pm
by jwinslow
oh wow ya.. ok so whether you add the dba_close($dba) or not it will close it eventually if left inactive after next files execution. I actually just found these pages and was looking up how to check WHEN and WHAT triggers the database to be created if one does not exist, and also how to check to see WHAT created the database. SUPER noob questions I truly apologize.