/** * Run multiple comma-separated queries * * @todo Split SQL queries more cleverly (currently not needed) * * @param string SQL commands * @param ressource database handle * @return mixed result array or false */ function runSQL($sql, $dbh, $verify = false) { $result = true; foreach (explode(';', $sql) as $query) { $query = trim($query); if (empty($query)) { continue; } $query = prefix_query($query); $result = mysqli_query($dbh, $query); // error running SQL? if ($result === false) { $sql = $query; break; } } // error running SQL? if ($result === false) { if ($verify) { error("Error in SQL statement:<br/>" . "<code>{$sql}</code><br/>" . mysqli_error($dbh)); } } elseif ($result !== true) { $res = array(); for ($i = 0; $i < mysqli_num_rows($result); $i++) { $res[] = mysqli_fetch_assoc($result); } mysqli_free_result($result); return $res; } return $result; }
runSQL("DELETE FROM cache", $dbh, true); } $sql = ''; // check db encoding $res = runSQL("SHOW VARIABLES LIKE 'character_set_database'", $dbh, true); $enc = strtoupper($res[0]['Value']); if ($enc !== 'UTF8') { $sql = "ALTER DATABASE CHARACTER SET UTF8;"; } $tables = array('actors', 'cache', 'config', 'genres', 'lent', 'mediatypes', 'permissions', 'userconfig', 'users', 'userseen', 'videodata', 'videogenre'); foreach ($tables as $table) { #!! note: new code- checking table encoding on table level, too # this is for the case that the DB ist alreaady utf8 but the tables are not # # alternative approach: # SELECT TABLE_COLLATION FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'videodb' $res = runSQL("SHOW TABLE STATUS WHERE name = '" . $table . "'", $dbh, true); $enc = $res[0]['Collation']; if (!preg_match('/UTF8/i', $enc)) { $sql .= "\nALTER TABLE " . $table . " CONVERT TO CHARACTER SET UTF8;"; } } if ($sql) { // add DB prefix $sql = prefix_query($sql); warn('<b>Note:</b><br/> Install detected that your database encoding is not UTF8 (current encoding: ' . $enc . '). Please make sure to convert your database encoding to UTF8 before you continue using videoDB. If you do not upgrade your database encoding, you may experience problems using videoDB such as SQL error messages or when exporting data. To perform the conversion please execute the following SQL statements against your database: <p><pre>' . $sql . '</pre></p>'); } // signal success return true;