gpt4 book ai didi

php - mysqli 标志字段(工作代码)

转载 作者:行者123 更新时间:2023-11-29 13:59:33 26 4
gpt4 key购买 nike

这是 What do bit flags in mysqli mean using fetch_field_direct 的后续帖子。以下是一个脚本示例,该脚本将通过添加 ?tablename= 来处理 url 中指定的数据库中的表当用户将鼠标悬停在表标题上时显示的字段代码并不反射(reflect)表中字段的属性(这些代码来自 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>";
?>

最佳答案

C API Data Structures 中对这些标志进行了简要说明。 MySQL手册章节:

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 常量,如 Predefined Constants 中所述。 Mysqli 手册的部分。

关于php - mysqli 标志字段(工作代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15322548/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com