Is there a way to check what columns are there in mysql syntax?
For example,
$dbh = $db->query("SELECT * FROM table");
while($row = $dbh->fetchAll(PDO::FETCH_ASSOC))
{
$row['somecolumn'];
}
For instance, if there is or not somecolumn, I would like to find out if there is a way that will display a list of ALL the columns in the syntax I’m looking for in table
This way in the future, I’ll be able to check if there is $row['fr'] or $row['food'] that can work.
Thanks
You could simply check whether
$rowcontains the key you need.The PHP function
array_key_existswill tell you whether$rowcontains the key you want. For instance,array_key_exists("fr", $row)will return true if$rowcontains anfrkey.If you want to see all the keys (i.e. all the columns), simply use
array_keys; for example :array_keys($row)will give you all the keys in$row.Alternatively, if you don’t mind another SQL call, you can use
SELECT column_name FROM information_schema.columns WHERE table_name='your_table'as xdazz’s answer mentions.