question about querying 2 databases in php

This is a discussion on "question about querying 2 databases in php" within the PHP Forum section. This forum, and the thread "question about querying 2 databases in php are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Mar 11th, 2008, 12:32
Junior Member
Join Date: Dec 2007
Location: auckland
Age: 33
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
question about querying 2 databases in php

i am building a registration page where a user register for a username. i am able to insert this into mysql. the situation is every time data is inserted into example table1 in mysql in database1 for example at the same time i would like to extract the username, password and email from table1 based on the last inserted row and insert these values into another table ex table2 in a different database called database2.

the issue i am having is with the select query as it is not returning values from table1 for me to insert those values into table2 in database2.

NOTE = the hostname, user name, password are the same for both the tables and databases and are physically in 1 server machine itself.

presently my code is

$conn = mysql_connect($hostname, $user, $dbpassword);

if(!$conn)
{

}
else
{
mysql_select_db($database, $conn);
$insertqueryresult = mysql_query($insertqueryfortable1);
$lastid = mysql_insert_id();

$selectqueryoftable1 = "Select username, password, email from table1 where slno = '$lastid'";
slno is an autoincrement and primary key which is like a serial number

$selectunempsq = mysql_query($selectqueryoftable1);

while($rowunemps = mysql_fetch_assoc($selectunempsq))
{
$unis = $rowunemps['username'];
$psis = $rowunemps['password'];
$emis = $rowunemps['email'];
}

$insertqueryfortable2 = "Insert into table2(username, password, email) VALUES ('$unis', '$psis', '$emis')";

$unpsemresult = mysql_query($insertqueryfortable2);

the values in the 3 variables $unis, $psis, $emis are blank.

i have tried
a) while($rowunemps = mysql_fetch_array($selectunempsq))
b) creating the table2 in database1 itself to see if it works
but both these methods is not working

data is being inserted into table1 but i am not able to read the values stored in table1 and then insert into table2. i have used echoing the values of the 3 variables $unis, $psis, $emis however the values are blank. with the insertquery for table2 everytime the insert query is executed a new row is created but there are no values for the 3 fileds username, password, email in table2

initially i had $conn = mysql_connect($hostname, $user, $dbpassword); 2 times as the tables were in different database now i have only 1 mysql_connect

please advice how to fix this ideally both tables sitting in different databases.

thanks.
Reply With Quote

  #2 (permalink)  
Old Mar 11th, 2008, 19:33
CloudedVision's Avatar
Nerdy Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 828
Blog Entries: 6
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via AIM to CloudedVision Send a message via MSN to CloudedVision Send a message via Skype™ to CloudedVision
Re: question about querying 2 databases in php

You use mysql_select_db to select which database is your default. Since table2 isn't in the selected database, mysql can't find it. If you use "database2.table2" instead of "table2" should make mysql query the right database.
__________________
echo "Take it easy, ".$CloudedVision;
.links { site: other-road-design; blog: only-nerds-allowed; project: resource-fish; organization: ARMIES6; }
<quote>&quot;I think it's wrong that only one company makes the game Monopoly&quot; - <name>Steven Wright</name></quote>
Last Blog Entry: Nerd Poster (Jun 2nd, 2008)
Reply With Quote
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Databases? Apocalyptic Poet Classic ASP 1 Apr 23rd, 2008 12:24
Querying Microsoft SQL server 2005 alexgeek Classic ASP 14 Sep 30th, 2007 01:28
Querying Dates Steve Mellor Databases 1 Apr 13th, 2007 14:59
Populating databases DennisK PHP Forum 9 Nov 16th, 2006 15:55
Form Element has no properties when querying antonyx JavaScript Forum 13 Aug 25th, 2006 14:06


All times are GMT. The time now is 22:15.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43