Welcome Guest, Not a member yet? Register   Sign In
NULLs in DB backup
#1

[eluser]Seppo[/eluser]
Using current SVN version, all empty fields are saved as NULL. This can be awful when restoring the DB...

Current SVN version (mysql_utility.php, rows 211 to 243)
Code:
foreach ($row as $v)
                {
                    // Do a little formatting...
                    $v = str_replace(array("\x00", "\x0a", "\x0d", "\x1a"), array('\', '\n', '\r', '\Z'), $v);
                    $v = str_replace(array("\n", "\r", "\t"), array('\n', '\r', '\t'), $v);
                    $v = str_replace('\\', '\\\\',    $v);
                    $v = str_replace('\'', '\\\'',    $v);
                    $v = str_replace('\\\n', '\n',    $v);
                    $v = str_replace('\\\r', '\r',    $v);
                    $v = str_replace('\\\t', '\t',    $v);

                    // Is the value NULL?
                    if ($v == NULL)
                    {
                        $val_str .= 'NULL';
                    }
                    else
                    {
                        // Escape the data if it's not an integer
                        if ($is_int[$i] == FALSE)
                        {
                            $val_str .= $this->db->escape($v);
                        }
                        else
                        {
                            $val_str .= $v;
                        }                    
                    }                    
                    
                    // Append a comma
                    $val_str .= ', ';
                    $i++;
                }

My suggestion

Code:
foreach ($row as $v)
                {
                    // Is the value NULL?
                    if ($v === NULL)
                    {
                        $val_str .= 'NULL';
                    }
                    else
                    {
                        // Do a little formatting...
                        $v = str_replace(array("\x00", "\x0a", "\x0d", "\x1a"), array('\', '\n', '\r', '\Z'), $v);
                        $v = str_replace(array("\n", "\r", "\t"), array('\n', '\r', '\t'), $v);
                        $v = str_replace('\\', '\\\\',    $v);
                        $v = str_replace('\'', '\\\'',    $v);
                        $v = str_replace('\\\n', '\n',    $v);
                        $v = str_replace('\\\r', '\r',    $v);
                        $v = str_replace('\\\t', '\t',    $v);
    
                        // Escape the data if it's not an integer
                        if ($is_int[$i] == FALSE)
                        {
                            $val_str .= $this->db->escape($v);
                        }
                        else
                        {
                            $val_str .= $v;
                        }                    
                    }                    
                    
                    // Append a comma
                    $val_str .= ', ';
                    $i++;
                }
#2

[eluser]Seppo[/eluser]
The fix commited (r824) is wrong, after the str_replace it will always return a string, that's why I moved that before here...
I posted on the bug tracker, but I am not able to reopen the case, so I post here in case you miss that.
#3

[eluser]Derek Jones[/eluser]
This is what I get for 7am pre-coffee coding. Thanks, Seppo.




Theme © iAndrew 2016 - Forum software by © MyBB