I am trying to extract a column name from SQL database to use in a dropdown in HTML. I am then trying to insert PHP into the HTML dropdown to display column name.
I have used fetch array to get output from SQL using a variable from another drop down to pull the relevant SQL table. However i can't return the column name in PHP. All I get is the value using $row[2].
Is there an easy way to pull the column name (ie the non-numeric value in square brackets after the numerical value).
$query="SELECT * FROM ".($units_cat)."";
if ($result = mysqli_query($link, $query)) {
$row = mysqli_fetch_array($result);
echo nl2br (" \n Query was successful \n");
print_r($row);
echo "<br>";
}
HTML
<option value="<?php $row[2]?>"
<?php
if($_POST['units_from']==$row)
echo 'selected="selected"';?>
>
<?php echo ($row[2])?>
</option>
OUTPUT FROM SQL QUERY
Array (
[0] => 1
[id] => 1
[1] => Klbs
[from_value] => Klbs
[2] => 1.0000000000
[klbs] => 1.0000000000
[3] => 1000.0000000000
[pound] => 1000.0000000000
[4] => 0.4535923700
[ton] => 0.4535923700
[5] => 453.5923700000
[kg] => 453.5923700000
[6] => 224.8089424432
[newton] => 224.8089424432
[7] => 22.4808942443
[dn] => 22.4808942443
[8] => 0.2248089424
[kn] => 0.2248089424
)
I was able to do this by using DESCRIBE to output array of table data. Then create an array of the columns.
$query2=("DESCRIBE ".mysqli_real_escape_string($link, $units_cat));
if ($result2 = mysqli_query($link, $query2)) {
$table_desc = mysqli_fetch_all($result2);
#Create an array of column values
foreach($table_desc as $val) {
$col[]=$val[0];
}
Then call the column values in the relevant dropdown.
<option value="<?php echo($col[2]) ?>"<?php if($_POST['units_from']==($col[2])) echo
'selected="selected"';?>><?php echo($col[2]) ?></option>
I had to white-list the tables that $units_cat was referencing as I believe you cannot prepare a table as you are unable to bind parameters to a table entry in PHP/SQL.
#Get tables list for whitelisting
$query3="Show tables";
$result3=mysqli_query($link,$query3);
if (!$result3) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysqli_error();
exit;
}
if ($result3=mysqli_query($link,$query3)){
while($row=mysqli_fetch_row($result3)){
$tables[]=$row[0];
}
}
#UNITS CAT WHITELIST CHECK
if (in_array($units_cat,$tables)){
echo nl2br (" \n Units Cat Table exists");
}else{
echo "Table does not exist";
mysqli_close($link);
echo "<br>";
die("non valid entry");
}
with mysql_fetch_array you can access each parameter individually $row['id']; $row['pound'];
However, storing the different values for a same variable (in kg, in tons , in...) in the database seems a bit unnecessary taking into account that you can save the space with a simple conversion using the metric unit. do your users need to input the weight in different units each time?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.