Jump to content


Photo

$_GET in an include


  • Please log in to reply
6 replies to this topic

#1 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 16 August 2006 - 08:55 AM

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.
<?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."); 
?>

I then want to include this file like this
include("db.php?database=whatever");

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


and

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


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

Thanks,
Tom



Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#2 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 16 August 2006 - 09:04 AM

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?
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 16 August 2006 - 09:08 AM

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

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 August 2006 - 09:08 AM

database.php
<?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.");
} 
?>

<?php

include("database.php");


database_connection();

?>


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 16 August 2006 - 12:49 PM

include does not work in that way.

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

a.php:
<?php

echo $a;

?>

b.php:
<?php

$a = 'Hello World!';

include 'a.php';

?>

output:
Hello World!


#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 16 August 2006 - 02:51 PM

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:
<?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.");
} 
?>


Now we use the following to connect to the database:
include 'database.php';

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


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

#7 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 16 August 2006 - 04:59 PM

Or you could just do
$database = 'whatever';
include("db.php");

But I recommed the function too :)
I'm just a guy who likes to code for fun...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users