SQL在表中显示数据而未指定列名 [英] Sql show data in tables without specifying the colum name

查看:171
本文介绍了SQL在表中显示数据而未指定列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这段代码:

 <?php 
 ini_set('display_errors', 1);
 ini_set('display_startup_errors', 1);
 error_reporting(E_ALL);

 include "../includes/db_conn.php";

 $data = $_POST['tabledb'];

 $sfm = $dbm->prepare('SELECT * FROM '.$data.'');
 $sfm->execute();
 $all = $sfm->fetchAll();
 ?>
 <!DOCTYPE html>
 <html>
 <body>
 <?php 
  foreach ($all as $row)
  {
  echo "<option value=\"info\">" . $row[0] . "</option>";
  }
  ?>
  </body>
  </html>

如果具有下拉菜单,则可以选择上下文,如果可以选择该表是通过ajax调用发送到此页面的,则可以从数据库中选择行.这就是为什么我有

oke a littel context if have a dropdown menu where you can select rows from the database, if you select a table that table is send to this page trough a ajax call. that is why I have:

   $data = $_POST['tabledb'];

在我的代码中.现在,我想显示表的内容,但是每个表都有不同数量的列和列名称.如何在不指定列名的情况下显示表的内容?请帮忙.

in my code. now I would like to show the contents of the tables, but every table has a different amount of columus and colum names. how do I show the contents of the tables without specifying the colum name? please help.

编辑

我尝试过的方法是,尝试将其设置为for循环,但是没有用.

what I tried, I tried making it a for loop, didn't work.

编辑2

我还不够清楚,我试图获得与print_r相同的结果,但带有回显.

I wasn't clear enough, I am trying to get the same result as print_r but with a echo.

推荐答案

您可以使用implode将每一行的所有元素组合为一个字符串:

You could use implode to combine all elements for each row into a single string:

foreach ($all as $row)
  {
      echo ‘<option value="info">’ . implode(‘|’, $row) . ‘</option>‘;
  }

或者您可以使用嵌套的foreach:

Or you could use use nested foreach:

这篇关于SQL在表中显示数据而未指定列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆