note 58735 added to function.mysql-fetch-array

From: Date: Mon, 14 Nov 2005 13:49:17 +0000
Subject: note 58735 added to function.mysql-fetch-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-98545@lists.php.net to get a copy of this message
Here's a quick way to duplicate or clone a record to the same table using only 4 lines of code: // first, get the highest id number, so we can calc the new id number for the dupe // second, get the original entity // third, increment the dupe record id to 1 over the max // finally insert the new record - voila - 4 lines! $id_max = mysql_result(mysql_query("SELECT MAX(id) FROM table_name"),0,0) or die("Could not execute query"); $entity = mysql_fetch_array(mysql_query("SELECT * FROM table." WHERE id='$id_original'),MYSQL_ASSOC) or die("Could not select original record"); // MYSQL_ASSOC forces a purely associative array and blocks twin key dupes, vitally, it brings the keys out so they can be used in line 4 $entity["id"]=$id_max+1; mysql_query("INSERT INTO it_pages (".implode(", ",array_keys($Entity)).") VALUES ('".implode("', '",array_values($Entity))."')"); Really struggled in cracking this nut - maybe there's an easier way out there? Thanks to other posters for providing inspiration. Good luck - Tim ---- Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58735 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58735&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58735&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58735&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58735&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58735&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58735&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58735&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58735&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#98545) next »