tomfmason Posted August 16, 2006 Share Posted August 16, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/17717-_get-in-an-include/ Share on other sites More sharing options...
king arthur Posted August 16, 2006 Share Posted August 16, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/17717-_get-in-an-include/#findComment-75576 Share on other sites More sharing options...
tomfmason Posted August 16, 2006 Author Share Posted August 16, 2006 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..lolThanks,Tom Quote Link to comment https://forums.phpfreaks.com/topic/17717-_get-in-an-include/#findComment-75577 Share on other sites More sharing options...
redarrow Posted August 16, 2006 Share Posted August 16, 2006 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]<?phpinclude("database.php");database_connection();?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17717-_get-in-an-include/#findComment-75578 Share on other sites More sharing options...
Jenk Posted August 16, 2006 Share Posted August 16, 2006 include does not work in that way.include is just like copying and pasting the code into your script manually.a.php:[code]<?phpecho $a;?>[/code]b.php:[code]<?php$a = 'Hello World!';include 'a.php';?>[/code]output:[code]Hello World![/code] Quote Link to comment https://forums.phpfreaks.com/topic/17717-_get-in-an-include/#findComment-75647 Share on other sites More sharing options...
wildteen88 Posted August 16, 2006 Share Posted August 16, 2006 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 functiondbConnect('database_name_here');[/code]Change database_name_here to the name of the database you want to connect to. Quote Link to comment https://forums.phpfreaks.com/topic/17717-_get-in-an-include/#findComment-75698 Share on other sites More sharing options...
Ifa Posted August 16, 2006 Share Posted August 16, 2006 Or you could just do[code=php:0]$database = 'whatever';include("db.php");[/code]But I recommed the function too :) Quote Link to comment https://forums.phpfreaks.com/topic/17717-_get-in-an-include/#findComment-75777 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.