Jump to content

Getting Data Error


Suchy

Recommended Posts

<?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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.