Jump to content

[SOLVED] Help setting global variables


atrum

Recommended Posts

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

Link to comment
Share on other sites

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';

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

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.

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.