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
https://forums.phpfreaks.com/topic/17717-_get-in-an-include/
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
https://forums.phpfreaks.com/topic/17717-_get-in-an-include/#findComment-75578
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
https://forums.phpfreaks.com/topic/17717-_get-in-an-include/#findComment-75698
Share on other sites

Archived

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

×
×
  • 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.