本文整理汇总了PHP中mysql_num_fields函数的典型用法代码示例。如果您正苦于以下问题:PHP mysql_num_fields函数的具体用法?PHP mysql_num_fields怎么用?PHP mysql_num_fields使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了mysql_num_fields函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: mysql_query_cache
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;
}
示例2: get_table_fields
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;
}
示例3: showQuery
/**
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> {$item}</td>\n";
}
$html .= "</tr>\n";
$count++;
}
echo $html;
}
示例4: update_emp_values
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);
}
示例5: update_restaurant
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";
}
示例6: formatJSON
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;
}
示例7: tabledump
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;
}
示例8: display_db_query
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";
}
示例9: database_query
/**
* 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;
}
}
示例10: DB_dump_result
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> </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;
}
示例11: WriteHistoryFromSQL
/**
* 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>";
}
}
}
}
示例12: display_db_query
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]), "&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";
}
示例13: display_db_query
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";
}
示例14: loadFromResult
function loadFromResult($mysqlresult, $dblink)
{
$this->numRows = @mysql_num_rows($mysqlresult);
$this->numFields = @mysql_num_fields($mysqlresult);
$this->result = $mysqlresult;
return parent::load($dblink);
}
示例15: get_content
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);
}