Current location: Hot Scripts Forums » Programming Languages » PHP » select variable from one database to use in another?!??


select variable from one database to use in another?!??

Reply
  #1 (permalink)  
Old 07-07-06, 12:54 PM
Deansatch Deansatch is offline
Coding Addict
 
Join Date: Jul 2006
Location: Northumberland
Posts: 375
Thanks: 0
Thanked 0 Times in 0 Posts
select field from one database to use as variable in selecting a table in another?!??

I want to write this and have $username replaced with the contents of the username field from database1 and "table1_$username" is a table in database2

PHP Code:

$query mysql_query("SELECT * FROM table1_$username ORDER BY id DESC") or die(mysql_error()); 

I tried this during pulling my hair out and I think I am on the right track but can't get it to work. Where am I going wrong?

PHP Code:

require("config.php");

require(
"functions.php");
$connection mysql_connect($host,$usr,$pwd);
mysql_select_db('database1') or die('Cannot select database'); 
$username mysql_db_query($db"SELECT username FROM table_customers where id='$id'"$connection);


$connection mysql_connect($host,$usr,$pwd);
mysql_select_db('database2') or die('Cannot select database'); 

$query mysql_query("SELECT * FROM table1_$username ORDER BY id DESC") or die(mysql_error()); 
but it just doesn't work. I can either get
"Table 'databse2.table1_' doesn't exist "
or
"Table 'databse2.table1_Resource' doesn't exist "
__________________
Aye!

Last edited by Deansatch; 07-07-06 at 01:02 PM.
Reply With Quote
  #2 (permalink)  
Old 07-07-06, 02:24 PM
duesi's Avatar
duesi duesi is offline
Wannabe Coder
 
Join Date: Jun 2006
Posts: 225
Thanks: 0
Thanked 0 Times in 0 Posts
$username does not yet contain the username, it is just a reference to your result.

you need :

PHP Code:

require("config.php");

require(
"functions.php");
$connection mysql_connect($host,$usr,$pwd);
mysql_select_db('database1') or die('Cannot select database'); 
$username mysql_db_query($db"SELECT username FROM table_customers where id='$id'"$connection

//Normally you need a loop here, but I assume you have a UNIQUE
//KEY on id??
$row mysql_fetch_array($resultMYSQL_ASSOC));
$username $row["username"];

$connection mysql_connect($host,$usr,$pwd);
mysql_select_db('database2') or die('Cannot select database'); 

$query mysql_query("SELECT * FROM table1_$username ORDER BY id DESC") or die(mysql_error()); 
But like in your thread

http://www.programmingtalk.com/showthread.php?t=30765

I wonder why you are doing this, it is very uncommon to have dynamic tablenaes, it makes everything very complicated....

Cheers!
__________________
Duesi

"One of the great skills in using any language is knowing what not to use, what not to say" (Ron Jeffries)

http://www.swissbytes.de
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Why won't select list item insert into database? gavacho PHP 2 12-05-08 06:38 PM
Dynamic select box from database values PrashantJsp JavaScript 2 05-04-05 12:29 AM
Select say last 5 database entrys only Bonzo PHP 4 08-18-04 06:44 PM
PHP Can't select database EST PHP 4 08-19-03 10:14 AM


All times are GMT -5. The time now is 07:20 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.