Jump to content

$_GET in an include


tomfmason

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


Link to comment
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]
Link to comment
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]
Link to comment
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.
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.