note 92513 modified in function.mysql-fetch-array by danbrown
| From: | danbrown@php.net | Date: | Mon, 03 Aug 2009 17:10:21 +0000 |
| Subject: | note 92513 modified in function.mysql-fetch-array by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-159053@lists.php.net to get a copy of this message | ||
Here's a quicker way to clone a record. Only 3 lines of code instead of 4. But the table must
have an auto-incremented id.
I took the code from Tim and altered it. Props to Tim.
<?php
// copy content of the record you wish to clone
$entity = mysql_fetch_array(mysql_query("SELECT * FROM table_name WHERE id='$id_to_be
cloned'"), MYSQL_ASSOC) or die("Could not select original record");
// set the auto-incremented id's value to blank. If you forget this step, nothing will work
because we can't have two records with the same id
$entity["id"] = "";
// insert cloned copy of the original record
mysql_query("INSERT INTO table_name (".implode(", ",array_keys($entity)).")
VALUES ('".implode("', '",array_values($entity))."')");
// if you want the auto-generated id of the new cloned record, do the following
$newid = mysql_insert_id();
?>
There you go.
--was--
Here's a quicker way to clone a record. Only 3 lines of code instead of 4. But the table must
have an auto-incremented id.
I took the code from Tim and altered it. Props to Tim.
// copy content of the record you wish to clone
$entity = mysql_fetch_array(mysql_query("SELECT * FROM table_name WHERE id='$id_to_be
cloned'"), MYSQL_ASSOC) or die("Could not select original record");
// set the auto-incremented id's value to blank. If you forget this step, nothing will work
because we can't have two records with the same id
$entity["id"] = "";
// insert cloned copy of the original record
mysql_query("INSERT INTO table_name (".implode(", ",array_keys($entity)).")
VALUES ('".implode("', '",array_values($entity))."')");
// if you want the auto-generated id of the new cloned record, do the following
$newid = mysql_insert_id();
There you go.
http://php.net/manual/en/function.mysql-fetch-array.php