DotDragnet
May 23, 2012, 10:29:45 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: follow us on twitter @dotdragnet
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: PHP My Admin SQL question  (Read 491 times)
Charisma Bypass
Hero Member
*****
Posts: 556



View Profile Awards
« on: January 14, 2011, 11:46:37 AM »

Anyone know a way to update all varchars across all tables in a database to 255 ?  In one SQL shot?
Thanks!
Logged
JasonD
Global Moderator
Hero Member
*****
Posts: 551



View Profile Awards
« Reply #1 on: January 14, 2011, 02:12:52 PM »

I haven't used php my admin for years but I wouldn't have thought it a much requested feature. Certainly not possible in pure SQL.

Code:
$tables = mysql_query('SHOW TABLES');
while ($row = mysql_fetch_assoc($tables)) {
    $table = current($row);
    $result = mysql_query('SHOW COLUMNS FROM ' . $table);
    while ($row = mysql_fetch_assoc($result)) {
        if (preg_match('/^varchar\((\d+)\)$/', $row['Type'], $match)
          && $match[1] < 255) {
            $sql = 'ALTER TABLE ' . $table . ' CHANGE ' . $row['Field'] . ' '
              . $row['Field'] . ' varchar(255)';
            if ($row['Null'] == 'NO') {
                $sql .= ' NOT NULL';
            }
            if ($row['Default']) {
                $sql .= ' default \'' . mysql_real_escape_string($row['Default']) . '\'';
            }
            echo $sql, ";\n";
            // or uncomment
            // mysql_query($sql);
        }
    }
}
Logged
Charisma Bypass
Hero Member
*****
Posts: 556



View Profile Awards
« Reply #2 on: January 16, 2011, 03:41:40 PM »

Many thanks Jason
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!