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
https://forums.phpfreaks.com/topic/150115-database-connection-clarification/
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.