mysqli标志字段(工作代码) [英] mysqli flag fields (working code)

查看:40
本文介绍了mysqli标志字段(工作代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是来自使用fetch_field_direct ,mysqli中的位标志是什么意思.这是一个脚本示例,该脚本将通过添加?tablename = 用户将鼠标悬停在表标题上时显示的域代码不反映表中域的属性(这些代码来自

This is a follow-on post from What do bit flags in mysqli mean using fetch_field_direct. Here is an example of a script that will process a table in a database specified in the url by adding ?tablename= The field codes that are displayed when the user mouse-overs the table headings do not reflect the attributes of the fields in the table (these codes come from suggestions in http://www.php.net/manual/en/mysqli-result.fetch-fields.php#101828). The field flags output do not describe the fields in the table. What are the correct meanings for the field flags?

<?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>";
?>

推荐答案

预定义常量所述,它们已链接到等效的PHP常量. Mysqli手册中的"部分.

They're linked to equivalent PHP constants as described in the Predefined Constants section of the Mysqli manual.

这篇关于mysqli标志字段(工作代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆