3

Here is the situation. I just know total number of columns the table test_table has but don't know their names(sounds strange but its true). Is there any way I can write UPDATE Query for all column values on basis of some ID(auto-increment primary key)?

To add row in this table, I did following which is working but don't have idea how to do it for UPDATE:

$newCols = $_POST['newRowCols'];
$query = "INSERT INTO test_table VALUES "."("."NULL";   
foreach($newCols as $col)
{
$query .= ",'$col'";
}   
$query.=")";
mysqli_query($con,$query);

Thanks.

Azeem
  • 2,904
  • 18
  • 54
  • 89
  • 2
    You may want to use mysqli_fetch_fields() on a select to get the fields http://php.net/manual/en/mysqli-result.fetch-fields.php – cmorrissey Jul 25 '13 at 22:58

1 Answers1

4

No this is not possible. But you can get the column names using the information_schema database:

SELECT 
  `ORDINAL_POSITION`,
  `COLUMN_NAME`
FROM `information_schema`.COLUMNS
WHERE
  `TABLE_SCHEMA` = $dbname
AND
  `TABLE_NAME` = $tablename
ORDER BY `ORDINAL_POSITION`
hek2mgl
  • 152,036
  • 28
  • 249
  • 266