Jump to content

Site Wide DB Connection


billckr

Recommended Posts

Greetings all,

 

I like to make a config.php file and place it outside my document root for security. I do this all the time. Then just inlcude the config in any file that I need information for.

 

include('../includes/config.php');

 

This works most of the time for any DB connection information a piece of code might need. However, sometimes I like to place all my functions in a file called functions.php to keep them separate and have a single place to work with them.

 

I have not done any coding in about two years and just started back and for the life of me I can't get any connection attempt inside any of my functions to use the DB connection info from the config.php even if I include the file right inside the functions.php at the top. Even when I put all my functions inside the config file itself they still have no access to the connection vars.

 

I remember that I used to be able to use a global so some type to do this.  The important part is getting DB connection access to any functions that I write.

 

Here is an example of a plain config that I might start with;

 

//* Database connection information.. *//

$DBHost="localhost"; //* Database Hostname
$DBName="some_db"; //* Database Name
$DBUser="some_user"; //* Database Username
$DBPass="some_pass"; //* Database Password

$db = mysql_connect($DBHost, $DBUser, $DBPass);
mysql_select_db($DBName);

 

Can someone show me where I'm going wrong and the modern excepted method of doing this?

 

I would greatly appreciated it!!

 

Thank you.

 

 

 

 

 

Link to comment
Share on other sites

$host = "host";
$username = "username";
$password = "password";
$db_name = "db_name";
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB"); 

 

You forgot your quotes or apostrophes around your vars in mysql_connect

Link to comment
Share on other sites

If you only use a single database connection in your script, it is already available inside functions because the mysql_ statements use the last created connection by default.

 

However, if you need more than one connection or you are writing your code to be general purpose so that it uses a specific connection, the preferred method would be to pass the connection into the function as a parameter when the function is called.

Link to comment
Share on other sites

If you only use a single database connection in your script, it is already available inside functions because the mysql_ statements use the last created connection by default.

 

However, if you need more than one connection or you are writing your code to be general purpose so that it uses a specific connection, the preferred method would be to pass the connection into the function as a parameter when the function is called .

 

 

PFMaBiSmAd,

 

Thanks for the reply. I'm glad to know that's how it's supposed to work but currently it's not 'could be my fault'.  Using the config above if I have a funtion that like the below I get an mysql connection error that apache@localhost cannot connect meaning that it's not picking up my connection info for the config.php even if the function is written inside the actual config.php itself.

 

<?php

/* Select Clients from DB */
function client_select()
{
  $con = mysql_connect("$BDhost", "$DBUser", "$DBPass");
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db($DBName, $con);
    
    $result = mysql_query("SELECT * FROM some table");
    
    while ($row = mysql_fetch_array($result)) {
        echo $row['FirstName'] . " " . $row['LastName'];
        echo "<br />";
    }
    
    mysql_close($con);
}

?>

 

Any thoughts?

 

Link to comment
Share on other sites

A) You switched from talking about a connection to talking about the connection info. Those are not the same thing.

 

B) You should not be making a connection per query (the function code you just posted.) A function who's purpose is to execute a query and return (or echo) the results of a specific query should not be making a database connection.

 

For the code you posted, you would need to pass the $BDhost, $DBUser, and $DBPass connection info into the function as parameters when you call the function.

Link to comment
Share on other sites

A) You switched from talking about a connection to talking about the connection info. Those are not the same thing.

 

B) You should not be making a connection per query (the function code you just posted.) A function who's purpose is to execute a query and return (or echo) the results of a specific query should not be making a database connection.

 

For the code you posted, you would need to pass the $BDhost, $DBUser, and $DBPass connection info into the function as parameters when you call the function.

 

 

I don't think I switched what I was talking about. If I was misleading I apologize.

 

or the code you posted, you would need to pass the $BDhost, $DBUser, and $DBPass connection info into the function as parameters when you call the function.

 

Ok I think that is what I'm asking. How do I do this?

 

 

Link to comment
Share on other sites

OK, I'm still not sure what I'm doing wrong here. I've tried every way I could think of. I really appreciate everyone's patience.

 

Would someone be so kind as to "using the example below" show me how to pass the connection info from my config.php into the function below so I can use to select data from a database.

 

config.php

 

<?php

$DBHost="localhost"; //* Database Hostname
$DBName="dbname"; //* Database Name
$DBUser="dbuser"; //* Database Username
$DBPass="dbpass"; //* Database Password

$db = mysql_connect($DBHost, $DBUser, $DBPass);
mysql_select_db($DBName);

?>

 

Fuction:

 

<?php

/* Select Clients from DB */
function client_select()
{
    
    $result = mysql_query("SELECT FirstName FROM some table");
    
    while ($row = mysql_fetch_array($result)) {
        echo $row['FirstName'];
        echo "<br />";
    }
    
    mysql_close($con);
}

?>

 

 

So, the question is: How can I modify the function above or otherwise get the connection information from the config to it so I can place this fucntion in any place or any page that has my config.php included?

 

Thank you for the assistance.

 

 

 

 

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.