如何使用PHP中的爆破获取数据库表的列名

时间:2019-05-20 00:11:15

标签: php sql mysqli phpmyadmin

我想获取implode中的列而不是行的列表,但这给了我一个错误,但是当我使用数组的索引号时却给了我行的结果。 / p>

<?php
// this is the database connection
$session_id = session_start();
$con = mysqli_connect("localhost", "root", "", "ecommerce");
?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>array</title>
    </head>
    <body>
<?php
$sql_select = "SELECT * FROM cart";
$run_select = mysqli_query($con, $sql_select);
$datas = [];
if(mysqli_num_rows($run_select) > 0) {

    while($show = mysqli_fetch_assoc($run_select)) {

        $id[] = $show;
    }
}

$avengers = implode(",", $id['1']);
// i want to echo out the columns this is giving me the rows
echo $avengers;

1 个答案:

答案 0 :(得分:0)

while ($show = mysqli_fetch_assoc($run_select)) {

      $id[] = $show;
     }

$avengers = array_column($id, 'COLUMN_NAME');

print_r($avengers);

这会以字符串形式返回表或数组中列名/变量名的简单数组,这是我动态构建MySQL查询所需要的。

说明:-

<?php
// An array that represents a possible record set returned from a database
$a = array(
  array(
    'id' => 5698,
    'first_name' => 'Peter',
    'last_name' => 'Griffin',
  ),
  array(
    'id' => 4767,
    'first_name' => 'Ben',
    'last_name' => 'Smith',
  ),
  array(
    'id' => 3809,
    'first_name' => 'Joe',
    'last_name' => 'Doe',
  )
);

$last_names = array_column($a, 'last_name');
print_r($last_names);
?>
  

输出:

Array
(
  [0] => Griffin
  [1] => Smith
  [2] => Doe
)