mysqli flag fields (working code)


This is a follow-on post from <a href="https://stackoverflow.com/questions/11437650/what-do-bit-flags-in-mysqli-mean-using-fetch-field-direct" rel="nofollow">What do bit flags in mysqli mean using fetch_field_direct</a>. 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 <a href="http://www.php.net/manual/en/mysqli-result.fetch-fields.php#101828" rel="nofollow">http://www.php.net/manual/en/mysqli-result.fetch-fields.php#101828</a>). 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>"; ?>


Those flags are briefly explained in the <a href="http://dev.mysql.com/doc/refman/5.5/en/c-api-data-structures.html" rel="nofollow">C API Data Structures</a> chapter of the MySQL manual:

<pre class="lang-none prettyprint-override">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

They're linked to equivalent PHP constants as described in the <a href="http://php.net/manual/en/mysqli.constants.php" rel="nofollow">Predefined Constants</a> section of the Mysqli manual.


I know this is ancient but this might help somebody else. I think the problem is that the indices in the $flag array are read in reverse order - they should run from 18 to 0 to get the correct results. Or the descriptors should be inverted.


  • How to iterate over a SortedSet to modify items within
  • how to add Wizards created in New->File->others into popup menus when right click in project e
  • Can a running Google apps script determine its own resource id?
  • How to add html to an aspx C# codebehind page?
  • MonogoDB Replica Set Status Not changing from Startup to Secondary
  • sailsjs array query Exact match
  • Fetching Contact detail Take A lot of time in android?
  • Using imported docs from MongoDB in DerbyJS
  • How to increase MongoDB performance
  • Meteor JS Routing on Angular 2 Tutorial doesn't work
  • Overwriting/Adding an ActiveRecord association dynamically using a singleton class
  • PouchDB on Cloudant
  • Linq distinct based on two columns
  • Android CalendarContract, deleting a recurring event causes all events to disappear on calendar?
  • FindAndModify, return array of Objects
  • Mongodb exception, “ MongoCursorException' with message '$ operator made object too large”
  • Merging rows to columns
  • Zurb Foundation _global.scss meta styles for js?
  • ActiveRecord query for a count of new users by day
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • MongoError: Incorrect arguments
  • Is it possible to access block's scope in method?
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • SignalR .NET Client Invoke throws an exception
  • Where to put my custom functions in Wordpress?
  • Display Images one by one with next and previous functionality
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • json Serialization in asp
  • Buffer size for converting unsigned long to string
  • SQL merge duplicate rows and join values that are different
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • WPF Applying a trigger on binding failure
  • How do you join a server to an Active Directory (domain)?
  • Change div Background jquery
  • How to stop GridView from loading again when I press back button?
  • How does Linux kernel interrupt the application?
  • Qt: Run a script BEFORE make
  • reshape alternating columns in less time and using less memory
  • Binding checkboxes to object values in AngularJs