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='someemail@gmail.com' ") or die(mysql_error());

?>

Link to comment
Share on other sites

Hi

 

Do it as 2 seperate queries.

 

$sql="SELECT lastname, firstname, email FROM table1 WHERE email='someemail@gmail.com' ";
$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();

 

 

 

 

 

Link to comment
Share on other sites

  • 4 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.