Jump to content

database connection clarification


simpli

Recommended Posts

Hi all,

I have a problem with my connection.

As is illustrated in this non working code (I wanted to show what I do for advice not debugging), the first time I connect to the page I connect to the database and retrieve the information to build the form that I want the user to fill. when the user submits, the same page is called but this time is redirected to the server side validation. After the data has been validated, I now want to insert it in the database.

 

I had no luck referring to the connection variables as global and therefore I have to redeclare and reassign the value to them which basically means that I have the connection information at several places in my code. What can I do to remedy that situation. Should I have a connection function that I could call at several places or is there a way i can declare the connection info so it can be accessed from every place and function from my php file?

 

Thanks for your help.

J-R

 

<?php

//php stuff forms etc

//If we get here as a result of the submit button having been pushed: We are going to validate the data
if(isset($_POST['submit'])){
//if we came here as a result of submit: Do some validation etc etc..
}

//Server side validation ok we write to database or if there's an error we writte error msg
	if($error == ''){
		addmatchups();
	}else{

		echo '<fieldset class="six" style="width:450px">';
		echo '<legend>Erreurs</legend>';
		echo $error;
		echo '</fieldset>';
	}
}

//If we didnt come here as a result of a submit button (1st time form is displayed)
// Connects to database
$dbhost = 'localhost';
$dbuser = 'xyz';
$dbpass = 'whatever';
$dbname = 'dbwhatever';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>

<?php

//build the form etc....
}
//End of the form
?>
</form>
</br> 
</body></html> 



<?php
function addmatchups()
{
//Some variable declaration etc...

//Here I must redeclaremy connection stuff How can I avoid this?
$dbhost = 'localhost';
$dbuser = 'xyz';
$dbpass = 'whatever';
$dbname = 'dbwhatever';


$lconn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);

//writing in database etc....

}


?>

Link to comment
Share on other sites

You should really put

 

<?php
$dbhost = 'localhost';
$dbuser = 'xyz';
$dbpass = 'whatever';
$dbname = 'dbwhatever';

$lconn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>

 

into a file called connect.php or whatever and then include it with your page whenever you want to connect to the db

 

require_once("connect.php"); // connection in existence, you can now query mysql

//do queries etc

Link to comment
Share on other sites

Does that mean that if I break the connection I can resume it later just by adding the include? In this sense it would work like a function that is called several times. Why not do a function then?

 

 

Thanks for clarifying.

J-R

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.