Suchy Posted May 27, 2008 Share Posted May 27, 2008 <?php DEFINE (DB_USER, "xxx"); DEFINE (DB_PASSWORD, "xxx"); DEFINE (DB_HOST, "localhost"); $db_connection1 = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die ('lab1' . mysql_error($db_connection1)); mysql_select_db ('lab1', $db_connection1); $db_connection2 = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die ('lab2' . mysql_error($db_connection2)); mysql_select_db ('lab2', $db_connection2 ); function getRows($result) { $rows = array(); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $rows[] = $row; } } return $rows; } function in($key) { $value = $_POST[$key]; if (!isset($value)) { $value = $_GET[$key]; } return $value; } ?> <?php // ENTER DATA if(in("lab1")) // lab1 button pressed { $email = $_POST['email']; $query = "INSERT INTO lab1 (id, email) VALUES ( NULL, '$email')" ; $result = mysql_query($query); } if(in("lab2")) // lab2 button pressed { $email = $_POST['email']; $query = "INSERT INTO lab2 (id, email) VALUES ( NULL, '$email')" ; $result = mysql_query($query); } // GET DATA $info = "SELECT * FROM lab1"; $results = mysql_query($info); $from_lab1 = getRows($results); // PRINT DATA foreach($from_lab1 as $i) print("$i['email']"); ?> I can't connect to 2 databases, what is wrong with my code. This works I I try to connect to only one ? Link to comment https://forums.phpfreaks.com/topic/107532-getting-data-error/ Share on other sites More sharing options...
Barand Posted May 27, 2008 Share Posted May 27, 2008 So the two databases are "lab1" and "lab2" but what are the tablenames that you want to insert into in those databases? Link to comment https://forums.phpfreaks.com/topic/107532-getting-data-error/#findComment-551217 Share on other sites More sharing options...
Suchy Posted May 28, 2008 Author Share Posted May 28, 2008 Just noticed, the tables names are people1 and people2. Here is the right code: <?php // ENTER DATA if(in("lab1")) // lab1 button pressed { $email = $_POST['email']; $query = "INSERT INTO people1 (id, email) VALUES ( NULL, '$email')" ; $result = mysql_query($query); } if(in("lab2")) // lab2 button pressed { $email = $_POST['email']; $query = "INSERT INTO people2 (id, email) VALUES ( NULL, '$email')" ; $result = mysql_query($query); } // GET DATA $info = "SELECT * FROM people1"; $results = mysql_query($info); $from_lab1 = getRows($results); // PRINT DATA foreach($from_lab1 as $i) print("$i['email']"); ?> Link to comment https://forums.phpfreaks.com/topic/107532-getting-data-error/#findComment-551367 Share on other sites More sharing options...
Barand Posted May 28, 2008 Share Posted May 28, 2008 As they are on the same server you can prefix the tablelname with db name INSERT INTO lab1.people1 ... If on different servers then you specify the connection in the query $res = mysql_query($sql, $dbconnection_1); Link to comment https://forums.phpfreaks.com/topic/107532-getting-data-error/#findComment-551405 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.