这是来自What do bit flags in mysqli mean using fetch_field_direct的后续帖子。下面是一个脚本示例,它将通过添加?tablename =来处理url中指定的数据库中的表 当用户将鼠标悬停在表格标题上时显示的字段代码不反映表格中字段的属性(这些代码来自http://www.php.net/manual/en/mysqli-result.fetch-fields.php#101828中的建议)。 字段标志输出不描述表中的字段。字段标志的正确含义是什么?
<?php
/*----- Store username, password and name of database in variables
Log in to database using mysqli */
$user="username";
$pass="password";
$dbname="databaseName";
$mysqli = new mysqli("localhost", "$user", "$pass", "$dbname");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
/*----- Gets the table name from the URI
If no table is selected, choose a default table. */
if (isset($_GET['tablename'])){
$tablename = $_GET['tablename'];
}
else {
$tablename = "defaultTable";
}
$query="SELECT * FROM ".$tablename;
/*----- Query additions */
if (isset($_GET['id'])){
$id=$_GET['id'];
$query.=" WHERE ".$tablename."_id=".$id;
}
if ($query != "") {
$result = $mysqli->query($query);
//First, find the number of fields returned in the query and set fieldCount to 0
$numFields=$result->field_count;
$numRows=$result->num_rows;
$fieldCount=0;
//If the number of rows or the number of fields equals zero, add an error message
$errorText = "";
if ($numFields == "0") {
$errorText .= "No fields in this table.";
}
if ($numRows == "0") {
$errorText .= "Query did not return any results.";
}
//Next, store the field names in an array
do{
$field[$fieldCount] = mysqli_fetch_field_direct($result, $fieldCount);
$fieldname[]=$field[$fieldCount]->name;
$fieldlength[]=$field[$fieldCount]->length;
$fieldflags[]=$field[$fieldCount]->flags;
$fieldCount++;
}
while ($numFields>$fieldCount);
while($row = $result->fetch_array(MYSQLI_BOTH))
{
$rows[] = $row;
}
}
?>
<table >
<thead>
<?php
$fieldCount=0;
while ($fieldCount<$numFields)
{
$flag = decbin($fieldflags[$fieldCount]);
/* str_pad(string, length of desired string, what to pad with, 0 for right or 1 for left or 2 for both) */
$flag = str_pad($flag,'19','0','0');
echo "<th title=\"";
echo $flag ."\n";
if ($flag[0] == "1" ) {echo "NOT NULL \n";}
if ($flag[1] == "1" ) {echo "PRIMARY KEY \n";}
if ($flag[2] == "1" ) {echo "UNIQUE KEY \n";}
if ($flag[3] == "1" ) {echo "MULTIPLE KEY \n";}
if ($flag[4] == "1" ) {echo "BLOB \n";}
if ($flag[5] == "1" ) {echo "UNSIGNED \n";}
if ($flag[6] == "1" ) {echo "ZEROFILL \n";}
if ($flag[7] == "1" ) {echo "BINARY \n";}
if ($flag[8] == "1" ) {echo "ENUM \n";}
if ($flag[9] == "1" ) {echo "AUTO INCREMENT \n";}
if ($flag[10] == "1" ) {echo "TIMESTAMP \n";}
if ($flag[11] == "1" ) {echo "SET \n";}
if ($flag[12] == "1" ) {echo " \n";}
if ($flag[13] == "1" ) {echo " \n";}
if ($flag[14] == "1" ) {echo "PART KEY \n";}
if ($flag[15] == "1" ) {echo "GROUP FLAG \n";}
if ($flag[16] == "1" ) {echo "NUM FLAG \n";}
if ($flag[17] == "1" ) {echo "UNIQUE FLAG \n";}
if ($flag[18] == "1" ) {echo " \n";}
echo "\">".ucfirst(str_replace($tablename."_","",$fieldname[$fieldCount])) ."</th>";
$fieldCount++;
}
?>
<td></td>
</thead>
<tbody>
<?php
$rowCounter = 0;
$fieldCount=0;
while($rowCounter < $numRows){
echo "
<form name=\"update-".$rows[$rowCounter][0]."\"
id=\"update-".$rows[$rowCounter][0]."\"
method=\"post\" >
<tr id=\"update-".$rows[$rowCounter][0]."\">
";
while($fieldCount < $numFields){
echo"
<td><input size=\"$fieldlength[$fieldCount]\" maxlength=\"$fieldlength[$fieldCount]\" value=\"".$rows[$rowCounter][$fieldCount]."\" /></td>
";
$fieldCount++;
}
$fieldCount=0;
echo"
<td>
<a href=\"thispage.php?tablename=".$tablename."&id=".$rows[$rowCounter][0]."\" >view</a>
</td>
</tr>
</form>";
$rowCounter++;
}
$rowCounter=0;
echo "<tbody>
</table>";
?>
答案 0 :(得分:1)
这些标志在MySQL手册的C API Data Structures章节中进行了简要说明:
Flag Value Flag Description
NOT_NULL_FLAG Field can't be NULL
PRI_KEY_FLAG Field is part of a primary key
UNIQUE_KEY_FLAG Field is part of a unique key
MULTIPLE_KEY_FLAG Field is part of a nonunique key
UNSIGNED_FLAG Field has the UNSIGNED attribute
ZEROFILL_FLAG Field has the ZEROFILL attribute
BINARY_FLAG Field has the BINARY attribute
AUTO_INCREMENT_FLAG Field has the AUTO_INCREMENT attribute
ENUM_FLAG Field is an ENUM (deprecated)
SET_FLAG Field is a SET (deprecated)
BLOB_FLAG Field is a BLOB or TEXT (deprecated)
TIMESTAMP_FLAG Field is a TIMESTAMP (deprecated)
NUM_FLAG Field is numeric; see additional notes following table
NO_DEFAULT_VALUE_FLAG Field has no default value; see additional notes following table
它们与等效的PHP常量相关联,如Mysqli手册的Predefined Constants部分所述。