title: mysqli flag fields (working code)
body: <p>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">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 noreferrer">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? </p>
  
  <pre><code>&lt;?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-&gt;connect_errno) {
      printf("Connect failed: %s\n", $mysqli-&gt;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-&gt;query($query);
  
      //First, find the number of fields returned in the query and set fieldCount to 0
      $numFields=$result-&gt;field_count;
      $numRows=$result-&gt;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]-&gt;name;
          $fieldlength[]=$field[$fieldCount]-&gt;length;
          $fieldflags[]=$field[$fieldCount]-&gt;flags;
          $fieldCount++;
      }
      while ($numFields&gt;$fieldCount);
  
      while($row = $result-&gt;fetch_array(MYSQLI_BOTH))
      {
          $rows[] = $row;
      }
  }
  
          ?&gt; 
          &lt;table &gt;
              &lt;thead&gt;
              &lt;?php
                  $fieldCount=0;
                  while ($fieldCount&lt;$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 "&lt;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 "\"&gt;".ucfirst(str_replace($tablename."_","",$fieldname[$fieldCount])) ."&lt;/th&gt;";
                      $fieldCount++;
                  }
              ?&gt;
              &lt;td&gt;&lt;/td&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
  &lt;?php
              $rowCounter = 0;
              $fieldCount=0;
              while($rowCounter &lt; $numRows){
                  echo "
  
                  &lt;form name=\"update-".$rows[$rowCounter][0]."\"
                        id=\"update-".$rows[$rowCounter][0]."\"
                        method=\"post\" &gt;
                  &lt;tr id=\"update-".$rows[$rowCounter][0]."\"&gt;
                  ";
  
                  while($fieldCount &lt; $numFields){
                      echo"
                          &lt;td&gt;&lt;input size=\"$fieldlength[$fieldCount]\" maxlength=\"$fieldlength[$fieldCount]\" value=\"".$rows[$rowCounter][$fieldCount]."\" /&gt;&lt;/td&gt;
                      ";
                      $fieldCount++;
                  }
                  $fieldCount=0;
                  echo"
                  &lt;td&gt;
                  &lt;a href=\"thispage.php?tablename=".$tablename."&amp;id=".$rows[$rowCounter][0]."\" &gt;view&lt;/a&gt;
                  &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;/form&gt;";
                  $rowCounter++;
              }
              $rowCounter=0;
      echo "&lt;tbody&gt;
          &lt;/table&gt;";
  ?&gt;
  </code></pre>