simpli Posted March 19, 2009 Share Posted March 19, 2009 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.... } ?> Quote Link to comment Share on other sites More sharing options...
waynew Posted March 19, 2009 Share Posted March 19, 2009 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 Quote Link to comment Share on other sites More sharing options...
simpli Posted March 20, 2009 Author Share Posted March 20, 2009 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 Quote Link to comment Share on other sites More sharing options...
fenway Posted March 27, 2009 Share Posted March 27, 2009 Why wold you break the connection? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.