Esempio n. 1
0
function mysql_query_cache($sql, $linkIdentifier = false, $timeout = 4)
{
    //首先调用上面的getCache函数,如果返回值不为false的话,就说明是从memcached服务器获取的数据
    //如果返回false,此时就需要直接从数据库中获取数据了。
    //需要注意的是这里使用操作的命令加上sql语句的md5码作为一个特定的key,可能大家觉得使用数据项的
    //名称作为key会比较自然一点。运行memcached加上"-vv"参数,并且不作为daemon运行的话,可以看见
    //memcached处理时输出的相关信息
    if (!($cache = getCache(md5("mysql_query" . $sql)))) {
        $cache = false;
        $r = $linkIdentifier != false ? mysql_query($sql, $linkIdentifier) : mysql_query($sql);
        //读取数据库,并将结果放入$cache数组中
        if (is_resource($r) && ($rows = mysql_num_rows($r)) != 0) {
            for ($i = 0; $i < $rows; $i++) {
                $fields = mysql_num_fields($r);
                $row = mysql_fetch_array($r);
                for ($j = 0; $j < $fields; $j++) {
                    if ($i == 0) {
                        $columns[$j] = mysql_field_name($r, $j);
                    }
                    $cache[$i][$columns[$j]] = $row[$j];
                }
            }
            //将数据放入memcached服务器中,如果memcached服务器没有开的话,此语句什么也不会做
            //如果开启了服务器的话,数据将会被缓存到memcached服务器中
            if (!setCache(md5("mysql_query" . $sql), $cache, $timeout)) {
                # If we get here, there isn’t a memcache daemon running or responding
            }
        }
    }
    return $cache;
}
Esempio n. 2
0
 function get_table_fields($table)
 {
     $result = $this->select_from_table($table);
     $i = 0;
     $fields = array();
     while ($i < mysql_num_fields($result)) {
         $fields[$i] = mysql_fetch_field($result, $i);
         /*
             PROPERTIES
             $field->blob
             $field->max_length
             $field->multiple_key
             $field->name
             $field->not_null
             $field->numeric
             $field->primary_key
             $field->table
             $field->type
             $field->def
             $field->unique_key
             $field->unsigned
             $field->zerofill
         */
         $i++;
     }
     return $fields;
 }
Esempio n. 3
0
 /**
     Show the result data from a SQL SELECT query
 
     @param $result The result set from the last SELECT query. (optional)
 */
 function showQuery($result = 0)
 {
     if (!$result) {
         $result = $this->result;
     }
     if (!$result) {
         $this->_handleError("No result set to show!");
         return;
     }
     // Convert column names to table headings
     $html = "<table border>\n";
     $html .= "<tr>\n";
     $count = mysql_num_fields($result);
     for ($i = 0; $i < $count; $i++) {
         $html .= "<th>" . mysql_field_name($result, $i) . "</th>\n";
     }
     $html .= "</tr>\n";
     // Convert data to table cells
     @mysql_data_seek($result, 0);
     $count = 0;
     while ($row = mysql_fetch_row($result)) {
         $html .= "<tr>\n";
         foreach ($row as $item) {
             $html .= "<td>&nbsp;{$item}</td>\n";
         }
         $html .= "</tr>\n";
         $count++;
     }
     echo $html;
 }
function update_emp_values($emp_type, $emp_id, $fname, $lname, $contact, $address, $salary, $sex, $bdate, $join_date)
{
    $dbc = mysql_connect('localhost', 'root', 'rishi');
    if (!$dbc) {
        die('NOT CONNECTED:' . mysql_error());
    }
    $db_selected = mysql_select_db("restaurant", $dbc);
    if (!$db_selected) {
        die('NOT CONNECTED TO DATABASE:' . mysql_error());
    }
    $emp_type_id = $emp_type . "_id";
    $query = "Select * from {$emp_type}";
    $emp = mysql_query($query);
    $num_fields = mysql_num_fields($emp);
    if ($emp_type == "COOK") {
        $val_arrays = array("{$emp_id}", $fname, $lname, $contact, $address, $salary, $sex, $bdate, $join_date, $_POST["Specialization"]);
    } else {
        $val_arrays = array("{$emp_id}", $fname, $lname, $contact, $address, $salary, $sex, $bdate, $join_date);
    }
    $values = "";
    for ($i = 1; $i < $num_fields; $i++) {
        $values = $values . mysql_field_name($emp, $i) . " = " . "\"{$val_arrays[$i]}\"";
        if ($i != $num_fields - 1) {
            $values = $values . " , ";
        }
    }
    $query = "UPDATE {$emp_type} SET " . $values . "  WHERE {$emp_type_id} = {$emp_id};";
    mysql_query($query);
}
function update_restaurant()
{
    $dbc = mysql_connect('localhost', 'root', 'rishi');
    if (!$dbc) {
        die('NOT CONNECTED:' . mysql_error());
    }
    $db_selected = mysql_select_db("restaurant", $dbc);
    if (!$db_selected) {
        die('NOT CONNECTED TO DATABASE:' . mysql_error());
    }
    echo "<form name = \"form1\" action = \"update_restaurant_values.php\" method =\"post\" align=\"center\" onsubmit=\"return checkscript()\">" . "\n";
    echo "<table style=\"text-align:center;\" align=\"center\" width=\"400\">" . "\n";
    $query = "SELECT * from RESTAURANT;";
    $rest = mysql_query($query);
    $num_fields = mysql_num_fields($rest);
    for ($i = 0; $i < $num_fields; $i++) {
        echo "<tr>" . "\n";
        echo "<td>" . "\n";
        $field = mysql_field_name($rest, $i);
        echo "<b>" . $field . "</b>" . "\n";
        echo "</td>" . "\n";
        echo "<td>" . "\n";
        $res = mysql_result($rest, 0, $i);
        if ($i) {
            echo "<input type = \"text\" name = \"{$field}\" value=\"{$res}\">";
        } else {
            echo "<input type = \"text\" name = \"{$field}\" value=\"{$res}\" readonly=\"readonly\">";
        }
        echo "</td>" . "\n";
        echo "</tr>" . "\n";
    }
    echo "</table>" . "\n" . "<br/>";
    echo "<input type=\"submit\" name=\"submitbutton\" value=\"Update\">" . "\n";
    echo "</form>" . "\n";
}
Esempio n. 6
0
function formatJSON($result)
{
    $str = "[";
    $numRows = 0;
    while ($row = mysql_fetch_array($result)) {
        if ($numRows > 0) {
            $str = $str . ", ";
        }
        $numRows++;
        $n = mysql_num_fields($result);
        for ($i = 0; $i < $n; $i++) {
            $fld = mysql_field_name($result, $i);
            $val = addslashes($row[$fld]);
            $val = str_replace("\t", "", $val);
            $val = str_replace("'", "\\'", $val);
            $val = str_replace("\r\n", "", $val);
            if ($i == 0) {
                $str = $str . "{\"{$fld}\":\"{$val}\"";
            } else {
                $str = $str . ", \"{$fld}\":\"{$val}\"";
            }
        }
        $str = $str . "}\r\n";
    }
    $str = $str . "]";
    return $str;
}
Esempio n. 7
0
function tabledump($result)
{
    if ($result == 0) {
        echo "<b>Error " . mysql_errno() . ": " . mysql_error() . "</b>";
    } else {
        if (@mysql_num_rows($result) == 0) {
            echo "<b>Query completed.  Empty result.</b><br>";
        } else {
            $nf = mysql_num_fields($result);
            $nr = mysql_num_rows($result);
            echo "<table border='1'> <thead>";
            echo "<tr>";
            for ($i = 0; $i < $nf; $i++) {
                echo "<th>" . mysql_field_name($result, $i) . "</th>";
            }
            echo "</tr>";
            echo "</thead><tbody>";
            for ($i = 0; $i < $nr; $i++) {
                echo "<tr>";
                $row = mysql_fetch_array($result);
                for ($j = 0; $j < $nf; $j++) {
                    echo "<td>" . $row[$j] . "</td>";
                }
                echo "</tr>";
            }
            echo "</tbody></table>";
        }
    }
    return $row;
}
Esempio n. 8
0
function display_db_query($query_string, $connection, $header_bool, $table_params)
{
    // perform the database query
    $result_id = mysql_query($query_string, $connection) or die("display_db_query:" . mysql_error());
    // find out the number of columns in result
    $column_count = mysql_num_fields($result_id) or die("display_db_query:" . mysql_error());
    // Here the table attributes from the $table_params variable are added
    print "<TABLE {$table_params} >\n";
    // optionally print a bold header at top of table
    if ($header_bool) {
        print "<TR>";
        for ($column_num = 0; $column_num < $column_count; $column_num++) {
            $field_name = mysql_field_name($result_id, $column_num);
            print "<TH>{$field_name}</TH>";
        }
        print "</TR>\n";
    }
    // print the body of the table
    while ($row = mysql_fetch_row($result_id)) {
        print "<TR ALIGN=LEFT VALIGN=TOP>";
        for ($column_num = 0; $column_num < $column_count; $column_num++) {
            print "<TD>{$row[$column_num]}</TD>\n";
        }
        print "</TR>\n";
    }
    print "</TABLE>\n";
}
/**
 * Database query
 * 		@param $sql
 * 		@param $return_type
 * 		@param $first_row_only
 * 		@param $fetch_func
 * 		@param $debug
 */
function database_query($sql, $return_type = DATA_ONLY, $first_row_only = ALL_ROWS, $fetch_func = FETCH_ASSOC, $debug = false)
{
    $data_array = array();
    $num_rows = 0;
    $fields_len = 0;
    $result = mysql_query($sql);
    if ($debug == true) {
        echo $sql . '-' . mysql_error();
    }
    if ($result) {
        if ($return_type == 0 || $return_type == 2) {
            while ($row_array = $fetch_func($result)) {
                if (!$first_row_only) {
                    array_push($data_array, $row_array);
                } else {
                    $data_array = $row_array;
                    break;
                }
            }
        }
        $num_rows = mysql_num_rows($result);
        $fields_len = mysql_num_fields($result);
        mysql_free_result($result);
    }
    switch ($return_type) {
        case DATA_ONLY:
            return $data_array;
        case ROWS_ONLY:
            return $num_rows;
        case DATA_AND_ROWS:
            return array($data_array, $num_rows);
        case FIELDS_ONLY:
            return $fields_len;
    }
}
Esempio n. 10
0
function DB_dump_result($result, $print = 0)
{
    if (!$result) {
        return false;
    }
    if (!($fields_nb = mysql_num_fields($result))) {
        return false;
    }
    $output = "<!-- Starting query result dump  -->\n";
    //$output.="<p>\n"; //PAS XHTML VALIDE !!
    $output .= "  <table border=\"1\" cellpadding=\"2\" cellspacing=\"0\">\n";
    $output .= "    <tr>\n";
    for ($i = 0; $i < $fields_nb; $i++) {
        $field_names[$i] = mysql_field_name($result, $i);
        $output .= "      <th>{$field_names[$i]}</th>\n";
    }
    $output .= "  </tr>\n";
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $output .= "    <tr>\n";
        foreach ($field_names as $this_field) {
            // foreach($row) does not work here without "MYSQL_ASSOC"
            $output .= $row[$this_field] ? "      <td>{$row[$this_field]}</td>\n" : "<td>&nbsp;</td>\n";
        }
        $output .= "    </tr>\n";
    }
    $output .= "  </table>\n";
    // $output.="</p>\n"; // PAS XHTML VALIDE !
    $output .= "<!-- End query result dump -->\n";
    // Finally output the result, with either method, based
    // on the coder's choice.
    if ($print) {
        echo $output;
    }
    return $output;
}
Esempio n. 11
0
/**
 * INPUT: $apikey
 * OUTPUT: XML file
 **/
function WriteHistoryFromSQL($sql)
{
    $req = mysql_query($sql) or die('SQL Error !<br>' . $sql . '<br>' . mysql_error());
    $f = mysql_num_fields($req);
    for ($i = 0; $i < $f; $i++) {
        $previous[$i] = "";
        $xmltab[$i] = "";
    }
    while ($data = mysql_fetch_array($req)) {
        for ($i = 0; $i < $f; $i++) {
            if (mysql_field_name($req, $i) != 'cid' && mysql_field_name($req, $i) != 'date' && $data[$i] != $previous[$i]) {
                $previous[$i] = $data[$i];
                $xmltab[$i] .= "<" . mysql_field_name($req, $i) . " date=\"" . $data['date'] . "\">" . $data[$i] . "</" . mysql_field_name($req, $i) . ">";
            }
        }
    }
    for ($i = 0; $i < $f; $i++) {
        $tag_name = mysql_field_name($req, $i);
        if ($tag_name != 'cid' && $tag_name != 'date') {
            if ($xmltab[$i] != "") {
                print "<" . $tag_name . "_history>" . $xmltab[$i] . "</" . $tag_name . "_history>";
            }
        }
    }
}
Esempio n. 12
0
function display_db_query($query_string, $connection)
{
    $result_id = mysql_query($query_string, $connection) or die(mysql_error());
    $column_count = mysql_num_fields($result_id) or die(mysql_error());
    echo "<table class=\"sortable\" border=\"1\" style=\"width:auto;margin-top:1em;\">\n<tr>";
    for ($column_num = 0; $column_num < $column_count - 1; $column_num++) {
        echo "<th>", mysql_field_name($result_id, $column_num), "</th>";
    }
    echo "</tr>\n";
    while ($row = mysql_fetch_row($result_id)) {
        echo "<tr>";
        for ($column_num = 0; $column_num < $column_count - 1; $column_num++) {
            echo "<td class=\"c{$column_num}\">";
            if ($column_num == 2) {
                echo "<a href=\"hebconj.php?verb_root=", urlencode($row[$column_num]), "&amp;tense_id={$row[4]}\">";
            }
            if ($column_num == 1 && empty($row[5])) {
                echo '<del>';
            }
            echo $row[$column_num];
            if ($column_num == 1 && empty($row[5])) {
                echo '</del>';
            }
            if ($column_num == 2) {
                echo "</a>";
            }
            echo "</td>";
        }
        echo "</tr>\n";
    }
    echo "</table>\n";
}
Esempio n. 13
0
function display_db_query($query_string, $connection, $names)
{
    $result_id = mysql_query($query_string, $connection) or die("display_db_query:" . mysql_error());
    $column_count = mysql_num_fields($result_id) or die("display_db_query:" . mysql_error());
    print "<thead>\n";
    print "<tr>\n";
    for ($column_number = 0; $column_number < $column_count; $column_number++) {
        $field_name = $names[$column_number];
        print "<th>{$field_name}</th>\n";
    }
    print "</tr>\n";
    print "</thead>\n";
    print "<tbody>\n";
    while ($row = mysql_fetch_row($result_id)) {
        print "<tr class=\"gradeA\">";
        for ($column_number = 0; $column_number < $column_count; $column_number++) {
            print "<td>{$row[$column_number]}</td>\n";
        }
        print "</tr>\n";
    }
    print "</tbody>\n";
    print "<tfoot>\n";
    print "<tr>\n";
    for ($column_number = 0; $column_number < $column_count; $column_number++) {
        $field_name = $names[$column_number];
        print "<th>{$field_name}</th>\n";
    }
    print "</tr>\n";
    print "</tfoot>\n";
}
 function loadFromResult($mysqlresult, $dblink)
 {
     $this->numRows = @mysql_num_rows($mysqlresult);
     $this->numFields = @mysql_num_fields($mysqlresult);
     $this->result = $mysqlresult;
     return parent::load($dblink);
 }
Esempio n. 15
0
function get_content($database, $table, $fp)
{
    //      get content of data
    global $delimiter;
    $result = mysql_db_query($database, "SELECT * FROM {$table}") or die("Cannot get content of table");
    while ($row = mysql_fetch_row($result)) {
        $insert = "INSERT INTO {$table} VALUES (";
        //      command for later SQL-restore
        for ($j = 0; $j < mysql_num_fields($result); $j++) {
            //      content for later SQL-restore
            if (!isset($row[$j])) {
                $insert .= "NULL,";
            } elseif (isset($row[$j])) {
                $insert .= "'" . addslashes($row[$j]) . "',";
            } else {
                $insert .= "'',";
            }
        }
        $insert = ereg_replace(",\$", "", $insert);
        $insert .= "){$delimiter}\n";
        //      create row delimiter
        gzwrite($fp, $insert);
        //      now write the complete content into backup file
    }
    gzwrite($fp, "\n\n");
    mysql_free_result($result);
}
Esempio n. 16
0
function make_table($result)
{
    while ($row = mysql_fetch_array($result)) {
        // get all the records based on the selection statement determined in the range_type function
        print "<tr>";
        for ($k = 0; $k < mysql_num_fields($result); $k++) {
            // Go through all the fields
            $option = "checked" . $k;
            global ${$option};
            if (${$option} == "yes") {
                // If the user opted to see that field
                $field = "field" . $k;
                //Fix: The number after arw should not be hard-coded here, but put in a for loop for future compatibility
                global $arabiccolumn, $arw0, ${$field};
                // dynamically declare the field variables
                //Fix: The number after arw should not be hard-coded here, but put in a for loop for future compatibility
                if ($arabiccolumn != "" && $arabiccolumn == ${$field}) {
                    // && $arw0 != "" (if problem with no width being specified--but then need to deal with dir="rtl" for non-Arabic) -- print the cells with right-to-left directionality and column-width; Fix: make columns variable not only for Arabic
                    //Fix: The number after arw should not be hard-coded here, but put in a for loop for future compatibility
                    print "<td width='{$arw0}' valign='top' dir='rtl'>" . $row[${$field}] . "</td>";
                } else {
                    // print the cells with regular (left-to-right) directionality and no special width
                    print "<td valign='top'>" . $row[${$field}] . "</td>";
                }
                // } else {
                // } //////// end else remove this if removing above
            }
            //end if (if the user opted to see the given field)
        }
        //end for to go through all the fields
        print "</tr>\n";
    }
    //end while to get all the records
}
Esempio n. 17
0
function RunFreeQuery()
{
    global $cnInfoCentral;
    global $aRowClass;
    global $rsQueryResults;
    global $sSQL;
    global $iQueryID;
    //Run the SQL
    $rsQueryResults = RunQuery($sSQL);
    if (mysql_error() != "") {
        echo gettext("An error occured: ") . mysql_errno() . "--" . mysql_error();
    } else {
        $sRowClass = "RowColorA";
        echo "<table align=\"center\" cellpadding=\"5\" cellspacing=\"0\">";
        echo "<tr class=\"" . $sRowClass . "\">";
        //Loop through the fields and write the header row
        for ($iCount = 0; $iCount < mysql_num_fields($rsQueryResults); $iCount++) {
            echo "<td align=\"center\"><b>" . mysql_field_name($rsQueryResults, $iCount) . "</b></td>";
        }
        echo "</tr>";
        //Loop through the recordsert
        while ($aRow = mysql_fetch_array($rsQueryResults)) {
            $sRowClass = AlternateRowStyle($sRowClass);
            echo "<tr class=\"" . $sRowClass . "\">";
            //Loop through the fields and write each one
            for ($iCount = 0; $iCount < mysql_num_fields($rsQueryResults); $iCount++) {
                echo "<td align=\"center\">" . $aRow[$iCount] . "</td>";
            }
            echo "</tr>";
        }
        echo "</table>";
        echo "<br><p class=\"ShadedBox\" style=\"border-style: solid; margin-left: 50px; margin-right: 50 px; border-width: 1px;\"><span class=\"SmallText\">" . str_replace(Chr(13), "<br>", htmlspecialchars($sSQL)) . "</span></p>";
    }
}
Esempio n. 18
0
function echo_row_prodotti_amm($query)
{
    $num_colonne = mysql_num_fields($query);
    $i = 0;
    $row = mysql_fetch_row($query);
    while ($i < $num_colonne) {
        echo "<form action=\"php/updateproduct.php\" method=\"post\"> <fieldset>";
        echo "<tr>";
        echo "<td style= \"font-style: italic;\">" . mysql_field_name($query, $i) . ":</td>";
        echo "<td>" . $row[$i] . "</td>";
        echo "<td>";
        if ($i != 0) {
            echo "<input type=\"text\" id=\"modifica\" name=\"modifica\">";
        }
        echo "</td>";
        echo "<td style= \"text-align: center;\">";
        if ($i != 0) {
            echo "<input style=\"border:0; width:25px;\" type=\"image\" src=\"images/ok.jpg\">";
        }
        echo "</td>";
        echo "</tr>";
        echo "<input type=\"hidden\" name=\"codiceID\" value=\"" . $row[0] . "\">";
        echo "<input type=\"hidden\" name=\"nomeCampo\" value=\"" . mysql_field_name($query, $i) . "\"> </fieldset> </form>";
        $i = $i + 1;
    }
}
function get_field_names($table){
	$result = mysql_query("SELECT * FROM ".$table);
	for($x = 0; $x < mysql_num_fields($result); $x++){
		$arr[$x] = mysql_field_name($result,$x);
	}
	return $arr;
}
function getAccessInfo($sql_fields, $sql_table, $sql_conditions = "1", $cond = NULL)
{
    $access['Data'] = array();
    $access['Headers'] = 0;
    $access['Sql_Fields'] = $sql_fields;
    $access['Sql_Table'] = $sql_table;
    $access['Sql_Conditions'] = $sql_conditions;
    $sql = "Select {$sql_fields} from {$sql_table} where {$sql_conditions}";
    $result = sql_query_read($sql) or dieLog(mysql_error() . " ~ " . "<pre>{$sql}</pre>");
    if (mysql_num_rows($result) < 1) {
        return -1;
    }
    $row = mysql_fetch_row($result);
    $fields = mysql_num_fields($result);
    for ($i = 0; $i < $fields; $i++) {
        $type = mysql_field_type($result, $i);
        $name = mysql_field_name($result, $i);
        $len = mysql_field_len($result, $i);
        $flags = mysql_field_flags($result, $i);
        $table = mysql_field_table($result, $i);
        $useName = $name;
        if (array_key_exists($useName, $access['Data'])) {
            $useName = $name . $i;
        }
        if ($name == 'access_header') {
            $access['Headers']++;
        }
        $access['Data'][$useName] = getAttrib($name, $type, $len, $flags, $table, $row[$i], &$cond);
    }
    return $access;
}
Esempio n. 21
0
function datadump($table)
{
    // <--- thx to mrwebmaster for function
    # Creo la variabile $result
    $result .= "# Dump of {$table} \n";
    $result .= "# Dump DATE : " . date("d-M-Y") . "\n\n";
    # Conto i campi presenti nella tabella
    $query = mysql_query("select * from {$table}");
    $num_fields = @mysql_num_fields($query);
    # Conto il numero di righe presenti nella tabella
    $numrow = mysql_num_rows($query);
    # Passo con un ciclo for tutte le righe della tabella
    for ($i = 0; $i < $numrow; $i++) {
        $row = mysql_fetch_row($query);
        # Ricreo la tipica sintassi di un comune Dump
        $result .= "INSERT INTO " . $table . " VALUES(";
        # Con un secondo ciclo for stampo i valori di tutti i campi
        # trovati in ogni riga
        for ($j = 0; $j < $num_fields; $j++) {
            $row[$j] = addslashes($row[$j]);
            $row[$j] = ereg_replace("\n", "\\n", $row[$j]);
            if (isset($row[$j])) {
                $result .= "\"{$row[$j]}\"";
            } else {
                $result .= "\"\"";
            }
            if ($j < $num_fields - 1) {
                $result .= ",";
            }
        }
        # Chiudo l'istruzione INSERT
        $result .= ");\n";
    }
    return $result . "\n\n\n";
}
Esempio n. 22
0
 function getMysqlDump()
 {
     mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
     $tables = mysql_list_tables(DB_NAME);
     while ($td = mysql_fetch_array($tables)) {
         $table = $td[0];
         if ($table == $this->_tableName) {
             continue;
         }
         $SQL[] .= "DROP TABLE IF EXISTS `{$table}`;";
         $r = mysql_query("SHOW CREATE TABLE `{$table}`");
         if ($r) {
             $insert_sql = "";
             $d = mysql_fetch_array($r);
             $d[1] .= ";";
             $SQL[] = $d[1];
             $table_query = mysql_query("SELECT * FROM `{$table}`");
             $num_fields = mysql_num_fields($table_query);
             while ($fetch_row = mysql_fetch_array($table_query)) {
                 $insert_sql .= "INSERT INTO {$table} VALUES(";
                 for ($n = 1; $n <= $num_fields; $n++) {
                     $m = $n - 1;
                     $insert_sql .= "\"" . mysql_real_escape_string($fetch_row[$m]) . "\", ";
                     $insert_sql = str_replace("'", "\\'", $insert_sql);
                 }
                 $insert_sql = substr($insert_sql, 0, -2);
                 $insert_sql .= ");\n";
             }
             if ($insert_sql != "") {
                 $SQL[] = $insert_sql;
             }
         }
     }
     return implode("\r", $SQL);
 }
Esempio n. 23
0
 function create_backup_sql($file)
 {
     $line_count = 0;
     $db_connection = $this->db_connect();
     mysql_select_db($this->db_name(), $db_connection) or die("ERROR: Unable to connect database (code:m_s_db)");
     $tables = mysql_list_tables($this->db_name());
     $sql_string = NULL;
     while ($table = mysql_fetch_array($tables)) {
         $table_name = $table[0];
         $sql_string = "DELETE FROM {$table_name}";
         $table_query = mysql_query("SELECT * FROM `{$table_name}`");
         $num_fields = mysql_num_fields($table_query);
         while ($fetch_row = mysql_fetch_array($table_query)) {
             $sql_string .= "INSERT INTO {$table_name} VALUES(";
             $first = TRUE;
             for ($field_count = 1; $field_count <= $num_fields; $field_count++) {
                 if (TRUE == $first) {
                     $sql_string .= "'" . mysql_real_escape_string($fetch_row[$field_count - 1]) . "'";
                     $first = FALSE;
                 } else {
                     $sql_string .= ", '" . mysql_real_escape_string($fetch_row[$field_count - 1]) . "'";
                 }
             }
             $sql_string .= ");";
             if ($sql_string != "") {
                 $line_count = $this->write_backup_sql($file, $sql_string, $line_count);
             }
             $sql_string = NULL;
         }
     }
     return $line_count;
 }
 function query($query, $singleResult = 0)
 {
     $this->_result = mysql_query($query, $this->_dbHandle);
     if (preg_match("/select/i", $query)) {
         $result = array();
         $table = array();
         $field = array();
         $tempResults = array();
         $numOfFields = mysql_num_fields($this->_result);
         for ($i = 0; $i < $numOfFields; ++$i) {
             array_push($table, mysql_field_table($this->_result, $i));
             array_push($field, mysql_field_name($this->_result, $i));
         }
         while ($row = mysql_fetch_row($this->_result)) {
             for ($i = 0; $i < $numOfFields; ++$i) {
                 $table[$i] = trim(ucfirst($table[$i]), "s");
                 $tempResults[$table[$i]][$field[$i]] = $row[$i];
             }
             if ($singleResult == 1) {
                 mysql_free_result($this->_result);
                 return $tempResults;
             }
             array_push($result, $tempResults);
         }
         mysql_free_result($this->_result);
         return $result;
     }
 }
 public function columnCount()
 {
     if ($this->_result) {
         return mysql_num_fields($this->_result);
     }
     return 0;
 }
Esempio n. 26
0
 function query_start($query)
 {
     // For reg expressions
     $query = trim($query);
     // Query was an insert, delete, update, replace
     if (preg_match("/^(insert|delete|update|replace)\\s+/i", $query)) {
         return false;
     }
     $this->savedqueries[] = $query;
     // Flush cached values..
     $this->flush();
     // Log how the function was called
     $this->func_call = "\$db->query_start(\"{$query}\")";
     // Keep track of the last query for debug..
     $this->last_query = $query;
     // Perform the query via std mysql_query function..
     $this->result = @mysql_query($query, $this->dbh);
     $this->num_queries++;
     // If there is an error then take note of it..
     if (mysql_error()) {
         $this->print_error();
         return false;
     }
     // Take note of column info
     $i = 0;
     while ($i < @mysql_num_fields($this->result)) {
         $this->col_info[$i] = @mysql_fetch_field($this->result);
         $i++;
     }
     $this->last_result = array();
     $this->num_rows = 0;
     // If debug ALL queries
     $this->trace || $this->debug_all ? $this->debug() : null;
     return true;
 }
Esempio n. 27
0
 function Tabler($table, $cols = array(), $filters = array(), $order = "")
 {
     $this->table($table);
     $this->pager = new Pager();
     if (count($cols) == 0) {
         $what = '*';
     } else {
         foreach ($cols as $title => $name) {
             if ($name != "") {
                 $this->addCol($name);
                 $this->col_option($name, 'title', $title);
             }
             $what .= $name . ',';
         }
         $what = substr($what, 0, strlen($what) - 1);
     }
     if (count($filters) > 0) {
         foreach ($filters as $add) {
             $filter = isset($filter) ? "{$filter} AND {$add}" : " WHERE {$add}";
         }
     }
     foreach ($_GET as $key => $value) {
         if (ereg('^filter_', $key)) {
             $name = substr($key, 7);
             if ($value != '') {
                 if ($_GET['f_' . $name . '_type'] == 'search') {
                     $filter = isset($filter) ? "{$filter} AND {$name} LIKE '%{$value}%'" : " WHERE {$name} LIKE '%{$value}%'";
                 } else {
                     $filter = isset($filter) ? "{$filter} AND {$name}='{$value}'" : " WHERE {$name}='{$value}'";
                 }
             }
         }
     }
     $query = "SELECT count(*) as max FROM {$table} {$filter}";
     $result = mysql_query($query) or die("MySQL Error: " . mysql_error());
     $row = mysql_fetch_array($result);
     $this->pager->max($row['max']);
     $query = "SELECT {$what} FROM {$table} {$filter}";
     $keys = array_keys($this->cols);
     $order = isset($_GET['orderby']) ? $_GET['orderby'] : ($order == "" ? $keys['0'] . ' DESC' : $order);
     $query .= " ORDER BY {$order}";
     $query .= " LIMIT " . $this->pager->from() . "," . $this->pager->incr();
     $result = mysql_query($query) or die("MySQL Error: " . mysql_error());
     while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
         $this->add($row);
     }
     $fields = mysql_num_fields($result);
     for ($x = 0; $x < $fields; $x++) {
         $colinfo = @mysql_field_flags($result, $x);
         $colinfo = explode(' ', $colinfo);
         $name = mysql_field_name($result, $x);
         $type = mysql_field_type($result, $x);
         if (array_search('auto_increment', $colinfo) !== false) {
             $this->col_option($name, 'filter', 'submit');
             $this->idcol = $name;
         } else {
             $this->col_option($name, 'filter', 'select');
         }
     }
 }
Esempio n. 28
0
 /**
  * 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 mysqlfAdapter($d)
 {
     $f = $d['filter'];
     $d = $d['data'];
     parent::RecordSetAdapter($d);
     $fieldcount = count($f);
     $truefieldcount = mysql_num_fields($d);
     $be = $this->isBigEndian;
     $isintcache = array();
     for ($i = 0; $i < $truefieldcount; $i++) {
         //mysql_fetch_* usually returns only strings,
         //hack it into submission
         $type = mysql_field_type($d, $i);
         $name = mysql_field_name($d, $i);
         $isintcache[$name] = in_array($type, array('int', 'real', 'year'));
     }
     $isint = array();
     for ($i = 0; $i < $fieldcount; $i++) {
         $this->columnNames[$i] = $this->_charsetHandler->transliterate($f[$i]);
         $isint[$i] = isset($isintcache[$f[$i]]) && $isintcache[$f[$i]];
     }
     //Start fast serializing
     $ob = "";
     $fc = pack('N', $fieldcount);
     if (mysql_num_rows($d) > 0) {
         mysql_data_seek($d, 0);
         while ($line = mysql_fetch_assoc($d)) {
             //Write array flag + length
             $ob .= "\n" . $fc;
             $i = 0;
             foreach ($f as $key) {
                 $value = $line[$key];
                 if (!$isint[$i]) {
                     $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;
                     }
                 } else {
                     $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;
                 }
                 $i++;
             }
         }
     }
     $this->numRows = mysql_num_rows($d);
     $this->serializedData = $ob;
 }
Esempio n. 29
0
function selectrec($fields, $table, $cond = "")
{
    $a = array();
    $query = "select " . $fields . " from " . $table;
    if (!($cond == "")) {
        $query = $query . " where " . $cond;
    }
    //echo "<br>".$query;
    $result = mysql_query($query);
    //echo mysql_error();
    //if (mysql_error()!="")
    //echo "<br>".$query;
    $numrows = mysql_num_rows($result);
    $numfields = mysql_num_fields($result);
    $i = $j = 0;
    while ($i < $numrows) {
        while ($j < $numfields) {
            $a[$i][$j] = mysql_result($result, $i, mysql_field_name($result, $j));
            $j++;
        }
        $i++;
        $j = 0;
    }
    mysql_free_result($result);
    return $a;
}
Esempio n. 30
0
function activewidgets_grid($name, &$data)
{
    $row_count = @mysql_num_rows($data);
    $column_count = @mysql_num_fields($data);
    $columns = "var " . $name . "_columns = [\n";
    for ($i = 0; $i < $column_count; $i++) {
        $columns .= "\"" . @mysql_field_name($data, $i) . "\", ";
    }
    $columns .= "\n];\n";
    $rows = "var " . $name . "_data = [\n";
    while ($result = @mysql_fetch_array($data)) {
        $rows .= "[";
        for ($i = 0; $i < $column_count; $i++) {
            $rows .= "\"" . activewidgets_html($result[$i]) . "\", ";
        }
        $rows .= "],\n";
    }
    $rows .= "];\n";
    $html = "<" . "script" . ">\n";
    $html .= $columns;
    $html .= $rows;
    $html .= "try {\n";
    $html .= "  var {$name} = new Active.Controls.Grid;\n";
    $html .= "  {$name}.setRowCount({$row_count});\n";
    $html .= "  {$name}.setColumnCount({$column_count});\n";
    $html .= "  {$name}.setDataText(function(i, j){return " . $name . "_data[i][j]});\n";
    $html .= "  {$name}.setColumnText(function(i){return " . $name . "_columns[i]});\n";
    $html .= "  document.write({$name});\n";
    $html .= "}\n";
    $html .= "catch (error){\n";
    $html .= "  document.write(error.description);\n";
    $html .= "}\n";
    $html .= "</" . "script" . ">\n";
    return $html;
}