sudhakararaog Posted March 11, 2008 Share Posted March 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/95600-question-about-querying-2-databases-in-php/ Share on other sites More sharing options...
beebum Posted March 11, 2008 Share Posted March 11, 2008 You could really simplify your life if you would put all related tables in the same database. Quote Link to comment https://forums.phpfreaks.com/topic/95600-question-about-querying-2-databases-in-php/#findComment-489414 Share on other sites More sharing options...
redarrow Posted March 11, 2008 Share Posted March 11, 2008 This should let you use two database at once i think have a go tell me ok........ <?php $link[] = mysql_connect('localhost', 'mysql_user', 'mysql_password'); $link[] = mysql_connect('192.0.0.0', 'mysql_user', 'mysql_password'); $database_name=array("databse_name1","database_name2"); for($i=0; $i<count($databse_name); $i++){ foreach($link as $x){ mysql_select_db($database,$x); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/95600-question-about-querying-2-databases-in-php/#findComment-489438 Share on other sites More sharing options...
sasa Posted March 11, 2008 Share Posted March 11, 2008 try $conn = mysql_connect($hostname, $user, $dbpassword); if(!$conn) { } else { $db1 = mysql_select_db($database, $conn); //$insertqueryresult = mysql_query($insertqueryfortable1); //$lastid = mysql_insert_id(); $selectqueryoftable1 = "Select username, password, email from table1 where 1 ORDER BY slno DESC LIMIT 1"; $selectunempsq = mysql_query($selectqueryoftable1,$db1); $rowunemps = mysql_fetch_assoc($selectunempsq); $unis = $rowunemps['username']; $psis = $rowunemps['password']; $emis = $rowunemps['email']; $db2 = mysql_select_db($database2, $conn); $insertqueryfortable2 = "Insert into table2(username, password, email) VALUES ('$unis', '$psis', '$emis')"; $unpsemresult = mysql_query($insertqueryfortable2,$db2); Quote Link to comment https://forums.phpfreaks.com/topic/95600-question-about-querying-2-databases-in-php/#findComment-489463 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.