note 92513 added to function.mysql-fetch-array
| From: | Tristan at osu1 dot php dot net | Date: | Tue, 28 Jul 2009 00:42:21 +0000 |
| Subject: | note 92513 added to function.mysql-fetch-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158802@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.
// 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.
----
Server IP: 69.147.83.197
Probable Submitter: 113.146.99.47
----
Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-array.php
Edit -- https://master.php.net/note/edit/92513
Del: integrated -- https://master.php.net/note/delete/92513/integrated
Del: useless -- https://master.php.net/note/delete/92513/useless
Del: bad code -- https://master.php.net/note/delete/92513/bad+code
Del: spam -- https://master.php.net/note/delete/92513/spam
Del: non-english -- https://master.php.net/note/delete/92513/non-english
Del: in docs -- https://master.php.net/note/delete/92513/in+docs
Del: other reasons-- https://master.php.net/note/delete/92513
Reject -- https://master.php.net/note/reject/92513
Search -- https://master.php.net/manage/user-notes.php