Hello,
Database backup with php is easy to use from the website administration panel.
My backup code below works fine
But I want to add a feature
If the column type is “DEFAULT NULL” and the content is empty I want it to add NULL
if ( strlen($row[$j])>0 ){
$return .= "'" . $row[$j] . "'";
} else {
$return .= "NULL";
}
I am using the code as above in the data backup section but it is adding NULL to all empty fields.
This causes NULL in fields that are not “DEFAULT NULL”
How can I check if the column is “DEFAULT NULL” in the above code?
$return .= 'DROP TABLE IF EXISTS '.$table.';';
$pstm2 = $db->query('SHOW CREATE TABLE '.$table);
$row2 = $pstm2->fetch(PDO::FETCH_NUM);
$ifnotexists = str_replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS', $row2[1]);
$return .= "\n".$ifnotexists.";\n";
$return .= "\n--\n" ;
$return .= '-- Tablonun veri dökümü `' . $table . '`' . "\n" ;
$return .= "--\n\n" ;
for ($i = 0; $i < $num_fields; $i++) {
@set_time_limit(0);
$s = 0;
while ($row = $sutun_sayisi->fetch(PDO::FETCH_NUM)) {
$s++;
$return .= 'INSERT INTO `' . trim($table) . '` VALUES(';
for ($j = 0; $j < $num_fields; $j++) {
$row[$j] = addslashes($row[$j]);
$row[$j] = preg_replace("/\r\n/", "\\r\\n", $row[$j]);
if ( strlen($row[$j])>0 ){
$return .= "'" . $row[$j] . "'";
} else {
$return .= "NULL";
}
if ($j < ($num_fields - 1)) {
$return .= ', ';
}
}
$return .= ");\n";
// Tablo verinin dökümün altına ekler
if ( $s == ( $numrow - 0 ) ){
$return .="\n-- --------------------------------------------------------\n";
}
// Okunan veriyi dosyaya yaz
fwrite($handle, $return);
$return=null;
}// end while
// Tabloda veri yoksa tablo yapısını dosyaya yaz
$satir = $sutun_sayisi->rowCount();
if($satir == '0'){
fwrite($handle, $return);
$return=null;
}
}//end for