Jump to content

Archived

This topic is now archived and is closed to further replies.

tomfmason

$_GET in an include

Recommended Posts

I have a control pannel that I use to manage all of my domains from. I am wanting to be able to select the database dynamicly. Well what I am trying to do is this.
[code]
<?php 
$dbhost = 'yourhost';
$dbusername = 'username';
$dbpasswd = 'password';
$database = $_GET['database'];

$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd") 
    or die ("Couldn't connect to server.");
$db = mysql_select_db("$database", $connection)
    or die("Couldn't select database.");
?>[/code]

I then want to include this file like this [code=php:0]include("db.php?database=whatever");[/code]

I get the following error messages
[code]
Warning: main(db.php?database=mydb) [function.main]: failed to open stream: Invalid argument

[/code]

and

[code]
Warning: main() [function.include]: Failed opening 'db.php?database=mydb' for inclusion

[/code]

This maynot even be possible with an include. Any suggestions.

Thanks,
Tom


Share this post


Link to post
Share on other sites
No I don't think you can do that. Why not make your connection code into a function and pass in the database name as a parameter?

Share this post


Link to post
Share on other sites
I agree but about the function. I will do that. But you can include it like that. You just have to use the ful url instead of just the filename. Include issues..php 101 allover again..lol

Thanks,
Tom

Share this post


Link to post
Share on other sites
database.php
[code]
<?php  

function database_connection() {

$dbhost = 'yourhost';
$dbusername = 'username';
$dbpasswd = 'password';
$database = $_GET['database'];

$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd")  
   or die ("Couldn't connect to server.");
$db = mysql_select_db("$database", $connection)
   or die("Couldn't select database.");
}
?>
[/code]

[code]
<?php

include("database.php");


database_connection();

?>

[/code]

Share this post


Link to post
Share on other sites
include does not work in that way.

include is just like copying and pasting the code into your script manually.

a.php:[code]<?php

echo $a;

?>[/code]

b.php:
[code]<?php

$a = 'Hello World!';

include 'a.php';

?>[/code]

output:
[code]Hello World![/code]

Share this post


Link to post
Share on other sites
You cannot send URL parameters through an include or rquire statement. Instead What I'd do is what redarrow suggested by pass the database through the function like so:
database.php:
[code=php:0]<?php 

function dbConnect($db)
{
    // we'll define $connection as global so we can use this outside of the dbConnect function
    global $connection;

    $dbhost = 'yourhost';
    $dbusername = 'username';
    $dbpasswd = 'password';

    $connection = mysql_pconnect($dbhost, $dbusername, $dbpasswd) or die ("Couldn't connect to server.");

    mysql_select_db($db, $connection)  or die("Couldn't select database.");
}
?>[/code]


Now we use the following to connect to the database:
[code=php:0]include 'database.php';

// now we call our dbConnect function
dbConnect('database_name_here');[/code]


Change database_name_here to the name of the database you want to connect to.

Share this post


Link to post
Share on other sites
Or you could just do
[code=php:0]$database = 'whatever';
include("db.php");
[/code]

But I recommed the function too :)

Share this post


Link to post
Share on other sites

×

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.