我有一個包含大量列的表。我有一個函數將我的記錄複製到具有更新的自動遞增ID的新行中。使用空變量複製mysql行
該函數完美工作,但是,在我的一些INT列,有時我有一個NULL作爲默認值。當記錄被複制,就會使我空放置到0
我猜想那是因爲我在做'" . value . "'
誰能幫我找出我怎麼能讓NULL值插入爲" . NULL . "
和保持其他值爲'" . value . "'
?
編輯我無法區分空值和空值。我試過空()和is_null()和沒有價值的varchar和一個NULL值的INT沒有顯示
注一個區別:我明白,我使用的是過時的MySQL擴展。現在,我只是想正確處理我的空變量。
function duplicateRow($table, $id_field, $id_value)
{
// copy content of the record you wish to clone
$entity = mysql_fetch_array(mysql_query("SELECT * FROM {$table} WHERE {$id_field}={$id_value}"), 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_field] = "";
// insert cloned copy of the original record
mysql_query("INSERT INTO {$table} (".implode(", ",array_keys($entity)).") VALUES ('".implode("', '",array_values($entity))."')") or die(mysql_error());
//returns the new id
return mysql_insert_id();
}
你有這裏的一切都非常有活力的,並且那是偉大的,但是當你要開始爲特定的使用值是如何在你的領域處理,你必須放棄一些動態性的。指定字段並將它們合併爲一個'NULL'字符串,或更改'。價值。「來處理空值。 – paqogomez
我沒有寫這個函數,所以我很難分解發生了什麼@paqogomez – user3566265
爲什麼不使用['INSERT ... SELECT'](http://dev.mysql.com/doc /en/insert-select.html)? – eggyal