atrum Posted November 7, 2009 Share Posted November 7, 2009 Hello all, I am still somewhat new to php, and I am trying to teach my self how to setup a global variable. I have a mysql connection string stored in a secure location on my server, and I am trying to setup that connection string variable as a global variable so it can be used from any page I call it on. Can anyone point me in the right direction, or perhaps provide an example. Please let me know if more information is needed. Thanks, Atrum Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 7, 2009 Share Posted November 7, 2009 Place all your mysql connection info in one file, eg mysql_conn.php. Place this file in your secure location. Whenever you want to connect to mysql you'd use include. mysql_conn.php <?php $host= 'localhost'; $user = 'username'; $pass = 'password'; $database = 'database name here'; $conn = mysql_connect($host, $user, $pass); mysql_select_db($database); ?> Connect to mysql using include 'path/to/mysql_conn.php'; Quote Link to comment Share on other sites More sharing options...
atrum Posted November 7, 2009 Author Share Posted November 7, 2009 When I try that I get the following error. Notice: Undefined variable: sqlcon in /www/tools.exiled-alliance.com/test/includes/register_user.php on line 25 Line 25 of register_user.php = include("constr.php"); In side of constr.php I have the following <?php ini_set('display_errors','On'); $sqlcon = mysql_connect("localhost","username","password"); if (!$sqlcon) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $sqlcon); ?> Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 7, 2009 Share Posted November 7, 2009 Post more code prior to line 25 from register_user.php. Make sure you're not using the variable $sqlcon before you have included constr.php . Remember variables/functions must be defined before you use them. Quote Link to comment Share on other sites More sharing options...
atrum Posted November 7, 2009 Author Share Posted November 7, 2009 register_users.php=== <?php ini_set('display_errors','On'); /* Con String */ include("/usr/home/username/restricted/constr.php"); /* Store user details */ /* Declare field values */ $username = $_POST['txt_username']; $password = sha1($_POST['txt_password']); $firstname = $_POST['txt_firstname']; $lastname = $_POST['txt_lastname']; $email = $_POST['txt_email']; $email_c = $_POST['txt_email_c']; $dob = $_POST['txt_birthdate']; $sex = $_POST['ddl_sex']; $cb_agree = $_POST['cb_agree']; $date_reg = date("Y-m-d"); $query="INSERT INTO table (mUserName,mPassword,mFirstName,mLastName,mEmail,mDoB,mJoined,mSex,mCb_Agree) VALUES ('$username','$password','$firstname','$lastname','$email','$dob','$date_reg','$sex','$cb_agree')"; if (!mysql_query($query,$sqlcon)) { die('Error:' . mysql_error()); } echo "Registration Completed"; mysql_close($sqlcon) ?> constr.php <?php ini_set('display_errors','On'); $sqlcon = mysql_connect("localhost","username","password"); if (!$sqlcon) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $sqlcon); ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 7, 2009 Share Posted November 7, 2009 Based on the path in your error message and the path of your include() statement, the include statement is probably failing. Please use both of the following lines of code immediately after your first opening <?php tag in your main file (you should not put them into sub-files as you will need to remember to remove them) - ini_set("display_errors", "1"); error_reporting(E_ALL); You should in fact have those two settings in your master php.ini so that you don't need to remember to add/remove them from individual files at all. Quote Link to comment Share on other sites More sharing options...
atrum Posted November 7, 2009 Author Share Posted November 7, 2009 Ok, I made that change to my php.ini file. and removed them from the code on my pages. Here are the errors that I get. Warning: include(/usr/home/cdorris/restricted/constr.php) [function.include]: failed to open stream: No such file or directory in /home/cdorris/www/tools.exiled-alliance.com/test/includes/register_user.php on line 5 Warning: include() [function.include]: Failed opening '/usr/home/cdorris/restricted/constr.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /home/cdorris/www/tools.exiled-alliance.com/test/includes/register_user.php on line 5 Notice: Undefined variable: sqlcon in /home/cdorris/www/tools.exiled-alliance.com/test/includes/register_user.php on line 25 Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/cdorris/www/tools.exiled-alliance.com/test/includes/register_user.php on line 25 Error: Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted November 7, 2009 Share Posted November 7, 2009 I am still somewhat new to php, and I am trying to teach my self how to setup a global variable. Global variables are widely considered as bad practice. Quote Link to comment Share on other sites More sharing options...
Cardale Posted November 7, 2009 Share Posted November 7, 2009 Global variables can be very helpful in certain instances just don't make passwords global and probably not usernames either. Quote Link to comment Share on other sites More sharing options...
atrum Posted November 7, 2009 Author Share Posted November 7, 2009 So, I think I may be stuck using the variables on the page it self, because the include statement for some reason isn't working Quote Link to comment Share on other sites More sharing options...
Cardale Posted November 7, 2009 Share Posted November 7, 2009 You should try a persistent connection or maybe a ob_start. Really you should only have to create the connection to mysql once or what ever database and then just close it when your done using it a include should work perfect with this method. http://www.php-scripts.com/php_diary/070700.php3 Quote Link to comment Share on other sites More sharing options...
atrum Posted November 7, 2009 Author Share Posted November 7, 2009 Oh for the love of .......................... I can't believe I missed that typo in my include paths *bows head in shame* not working include("/usr/home/cdorris/restricted/constr.php"); Working include("/usr/home/cdorris/www/restricted/constr.php"); Thank you for the help any way guys. 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.