Jump to content

INSERT-INTO-SELECT with concatenated variables


adriscoll

Recommended Posts

Hello.

 

I have  a simple enough code that takes information from one table and drops it into another.  This is great, but I have 2a new complexities that I have been unable to code correctly.

 

A.  'lastname', 'firstname' on table1 need to be combined into 'name'

 

How & Where do I combine these strings and then pass them?

 

<?php
include('dbconfig.php');


// Make a MySQL Connection
mysql_connect("localhost", "$user", "$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());

$result = mysql_query(
"INSERT INTO table2 (lastname, firstname, email)
SELECT lastname, firstname, email
FROM table1
WHERE email='[email protected]' ") or die(mysql_error());

?>

Hi

 

Do it as 2 seperate queries.

 

$sql="SELECT lastname, firstname, email FROM table1 WHERE email='[email protected]' ";
$sql=mysql_query($sql) or die();
while($row=mysql_fetch_array($sql){
$name=$row['lastname'] . $row['firstname'];
}

$sql="INSERT INTO table2 " .  $name;
mysql_query($sql) or die();

 

 

 

 

 

  • 4 weeks later...

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.