473,396 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. $conn = mysql_connect($hostname, $user, $dbpassword);
  2.  
  3. if(!$conn)
  4.  
  5. }
  6. else
  7. {
  8. mysql_select_db($database, $conn);
  9. $insertqueryresult = mysql_query($insertqueryfortable1);       
  10. $lastid = mysql_insert_id();
  11.  
  12. $selectqueryoftable1 = "Select username, password, email from table1 where slno = '$lastid'";
  13. slno is an autoincrement and primary key which is like a serial number
  14.  
  15. $selectunempsq = mysql_query($selectqueryoftable1);
  16.  
  17. while($rowunemps = mysql_fetch_assoc($selectunempsq))
  18. {
  19. $unis = $rowunemps['username'];
  20. $psis = $rowunemps['password'];
  21. $emis = $rowunemps['email'];
  22. }
  23.  
  24. $insertqueryfortable2 = "Insert into table2(username, password, email) VALUES ('$unis', '$psis', '$emis')";
  25.  
  26. $unpsemresult = mysql_query($insertqueryfortable2);
  27.  
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.
Mar 11 '08 #1
2 1381
ronverdonk
4,258 Expert 4TB
This is the last warning after several (incl PMs) previous: As a full member now, you should know that we expect your code to be posted in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use the tags in future.

MODERATOR.
Mar 11 '08 #2
ronverdonk
4,258 Expert 4TB
You should test the status of each MySQL function after executing it by checking the false or true result. A statement should be something like:[php]$selectunempsq = mysql_query($selectqueryoftable1)
or die("SELECT error: ".mysql_error());[/php]That is the only way to see if the error you presumed is really that and not the result of the execution of an invalid SQL statement.

Ronald
Mar 11 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: alwaysawn | last post by:
Hi, I have databases on two different machines which I want to select from in one query, any good way of doing this without moving tables from one machine to another? Thanks, Alwaysawn
3
by: Henri | last post by:
Hello, I am quite new to ms-sql and I have a problem : I want to create an SQL request which would copy serveral records from a table in a given database to another table (with exactly the same...
1
by: Rick Brandt | last post by:
I know that a heterogeneous query joining tables from two different servers has performance penalties but is the same true when joining tables from two different databases on the same SQL Server...
2
by: Steve | last post by:
Hi; I am looking for suggestions about how to solve a problem using tsql. I have been asked to create a report concerning 4 tables. Each of the 4 tables is in its own database. The 4...
6
by: Greg | last post by:
I am working on a project that will have about 500,000 records in an XML document. This document will need to be queried with XPath, and records will need to be updated. I was thinking about...
5
by: Shane | last post by:
I wonder if someone has any ideas about the following. I am currently producing some reports for a manufacturing company who work with metal. A finished part can contain multiple sub-parts to...
6
by: Ober | last post by:
I'm having trouble with my security model, Application_Start, and accessing my database. My ASP.NET app is only going to be running in an intranet environment (not on the public Internet). ...
5
by: sql_er | last post by:
Guys, I have an XML file which is 233MB in size. It was created by loading 6 tables from an sql server database into a dataset object and then writing out the contents from this dataset into an...
1
by: plmanikandan | last post by:
Hi, I'm having two databases in the name of n1,n2 having same design I want to query both the databases by giving the database name in query Databases N1 N2 Table(both database contains same...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.