/** * {@inheritdoc} */ public function write_data($table_name) { if (!$this->is_initialized) { throw new extractor_not_initialized_exception(); } $col_types = sqlite_fetch_column_types($this->db->get_db_connect_id(), $table_name); $sql = "SELECT *\n\t\t\tFROM {$table_name}"; $result = sqlite_unbuffered_query($this->db->get_db_connect_id(), $sql); $rows = sqlite_fetch_all($result, SQLITE_ASSOC); $sql_insert = 'INSERT INTO ' . $table_name . ' (' . implode(', ', array_keys($col_types)) . ') VALUES ('; foreach ($rows as $row) { foreach ($row as $column_name => $column_data) { if (is_null($column_data)) { $row[$column_name] = 'NULL'; } else { if ($column_data == '') { $row[$column_name] = "''"; } else { if (strpos($col_types[$column_name], 'text') !== false || strpos($col_types[$column_name], 'char') !== false || strpos($col_types[$column_name], 'blob') !== false) { $row[$column_name] = sanitize_data_generic(str_replace("'", "''", $column_data)); } } } } $this->flush($sql_insert . implode(', ', $row) . ");\n"); } }
function get_table_defn($table) { if (PHP_VERSION < 5) { return false; } // PHP5 only return sqlite_fetch_column_types($table, $this->conn, SQLITE_ASSOC); }
/** * 获取数据表结构 * @param tbl_name 表名称 */ public function getTable($tbl_name) { $cols = sqlite_fetch_column_types($tbl_name, $this->conn, SQLITE_ASSOC); $columns = array(); foreach ($cols as $column => $type) { $columns[] = array('Field' => $column); } return $columns; }
function sql_fetch_column_types($data, $type = SQLITE_ASSOC) { global $con; if (defined('DB_TYPE')) { if (DB_TYPE == 'sqlite') { $sql = sqlite_fetch_column_types($data, $con, $type); } return $sql; } }
function GetFields($table_name) { $c_types = sqlite_fetch_column_types($table_name, $this->db_res, SQLITE_ASSOC); $results = array(); while (list($field, $type) = each($c_types)) { $new_t_obj = new stdClass(); $new_t_obj->Field = $field; $new_t_obj->Type = strtolower($type); $results[] = $new_t_obj; } return $results; }
function write_data($table_name) { global $db; static $proper; if (is_null($proper)) { $proper = version_compare(PHP_VERSION, '5.1.3', '>='); } if ($proper) { $col_types = sqlite_fetch_column_types($db->db_connect_id, $table_name); } else { $sql = "SELECT sql\n\t\t\t\tFROM sqlite_master\n\t\t\t\tWHERE type = 'table'\n\t\t\t\t\tAND name = '" . $table_name . "'"; $table_data = sqlite_single_query($db->db_connect_id, $sql); $table_data = preg_replace('#CREATE\\s+TABLE\\s+"?' . $table_name . '"?#i', '', $table_data); $table_data = trim($table_data); preg_match('#\\((.*)\\)#s', $table_data, $matches); $table_cols = explode(',', trim($matches[1])); foreach ($table_cols as $declaration) { $entities = preg_split('#\\s+#', trim($declaration)); $column_name = preg_replace('/"?([^"]+)"?/', '\\1', $entities[0]); // Hit a primary key, those are not what we need :D if (empty($entities[1]) || strtolower($entities[0]) === 'primary' && strtolower($entities[1]) === 'key') { continue; } $col_types[$column_name] = $entities[1]; } } $sql = "SELECT *\n\t\t\tFROM {$table_name}"; $result = sqlite_unbuffered_query($db->db_connect_id, $sql); $rows = sqlite_fetch_all($result, SQLITE_ASSOC); $sql_insert = 'INSERT INTO ' . $table_name . ' (' . implode(', ', array_keys($col_types)) . ') VALUES ('; foreach ($rows as $row) { foreach ($row as $column_name => $column_data) { if (is_null($column_data)) { $row[$column_name] = 'NULL'; } else { if ($column_data == '') { $row[$column_name] = "''"; } else { if (strpos($col_types[$column_name], 'text') !== false || strpos($col_types[$column_name], 'char') !== false || strpos($col_types[$column_name], 'blob') !== false) { $row[$column_name] = sanitize_data_generic(str_replace("'", "''", $column_data)); } } } } $this->flush($sql_insert . implode(', ', $row) . ");\n"); } }
function asurvey() { global $prefix, $sqldbdb, $MySQL, $set, $langmessage; if (file_exists("addons/survey/lang/lang_" . $set['language'] . ".php")) { require_once "addons/survey/lang/lang_" . $set['language'] . ".php"; } else { require_once "addons/survey/lang/lang_en_US.php"; } // Check if table exists in the database if ($MySQL == 0) { if (!($aa = sqlite_fetch_column_types($prefix . "surveynames", $sqldbdb))) { dbquery("CREATE TABLE " . $prefix . "surveynames ( id INTEGER NOT NULL PRIMARY KEY, surveyid INTEGER NOT NULL, surveyname VARCHAR(80), place INTEGER NOT NULL, adminlevel INTEGER NOT NULL)"); } if (!($aa = sqlite_fetch_column_types($prefix . "surveyvotes", $sqldbdb))) { dbquery("CREATE TABLE " . $prefix . "surveyvotes ( id INTEGER NOT NULL PRIMARY KEY, surveyid INTEGER NOT NULL, vote INTEGER NOT NULL, voterid INTEGER NOT NULL)"); } } else { dbquery("CREATE TABLE IF NOT EXISTS " . $prefix . "surveynames ( id INTEGER NOT NULL auto_increment, surveyid INTEGER NOT NULL, surveyname VARCHAR(80), place INTEGER NOT NULL, adminlevel INTEGER NOT NULL, PRIMARY KEY (id))"); dbquery("CREATE TABLE IF NOT EXISTS " . $prefix . "surveyvotes ( id INTEGER NOT NULL auto_increment, surveyid INTEGER NOT NULL, vote INTEGER NOT NULL, voterid INTEGER NOT NULL, PRIMARY KEY (id))"); } if (isset($_POST['surveysubmit'])) { if ($_POST['surveysubmit'] == "New Survey" && $_POST['surveyname'] != "") { if (!is_intval($_POST['adminlevel'])) { die($langmessage[98]); } dbquery("INSERT INTO " . $prefix . "surveynames ( id, surveyid, surveyname, place, adminlevel) VALUES ( null, 0, \"" . encode(sanitize($_POST['surveyname'])) . "\", 0, " . $_POST['adminlevel'] . ")"); } if ($_POST['surveysubmit'] == "Delete Survey" && $_POST['surveyid'] != "") { if (!is_intval($_POST['surveyid'])) { die($langmessage[98]); } dbquery("DELETE FROM " . $prefix . "surveynames WHERE (id=" . $_POST['surveyid'] . " AND surveyid=0) OR surveyid=" . $_POST['surveyid']); dbquery("DELETE FROM " . $prefix . "surveyvotes WHERE surveyid=" . $_POST['surveyid']); } if ($_POST['surveysubmit'] == "Add Option" && $_POST['option'] != "") { if (!is_intval($_POST['surveyid']) || !is_intval($_POST['place'])) { die($langmessage[98]); } dbquery("INSERT INTO " . $prefix . "surveynames ( id, surveyid, surveyname, place, adminlevel) VALUES ( null, " . $_POST['surveyid'] . ", \"" . encode(sanitize($_POST['option'])) . "\", " . $_POST['place'] . ", 0)"); } } $out .= "<h2>{$surveymessage['15']}</h2>\n<hr />\n"; $out .= "<h3>{$surveymessage['1']}</h3>\n"; $out .= "<form name=\"form1\" method=\"POST\" action=\"\">\n"; $out .= "<table>\n<tr><td>{$surveymessage['2']}: </td><td><input type=\"text\" name=\"surveyname\" value=\"\" size=\"50\" /></td></tr>\n"; $out .= "<tr><td>{$surveymessage['3']}: </td><td><SELECT name=\"adminlevel\">\n"; $out .= "<option value=\"0\">{$langmessage['161']}</option>\n"; $out .= "<option value=\"2\">{$langmessage['162']}</option>\n"; $out .= "<option value=\"3\">{$langmessage['29']}</option>\n"; $out .= "<option value=\"4\">{$langmessage['163']}</option>\n"; $out .= "</SELECT></td></tr>\n"; $out .= "<tr><td><input type=\"hidden\" name=\"surveysubmit\" value=\"New Survey\" /></td>"; $out .= "<td><input type=\"submit\" value=\"{$surveymessage['1']}\" name=\"aaa\" /></td></tr>\n"; $out .= "</table>\n</form>\n"; $out .= "<hr /><h3>{$surveymessage['5']}</h3>\n"; $out .= "<form name=\"form2\" method=\"POST\" action=\"\">\n"; $out .= "<table>\n"; $out .= "<tr><td>{$surveymessage['5']}: </td><td><SELECT name=\"surveyid\">\n"; $output = dbquery("SELECT * FROM " . $prefix . "surveynames WHERE surveyid=0"); $row = fetch_all($output); $i = 0; while ($row[$i]['surveyname']) { $out .= "<option value=\"" . $row[$i]['id'] . "\">" . decode($row[$i]['surveyname']) . "</option>\n"; $i++; } $out .= "</SELECT></td></tr>\n"; $out .= "<tr><td><input type=\"hidden\" name=\"surveysubmit\" value=\"Delete Survey\" /></td>"; $out .= "<td><input type=\"submit\" value=\"{$surveymessage['5']}\" name=\"aaa\" /></td></tr>\n"; $out .= "</table>\n</form>\n"; $out .= "<hr /><h3>{$surveymessage['6']}</h3>\n"; $out .= "<form name=\"form1\" method=\"POST\" action=\"\">\n"; $out .= "<table>\n"; $out .= "<tr><td>{$surveymessage['2']}: </td><td><SELECT name=\"surveyid\">\n"; $row = fetch_all(dbquery("SELECT * FROM " . $prefix . "surveynames WHERE surveyid=0")); $i = 0; while ($row[$i]['surveyname']) { $out .= "<option value=\"" . $row[$i]['id'] . "\">" . decode($row[$i]['surveyname']) . "</option>\n"; $i++; } $out .= "</SELECT></td></tr>\n"; $out .= "<tr><td>{$surveymessage['7']}: </td><td><input type=\"text\" name=\"place\" size=\"2\" value=\"\" /></td></tr>\n"; $out .= "<tr><td>{$surveymessage['8']}: </td><td><input type=\"text\" name=\"option\" size=\"50\" value=\"\" /></td></tr>\n"; $out .= "<tr><td><input type=\"hidden\" name=\"surveysubmit\" value=\"Add Option\" /></td>"; $out .= "<td><input type=\"submit\" name=\"aaa\" value=\"{$surveymessage['9']}\" /></td></tr>\n"; $out .= "</table>\n</form>\n"; $out .= "<hr /><h3>{$surveymessage['4']}</h3>\n<ul>"; $i = 0; while ($row[$i]['id']) { $out .= "<li>" . $row[$i]['id'] . " - " . decode($row[$i]['surveyname']) . "</li>\n"; $row1 = fetch_all(dbquery("SELECT * FROM " . $prefix . "surveynames WHERE surveyid=" . $row[$i]['id'])); $j = 0; $out .= "<ul>"; while ($row1[$j]['id']) { $out .= "<li>" . $row1[$j]['surveyname'] . "</li>\n"; $j++; } $out .= "</ul>\n"; $i++; } $out .= "</ul>\n"; return $out; }
public function table_exists($table_name) { return (bool) @sqlite_fetch_column_types($table_name, $this->lnk); }
function main($id, $mode) { global $db, $user, $auth, $template, $table_prefix; global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx; $user->add_lang('acp/database'); $this->tpl_name = 'acp_database'; $this->page_title = 'ACP_DATABASE'; $action = request_var('action', ''); $submit = isset($_POST['submit']) ? true : false; $template->assign_vars(array('MODE' => $mode)); switch ($mode) { case 'backup': switch ($action) { case 'download': $type = request_var('type', ''); $table = request_var('table', array('')); $format = request_var('method', ''); $where = request_var('where', ''); $store = $download = $structure = $schema_data = false; if ($where == 'store_and_download' || $where == 'store') { $store = true; } if ($where == 'store_and_download' || $where == 'download') { $download = true; } if ($type == 'full' || $type == 'structure') { $structure = true; } if ($type == 'full' || $type == 'data') { $schema_data = true; } @set_time_limit(1200); $filename = time(); // We set up the info needed for our on-the-fly creation :D switch ($format) { case 'text': $ext = '.sql'; $open = 'fopen'; $write = 'fwrite'; $close = 'fclose'; $oper = ''; $mimetype = 'text/x-sql'; break; case 'bzip2': $ext = '.sql.bz2'; $open = 'bzopen'; $write = 'bzwrite'; $close = 'bzclose'; $oper = 'bzcompress'; $mimetype = 'application/x-bzip2'; break; case 'gzip': $ext = '.sql.gz'; $open = 'gzopen'; $write = 'gzwrite'; $close = 'gzclose'; $oper = 'gzencode'; $mimetype = 'application/x-gzip'; break; } // We write the file to "store" first (and then compress the file) to not use too much // memory. The server process can be easily killed by storing too much data at once. if ($store == true) { $file = $phpbb_root_path . 'store/' . $filename . $ext; $fp = $open($file, 'w'); if (!$fp) { trigger_error('Unable to write temporary file to storage folder'); } } if ($download == true) { $name = $filename . $ext; header('Pragma: no-cache'); header("Content-Type: {$mimetype}; name=\"{$name}\""); header("Content-disposition: attachment; filename={$name}"); } // All of the generated queries go here $sql_data = ''; $sql_data .= "#\n"; $sql_data .= "# phpBB Backup Script\n"; $sql_data .= "# Dump of tables for {$table_prefix}\n"; $sql_data .= "# DATE : " . gmdate("d-m-Y H:i:s", $filename) . " GMT\n"; $sql_data .= "#\n"; switch (SQL_LAYER) { case 'sqlite': $sql_data .= "BEGIN TRANSACTION;\n"; break; case 'postgres': $sql_data .= "BEGIN;\n"; break; case 'mssql': case 'mssql_odbc': $sql_data .= "BEGIN TRANSACTION\nGO\n"; break; } foreach ($table as $table_name) { // Get the table structure if ($structure) { switch (SQL_LAYER) { case 'mysqli': case 'mysql4': case 'mysql': case 'sqlite': $sql_data .= '# Table: ' . $table_name . "\n"; $sql_data .= "DROP TABLE IF EXISTS {$table_name};\n"; break; case 'oracle': $sql_data .= '# Table: ' . $table_name . "\n"; $sql_data .= "DROP TABLE {$table_name};\n\\\n"; break; case 'postgres': case 'firebird': $sql_data .= '# Table: ' . $table_name . "\n"; $sql_data .= "DROP TABLE {$table_name};\n"; break; case 'mssql': case 'mssql_odbc': $sql_data .= '# Table: ' . $table_name . "\n"; $sql_data .= "IF OBJECT_ID(N'{$table_name}', N'U') IS NOT NULL\n"; $sql_data .= "DROP TABLE {$table_name};\nGO\n"; break; } $sql_data .= $this->get_table_structure($table_name); } // Now write the data for the first time. :) if ($store == true) { $write($fp, $sql_data); } if ($download == true) { if (!empty($oper)) { echo $oper($sql_data); } else { echo $sql_data; } } $sql_data = ''; // Data if ($schema_data) { $sql_data .= "\n"; switch (SQL_LAYER) { case 'mysqli': $sql = "SELECT * FROM {$table_name}"; $result = mysqli_query($db->db_connect_id, $sql, MYSQLI_USE_RESULT); if ($result != false) { $fields_cnt = mysqli_num_fields($result); // Get field information $field = mysqli_fetch_fields($result); $field_set = array(); for ($j = 0; $j < $fields_cnt; $j++) { $field_set[$j] = $field[$j]->name; } $search = array('\\', "'", "", "\n", "\r", ""); $replace = array('\\\\\\\\', "''", '\\0', '\\n', '\\r', '\\Z'); $fields = implode(', ', $field_set); $values = array(); $schema_insert = 'INSERT INTO ' . $table_name . ' (' . $fields . ') VALUES ('; while ($row = mysqli_fetch_row($result)) { for ($j = 0; $j < $fields_cnt; $j++) { if (!isset($row[$j]) || is_null($row[$j])) { $values[$j] = 'NULL'; } else { if ($field[$j]->flags & 32768 && !($field[$j]->flags & 1024)) { $values[$j] = $row[$j]; } else { $values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'"; } } } $sql_data .= $schema_insert . implode(', ', $values) . ");\n"; if ($store == true) { $write($fp, $sql_data); } if ($download == true) { if (!empty($oper)) { echo $oper($sql_data); } else { echo $sql_data; } } $sql_data = ''; $values = array(); } mysqli_free_result($result); } break; case 'mysql4': case 'mysql': $sql = "SELECT * FROM {$table_name}"; $result = mysql_unbuffered_query($sql, $db->db_connect_id); if ($result != false) { $fields_cnt = mysql_num_fields($result); // Get field information $field = array(); for ($i = 0; $i < $fields_cnt; $i++) { $field[$i] = mysql_fetch_field($result, $i); } $field_set = array(); for ($j = 0; $j < $fields_cnt; $j++) { $field_set[$j] = $field[$j]->name; } $search = array('\\', "'", "", "\n", "\r", ""); $replace = array('\\\\\\\\', "''", '\\0', '\\n', '\\r', '\\Z'); $fields = implode(', ', $field_set); $schema_insert = 'INSERT INTO ' . $table_name . ' (' . $fields . ') VALUES ('; while ($row = mysql_fetch_row($result)) { $values = array(); for ($j = 0; $j < $fields_cnt; $j++) { if (!isset($row[$j]) || is_null($row[$j])) { $values[$j] = 'NULL'; } else { if ($field[$j]->numeric && $field[$j]->type !== 'timestamp') { $values[$j] = $row[$j]; } else { $values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'"; } } } $sql_data .= $schema_insert . implode(', ', $values) . ");\n"; if ($store == true) { $write($fp, $sql_data); } if ($download == true) { if (!empty($oper)) { echo $oper($sql_data); } else { echo $sql_data; } } $sql_data = ''; } mysql_free_result($result); } break; case 'sqlite': $col_types = sqlite_fetch_column_types($table_name, $db->db_connect_id); $sql = "SELECT * FROM {$table_name}"; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { $names = $data = array(); foreach ($row as $row_name => $row_data) { $names[] = $row_name; // Figure out what this data is, escape it properly if (is_null($row_data)) { $row_data = 'NULL'; } else { if ($row_data == '') { $row_data = "''"; } else { if (strpos($col_types[$row_name], 'text') !== false || strpos($col_types[$row_name], 'char') !== false) { $row_data = "'" . $row_data . "'"; } } } $data[] = $row_data; } $sql_data .= 'INSERT INTO ' . $table_name . ' (' . implode(', ', $names) . ') VALUES (' . implode(', ', $data) . ");\n"; if ($store == true) { $write($fp, $sql_data); } if ($download == true) { if (!empty($oper)) { echo $oper($sql_data); } else { echo $sql_data; } } $sql_data = ''; } $db->sql_freeresult($result); break; case 'postgres': $ary_type = $ary_name = array(); // Grab all of the data from current table. $sql = "SELECT *\n\t\t\t\t\t\t\t\t\t\t\tFROM {$table_name}"; $result = $db->sql_query($sql); $i_num_fields = pg_num_fields($result); $seq = ''; for ($i = 0; $i < $i_num_fields; $i++) { $ary_type[$i] = pg_field_type($result, $i); $ary_name[$i] = pg_field_name($result, $i); $sql = "SELECT pg_get_expr(d.adbin, d.adrelid) as rowdefault\n\t\t\t\t\t\t\t\t\t\t\t\tFROM pg_attrdef d, pg_class c\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE (c.relname = '{$table_name}')\n\t\t\t\t\t\t\t\t\t\t\t\t\tAND (c.oid = d.adrelid)\n\t\t\t\t\t\t\t\t\t\t\t\t\tAND d.adnum = " . strval($i + 1); $result2 = $db->sql_query($sql); if ($row = $db->sql_fetchrow($result2)) { // Determine if we must reset the sequences if (strpos($row['rowdefault'], 'nextval(\'') === 0) { $seq .= "SELECT SETVAL('{$table_name}_seq',(select case when max({$ary_name[$i]})>0 then max({$ary_name[$i]})+1 else 1 end from {$table_name}));\n"; } } } while ($row = $db->sql_fetchrow($result)) { $schema_vals = $schema_fields = array(); // Build the SQL statement to recreate the data. for ($i = 0; $i < $i_num_fields; $i++) { $str_val = $row[$ary_name[$i]]; if (preg_match('#char|text|bool#i', $ary_type[$i])) { $str_quote = "'"; $str_empty = ''; $str_val = addslashes($str_val); } else { if (preg_match('#date|timestamp#i', $ary_type[$i])) { if (empty($str_val)) { $str_quote = ''; } else { $str_quote = "'"; } } else { $str_quote = ''; $str_empty = 'NULL'; } } if (empty($str_val) && $str_val !== '0') { $str_val = $str_empty; } $schema_vals[$i] = $str_quote . $str_val . $str_quote; $schema_fields[$i] = $ary_name[$i]; } // Take the ordered fields and their associated data and build it // into a valid sql statement to recreate that field in the data. $sql_data .= "INSERT INTO {$table_name} (" . implode(', ', $schema_fields) . ') VALUES(' . implode(', ', $schema_vals) . ");\n"; if ($store == true) { $write($fp, $sql_data); } if ($download == true) { if (!empty($oper)) { echo $oper($sql_data); } else { echo $sql_data; } } $sql_data = ''; } $db->sql_freeresult($result); // Write out the sequence statements if ($store == true) { $write($fp, $seq); } if ($download == true) { if (!empty($oper)) { echo $oper($seq); } else { echo $seq; } } $seq = ''; break; case 'mssql_odbc': $ary_type = $ary_name = array(); $ident_set = false; // Grab all of the data from current table. $sql = "SELECT *\n\t\t\t\t\t\t\t\t\t\t\tFROM {$table_name}"; $result = $db->sql_query($sql); $retrieved_data = odbc_num_rows($result); if ($retrieved_data) { $sql = "SELECT 1 as has_identity\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM INFORMATION_SCHEMA.COLUMNS\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE COLUMNPROPERTY(object_id('{$table_name}'), COLUMN_NAME, 'IsIdentity') = 1"; $result2 = $db->sql_query($sql); $row2 = $db->sql_fetchrow($result2); if (!empty($row2['has_identity'])) { $sql_data .= "\nSET IDENTITY_INSERT {$table_name} ON\nGO\n"; $ident_set = true; } $db->sql_freeresult($result2); } $i_num_fields = odbc_num_fields($result); for ($i = 0; $i < $i_num_fields; $i++) { $ary_type[$i] = odbc_field_type($result, $i); $ary_name[$i] = odbc_field_name($result, $i); } while ($row = $db->sql_fetchrow($result)) { $schema_vals = $schema_fields = array(); // Build the SQL statement to recreate the data. for ($i = 0; $i < $i_num_fields; $i++) { $str_val = $row[$ary_name[$i]]; if (preg_match('#char|text|bool#i', $ary_type[$i])) { $str_quote = "'"; $str_empty = ''; $str_val = addslashes($str_val); } else { if (preg_match('#date|timestamp#i', $ary_type[$i])) { if (empty($str_val)) { $str_quote = ''; } else { $str_quote = "'"; } } else { $str_quote = ''; $str_empty = 'NULL'; } } if (empty($str_val) && $str_val !== '0' && !(is_int($str_val) || is_float($str_val))) { $str_val = $str_empty; } $schema_vals[$i] = $str_quote . $str_val . $str_quote; $schema_fields[$i] = $ary_name[$i]; } // Take the ordered fields and their associated data and build it // into a valid sql statement to recreate that field in the data. $sql_data .= "INSERT INTO {$table_name} (" . implode(', ', $schema_fields) . ') VALUES(' . implode(', ', $schema_vals) . ");\n"; if ($store == true) { $write($fp, $sql_data); } if ($download == true) { if (!empty($oper)) { echo $oper($sql_data); } else { echo $sql_data; } } $sql_data = ''; } $db->sql_freeresult($result); if ($retrieved_data) { $sql_data = "\nGO\n"; if ($ident_set) { $sql_data .= "\nSET IDENTITY_INSERT {$table_name} OFF\nGO\n"; } } break; case 'mssql': $ary_type = $ary_name = array(); $ident_set = false; // Grab all of the data from current table. $sql = "SELECT *\n\t\t\t\t\t\t\t\t\t\t\tFROM {$table_name}"; $result = $db->sql_query($sql); $retrieved_data = mssql_num_rows($result); $i_num_fields = mssql_num_fields($result); for ($i = 0; $i < $i_num_fields; $i++) { $ary_type[$i] = mssql_field_type($result, $i); $ary_name[$i] = mssql_field_name($result, $i); } if ($retrieved_data) { $sql = "SELECT 1 as has_identity\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM INFORMATION_SCHEMA.COLUMNS\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE COLUMNPROPERTY(object_id('{$table_name}'), COLUMN_NAME, 'IsIdentity') = 1"; $result2 = $db->sql_query($sql); $row2 = $db->sql_fetchrow($result2); if (!empty($row2['has_identity'])) { $sql_data .= "\nSET IDENTITY_INSERT {$table_name} ON\nGO\n"; $ident_set = true; } $db->sql_freeresult($result2); } while ($row = $db->sql_fetchrow($result)) { $schema_vals = $schema_fields = array(); // Build the SQL statement to recreate the data. for ($i = 0; $i < $i_num_fields; $i++) { $str_val = $row[$ary_name[$i]]; if (preg_match('#char|text|bool#i', $ary_type[$i])) { $str_quote = "'"; $str_empty = ''; $str_val = addslashes($str_val); } else { if (preg_match('#date|timestamp#i', $ary_type[$i])) { if (empty($str_val)) { $str_quote = ''; } else { $str_quote = "'"; } } else { $str_quote = ''; $str_empty = 'NULL'; } } if (empty($str_val) && $str_val !== '0' && !(is_int($str_val) || is_float($str_val))) { $str_val = $str_empty; } $schema_vals[$i] = $str_quote . $str_val . $str_quote; $schema_fields[$i] = $ary_name[$i]; } // Take the ordered fields and their associated data and build it // into a valid sql statement to recreate that field in the data. $sql_data .= "INSERT INTO {$table_name} (" . implode(', ', $schema_fields) . ') VALUES(' . implode(', ', $schema_vals) . ");\n"; if ($store == true) { $write($fp, $sql_data); } if ($download == true) { if (!empty($oper)) { echo $oper($sql_data); } else { echo $sql_data; } } $sql_data = ''; } $db->sql_freeresult($result); if ($retrieved_data) { $sql_data = "\nGO\n"; if ($ident_set) { $sql_data .= "\nSET IDENTITY_INSERT {$table_name} OFF\nGO\n"; } } break; case 'firebird': $ary_type = $ary_name = array(); // Grab all of the data from current table. $sql = "SELECT *\n\t\t\t\t\t\t\t\t\t\t\tFROM {$table_name}"; $result = $db->sql_query($sql); $i_num_fields = ibase_num_fields($result); for ($i = 0; $i < $i_num_fields; $i++) { $info = ibase_field_info($result, $i); $ary_type[$i] = $info['type']; $ary_name[$i] = $info['name']; } while ($row = $db->sql_fetchrow($result)) { $schema_vals = $schema_fields = array(); // Build the SQL statement to recreate the data. for ($i = 0; $i < $i_num_fields; $i++) { $str_val = $row[strtolower($ary_name[$i])]; if (preg_match('#char|text|bool#i', $ary_type[$i])) { $str_quote = "'"; $str_empty = ''; $str_val = addslashes($str_val); } else { if (preg_match('#date|timestamp#i', $ary_type[$i])) { if (empty($str_val)) { $str_quote = ''; } else { $str_quote = "'"; } } else { $str_quote = ''; $str_empty = 'NULL'; } } if (empty($str_val) && $str_val !== '0') { $str_val = $str_empty; } $schema_vals[$i] = $str_quote . $str_val . $str_quote; $schema_fields[$i] = "'" . $ary_name[$i] . "'"; } // Take the ordered fields and their associated data and build it // into a valid sql statement to recreate that field in the data. $sql_data .= "INSERT INTO {$table_name} (" . implode(', ', $schema_fields) . ') VALUES(' . implode(', ', $schema_vals) . ");\n"; if ($store == true) { $write($fp, $sql_data); } if ($download == true) { if (!empty($oper)) { echo $oper($sql_data); } else { echo $sql_data; } } $sql_data = ''; } $db->sql_freeresult($result); break; case 'oracle': $ary_type = $ary_name = array(); // Grab all of the data from current table. $sql = "SELECT *\n\t\t\t\t\t\t\t\t\t\t\tFROM {$table_name}"; $result = $db->sql_query($sql); $i_num_fields = ocinumcols($result); for ($i = 0; $i < $i_num_fields; $i++) { $ary_type[$i] = ocicolumntype($result, $i); $ary_name[$i] = ocicolumnname($result, $i); } while ($row = $db->sql_fetchrow($result)) { $schema_vals = $schema_fields = array(); // Build the SQL statement to recreate the data. for ($i = 0; $i < $i_num_fields; $i++) { $str_val = $row[$ary_name[$i]]; if (preg_match('#char|text|bool#i', $ary_type[$i])) { $str_quote = "'"; $str_empty = ''; $str_val = addslashes($str_val); } else { if (preg_match('#date|timestamp#i', $ary_type[$i])) { if (empty($str_val)) { $str_quote = ''; } else { $str_quote = "'"; } } else { $str_quote = ''; $str_empty = 'NULL'; } } if (empty($str_val) && $str_val !== '0') { $str_val = $str_empty; } $schema_vals[$i] = $str_quote . $str_val . $str_quote; $schema_fields[$i] = '"' . $ary_name[$i] . "'"; } // Take the ordered fields and their associated data and build it // into a valid sql statement to recreate that field in the data. $sql_data .= "INSERT INTO {$table_name} (" . implode(', ', $schema_fields) . ') VALUES(' . implode(', ', $schema_vals) . ");\n"; if ($store == true) { $write($fp, $sql_data); } if ($download == true) { if (!empty($oper)) { echo $oper($sql_data); } else { echo $sql_data; } } $sql_data = ''; } $db->sql_freeresult($result); break; } } } switch (SQL_LAYER) { case 'sqlite': case 'postgres': $sql_data .= "COMMIT;"; break; case 'mssql': case 'mssql_odbc': $sql_data .= "COMMIT\nGO"; break; } if ($store == true) { $write($fp, $sql_data); $close($fp); } if ($download == true) { if (!empty($oper)) { echo $oper($sql_data); } else { echo $sql_data; } exit; } unset($sql_data); add_log('admin', 'LOG_DB_BACKUP'); trigger_error($user->lang['BACKUP_SUCCESS']); break; default: $tables = array(); switch (SQL_LAYER) { case 'sqlite': $sql = "SELECT name\n\t\t\t\t\t\t\t\t\tFROM sqlite_master\n\t\t\t\t\t\t\t\t\tWHERE type='table'\n\t\t\t\t\t\t\t\t\tORDER BY name"; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { if (strpos($row['name'], $table_prefix) === 0) { $tables[] = $row['name']; } } $db->sql_freeresult($result); break; case 'mysqli': case 'mysql4': case 'mysql': $sql = "SHOW TABLES\n\t\t\t\t\t\t\t\t\tLIKE '{$table_prefix}%'"; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { $tables[] = current($row); } $db->sql_freeresult($result); break; case 'postgres': $sql = "SELECT relname\n\t\t\t\t\t\t\t\t\tFROM pg_stat_user_tables\n\t\t\t\t\t\t\t\t\tORDER BY relname;"; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { if (strpos($row['relname'], $table_prefix) === 0) { $tables[] = $row['relname']; } } $db->sql_freeresult($result); break; case 'mssql': case 'mssql_odbc': $sql = "SELECT TABLE_NAME\n\t\t\t\t\t\t\t\t\tFROM INFORMATION_SCHEMA.TABLES\n\t\t\t\t\t\t\t\t\tWHERE TABLE_TYPE = 'BASE TABLE'\n\t\t\t\t\t\t\t\t\tORDER BY TABLE_NAME"; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { if (strpos($row['TABLE_NAME'], $table_prefix) === 0) { $tables[] = $row['TABLE_NAME']; } } $db->sql_freeresult($result); break; case 'firebird': $sql = 'SELECT RDB$RELATION_NAME as TABLE_NAME FROM RDB$RELATIONS WHERE RDB$SYSTEM_FLAG=0 AND RDB$VIEW_BLR IS NULL'; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { if (stripos($row['table_name'], $table_prefix) === 0) { $tables[] = $row['table_name']; } } $db->sql_freeresult($result); break; case 'oracle': $sql = 'SELECT TNAME as table_name FROM TAB'; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { if (stripos($row['table_name'], $table_prefix) === 0) { $tables[] = $row['table_name']; } } $db->sql_freeresult($result); break; } foreach ($tables as $table) { $template->assign_block_vars('tables', array('TABLE' => $table)); } $template->assign_vars(array('U_ACTION' => $this->u_action . '&action=download')); $available_methods = array('gzip' => 'zlib', 'bzip2' => 'bz2'); foreach ($available_methods as $type => $module) { if (!@extension_loaded($module)) { continue; } $template->assign_block_vars('methods', array('TYPE' => $type)); } $template->assign_block_vars('methods', array('TYPE' => 'text')); break; } break; case 'restore': switch ($action) { case 'submit': $delete = request_var('delete', ''); $file = request_var('file', ''); preg_match('#^(\\d{10})\\.(sql(?:\\.(?:gz|bz2))?)$#', $file, $matches); $file_name = $phpbb_root_path . 'store/' . $matches[0]; if (!(file_exists($file_name) && is_readable($file_name))) { trigger_error($user->lang['BACKUP_INVALID']); } if ($delete) { unlink($file_name); trigger_error($user->lang['BACKUP_DELETE']); } $data = file_get_contents($file_name); switch ($matches[2]) { case 'sql.bz2': $data = bzdecompress($data); break; case 'sql.gz': $data = gzinflate(substr($data, 10)); break; } $download = request_var('download', ''); if ($download) { $name = $matches[0]; switch ($matches[2]) { case 'sql': $mimetype = 'text/x-sql'; break; case 'sql.bz2': $mimetype = 'application/x-bzip2'; break; case 'sql.gz': $mimetype = 'application/x-gzip'; break; } header('Pragma: no-cache'); header("Content-Type: {$mimetype}; name=\"{$name}\""); header("Content-disposition: attachment; filename={$name}"); echo $data; die; } if (!empty($data)) { // Strip out sql comments... remove_remarks($data); switch (SQL_LAYER) { case 'firebird': $delim = ';;'; break; case 'mysql': case 'mysql4': case 'mysqli': case 'sqlite': case 'postgres': $delim = ';'; break; case 'oracle': $delim = '/'; break; case 'mssql': case 'mssql-odbc': $delim = 'GO'; break; } $pieces = split_sql_file($data, $delim); $sql_count = count($pieces); for ($i = 0; $i < $sql_count; $i++) { $sql = trim($pieces[$i]); if (!empty($sql) && $sql[0] != '#') { $db->sql_query($sql); } } } add_log('admin', 'LOG_DB_RESTORE'); trigger_error($user->lang['RESTORE_SUCCESS']); break; default: $selected = false; $methods = array('sql'); $available_methods = array('sql.gz' => 'zlib', 'sql.bz2' => 'bz2'); foreach ($available_methods as $type => $module) { if (!@extension_loaded($module)) { continue; } $methods[] = $type; } $dir = $phpbb_root_path . 'store/'; $dh = opendir($dir); while (($file = readdir($dh)) !== false) { if (preg_match('#^(\\d{10})\\.(sql(?:\\.(?:gz|bz2))?)$#', $file, $matches)) { $supported = in_array($matches[2], $methods); if ($supported == 'true') { $template->assign_block_vars('files', array('FILE' => $file, 'NAME' => gmdate("d-m-Y H:i:s", $matches[1]), 'SUPPORTED' => $supported)); $selected = true; } } } closedir($dh); if ($selected === true) { $template->assign_var('EXISTS', true); } $template->assign_vars(array('U_ACTION' => $this->u_action . '&action=submit')); break; } break; } }
/** * Database_SQLite::columns * * Returns an associative array of columns and their types (name => type) * for a particular database table. */ function columns($table) { if (!empty($this->pdo)) { return $this->pdo->columns($table); } else { return sqlite_fetch_column_types($table, $this->conn); } }
<?php $db = sqlite_open('mysqlitedb'); sqlite_query($db, 'CREATE TABLE foo (bar varchar(10), arf text)'); $cols = sqlite_fetch_column_types('foo', $db);
| the included gpl.txt file or visit http://gnu.org +----------------------------------------------------*/ global $gallerymessage, $myserver, $prefix, $sqldbdb, $MySQL, $set; if ($_SERVER['SERVER_NAME'] != $myserver) { die('Access Denied!'); } if (file_exists("addons/gallery/lang/lang_" . $set['language'] . ".php")) { require_once "addons/gallery/lang/lang_" . $set['language'] . ".php"; } else { require_once "addons/gallery/lang/lang_en_US.php"; } require_once "addons/gallery/common.php"; // Check if table exists in the database $query = ""; if ($MySQL == 0) { if (!($aa = sqlite_fetch_column_types($prefix . "images", $sqldbdb))) { $query = "CREATE TABLE " . $prefix . "images ( id INTEGER NOT NULL PRIMARY KEY, file VARCHAR(20) NOT NULL, name VARCHAR(50) NOT NULL)"; } } else { $query = "CREATE TABLE IF NOT EXISTS " . $prefix . "images ( id INTEGER NOT NULL auto_increment, file VARCHAR(20) NOT NULL, name VARCHAR(50) NOT NULL, PRIMARY KEY (id))"; } if ($query != "") { @dbquery($query); } if ($_POST['submit'] == "Delete Gallery") { $folder = "galeries/" . $_POST['name']; $filez = filelist('/./', $folder); foreach ($filez as $fil) { $out .= deleteimage($folder . "/" . $fil, $fil); } if (@rmdir($folder)) {
function list_fields($table) { $rs = sqlite_fetch_column_types($table, $this->conn, $this->rs_type); if (!$rs) { return false; } foreach ($rs as $key => $value) { $rslist[] = $key; } return $rslist; }
<?php $db = sqlite_open('mysqlitedb'); sqlite_query($db, 'CREATE TABLE foo (bar varchar(10), arf text)'); $cols = sqlite_fetch_column_types('foo', $db, SQLITE_ASSOC); foreach ($cols as $column => $type) { echo "Column: {$column} Type: {$type}\n"; }
<?php require 's_common.inc'; $db = makedb(); $r = sqlite_fetch_column_types('mytable', $db); var_dump($r); $rh = sqlite_query($db, 'SELECT * FROM mytable'); echo "result resource? " . is_resource($rh) . "\n"; if ($rh) { // num_rows echo "num rows: " . sqlite_num_rows($rh) . "\n"; // simple fetch_array loop echo "fetching results\n"; while ($result = sqlite_fetch_array($rh)) { var_dump($result); } // simple fetch_object loop // php5 dumps objects differently /* echo "fetching results object\n"; sqlite_rewind($rh); while ($result = sqlite_fetch_object($rh)) { var_dump($result); }*/ // rewind/next loop sqlite_rewind($rh); while (sqlite_next($rh)) { $result = sqlite_current($rh); var_dump($result); $b = sqlite_has_more($rh); echo "has_more?\n"; $b = sqlite_valid($rh);