PHP variable import

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

PHP variable import

Post by Pazuzu156 »

I need help!

In my php scripts, i have multiple pages connecting to more than one database. I need to know how to add a variable into an external page that i will link in the other pages.

like so:

Code: Select all

<?php

$connect = mysql_connect("localhost","root","") or die ("Error: Cannot connect to host");
mysql_select_db("com") or die ("Error: Cannot connect to database");
$queryget = mysql_query("SELECT * FROM user ORDER BY id ASC") or die ("Error: Invalid Query");

?>
How do I get this from a page and link that page to another page without typing all of this out?
Last edited by Pazuzu156 on Sun Nov 21, 2010 12:38 pm, edited 1 time in total.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP variable import

Post by requinix »

What? Are you trying to find a way that you can put the database connection stuff in one place instead of copying it into each file that needs it?

Two steps.

Step 1: Put the database credentials somewhere. For example, you could have a PHP script

Code: Select all

<?php

$cred = array(
    "foo" => array(
        "host" => "localhost",
        "username" => "root",
        "password" => "",
        "database" => "com"
    ),
    // ...
);

?>
It really, really doesn't matter how you store them. What does matter is whether someone could access it from the web: by using a PHP file it'll get executed instead of displayed to the user, but by using something else (such as a .txt file) anyone who browsed to it would see the information.
In that case just put the file somewhere not web accessible. If you have a public_html directory then put the file somewhere not in there.

Step 2: Write a bit of code to grab the information from that file and use it to connect. If you used a PHP file (like above) called "cred.php" then you could have

Code: Select all

<?php

function connect($to) {
    include "cred.php";
    $connection = mysql_connect($cred[$to]["host"], $cred[$to]["username"], $cred[$to]["password"]);
    mysql_select_db($cred[$to]["database"], $connection);
    return $connection;
}

?>
Step 3 (I lied): Include that connect() function file whenever you need it, and call it like

Code: Select all

connect("foo");

But there are a billion other ways of doing this. Look around and find one you like.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: PHP variable import

Post by Pazuzu156 »

Thanks so much for this bit of information.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Post Reply