简体   繁体   English

来自PHP中SQL查询的列名

[英]Column name from sql query in php

I am trying to extract a column name from SQL database to use in a dropdown in HTML. 我试图从SQL数据库中提取列名,以用于HTML的下拉列表中。 I am then trying to insert PHP into the HTML dropdown to display column name. 然后,我尝试将PHP插入HTML下拉列表中以显示列名称。

I have used fetch array to get output from SQL using a variable from another drop down to pull the relevant SQL table. 我已经使用fetch数组使用另一个下拉列表中的变量提取SQL来从SQL获取输出。 However i can't return the column name in PHP. 但是我不能在PHP中返回列名。 All I get is the value using $row[2]. 我所得到的只是使用$row[2].的值$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 的HTML

<option value="<?php $row[2]?>"
        <?php 
        if($_POST['units_from']==$row) 
            echo 'selected="selected"';?>
        >
<?php echo ($row[2])?>
</option>

OUTPUT FROM SQL QUERY 从SQL查询输出

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. 通过使用DESCRIBE输出表数据数组,我能够做到这一点。 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. 我不得不将$ units_cat所引用的表列入白名单,因为我相信您无法准备表,因为您无法将参数绑定到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']; 使用mysql_fetch_array,您可以单独访问每个参数$ row ['id']; $row['pound']; $ 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? 您的用户是否需要每次以不同的单位输入重量?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM