Get column names and values in for loop PHP

I would like to get all column names and values from a specific table. The fields in this table aren't fixed, so sometimes there will be added one field by the user and they also can be removed. What I want to do is retrieve all column names and getting the values of it. As far as I am now is retrieving the column names and getting one value out of it. I know why I get this result, but I don'tknow how to fix it.

//I edited this sql, normally the table value and id are variable $sql = "SELECT * FROM ".$_POST['moduleName']." WHERE modItemID= ".$_POST['modItemID']; $query = mysql_query($sql); $columns = mysql_num_fields($query); for($i = 1; $i < $columns; $i++) { //read field name $fieldName = mysql_field_name($query,$i); while($row = mysql_fetch_assoc($query,$i)){ echo $fieldName."=".$row[$fieldName]; } }

result: naam=Ketting


mysql_fetch_assoc returns an associative array. You can use a foreach loop to retreive the key=>value of each item in the array.

$sql = "SELECT * FROM products WHERE modItemID= 14"; $query = mysql_query($sql); $columns = mysql_num_fields($query); for($i = 1; $i < $columns; $i++) { //read field name $fieldName = mysql_field_name($query,$i); while($row = mysql_fetch_assoc($query,$i)){ foreach($row as $column=>$value) { echo "$column = $value\n"; } echo $fieldName."=".$row[$fieldName]; } }


Actually really easy solution to your problem would be to use foreach loop, like so:

while($row = mysql_fetch_assoc($query)) { foreach($row as $key => $value) { echo "$key=$value"; } }


Just Put your Database name,username,password and table name.

You get all data from your main database (with column name)

<?php function qry($q){ global $qry; try { $host = "?"; $dbname = "?"; $username = "?"; $password = "?"; $dbcon = new PDO("mysql:host=$host; dbname=$dbname","$username","$password"); } catch (Exception $e) { echo "ERROR ".$e->getMEssage(); } $qry = $dbcon->query($q); $qry->setFetchMode(PDO:: FETCH_OBJ); return $qry; } echo "<table>"; /*Get Colums Names in table row */ $columns = array(); $qry1= qry("SHOW COLUMNS FROM Your_table_name"); while (@$column = $qry1->fetch()->Field) { echo "<td>".$column."</td>"; $columns[] = $column; } echo "<tr>"; /* Fetch all data into a html table * $qry2 = qry("SELECT * FROM Your_table_name"); while ( $details = $qry2->fetch()) { echo "<tr>"; foreach ($columns as $c_name) { echo "<td>".$details->$c_name."</td>"; } } echo "</table>"; ?>


  • Retrieve information from checkboxes php/mysql
  • physicalDirectory and Redirection in azure
  • Javascript regex to match a pattern but NOT match a regex literal (r.js optimizer and uglify issue)?
  • ng-click not gettring triggered in accordian mode for ui-bootstarp-tpls.js when browser is adjusted
  • Migrate Native Module from Gradle Experimental Plugin to Stable Gradle
  • Android native libs disappearing once app is installed
  • Unable to apply custom pipe to high chart tooltip values
  • importing d3.event into a custom build using rollup
  • ES6 import in for-of-loop
  • Validation errors when saving
  • Import a Powershell Module on a Remote Server in a C# Runspace
  • How to get a module instance in PRISM
  • @PathVariable in SpringBoot with slashes in URL
  • Retrieve Greek letters from MSSQL database with PHP
  • Pass dropdown value to code behind static method
  • vb.net running a exe in memory
  • How to throw an exception, if an object structure is not matching/fitting another one
  • how to get context of a tapped list item to show details in another page in Nativescript
  • Describe relationship in maximo 7.5
  • How to create a TFS alert for changes to the items' Stack Rank field
  • Get column names and values in for loop PHP
  • Cannot access sitecore item field via API
  • How to alias an executable using Powershell permanently?
  • Access isolated scope in angular directive template
  • Display Images one by one with next and previous functionality
  • align graphs with different xlab
  • Return words with double consecutive letters
  • Unanticipated behavior
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • Can't mass-assign protected attributes when import data from csv file
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal