在不知道列名的情况下获取Mysql结果 [英] Getting a Mysql Results without knowing a column name

查看:229
本文介绍了在不知道列名的情况下获取Mysql结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我仍在学习PHP和MySQL,所以我想知道是否可以在不知道表名的情况下查询表.知道我要查询的表和想要检索的列后,我可以编写类似

As I am still learning PHP and MySQL, I would like to know if it is possible to query a table without knowing it's name. Knowing the table I am querying and the column I would like to retrieve, I can write something like

$book_title=$row['book_title'];

然后,我可以稍后在脚本中使用结果变量.在每种情况下,每个书籍类别表都会有一个感兴趣的列,并具有不同的名称.我有几个要在其上运行查询的表.我能够通过使用始终计算为正确表名的变量来查询任何表,因为用户的所有输入都对应于数据库中的表,因此$ _POST超全局变量将始终携带正确的表名.问题是我要拥有

Then I can use the resulting variable later in the script. In each case, each book category table will have a column of interest with a different name. I have several tables on which I am running queries. I am able to query any table by using a variable that always evaluates to the correct table name, because all the input from users corresponds to the tables in the database, so the $_POST super global will always carry a correct table name. The problem is for me to have a

$variable=$row['column'];

即使我知道表名,也无法事先知道列名.

in cases where I do not know a column name before hand even though I know the table name.

我的查询很简单,看起来像

My queries are simple, and look like

query="select * FROM $book_categories WHERE id=$id";
$row = mysqli_fetch_array ($result);
$variable=$row['?'];

问号说,我不知道要期待哪一列,因为它的名称可能是数据库表中的任何内容!

The question mark say, I do not know what column to expect, as it's name could be anything from the tables in the database!

由于我有多个表,因此查询在一个表上将为零,但是每个表中的列名各不相同,因此我希望能够使用一个查询来为我提供来自该列的条目.

Since I have several tables, the query will zero on a table, but the column names in each table varies so I would like to be able to use one query that can give me an entry from such a column.

我希望我的问题很清楚,我不要求不可能的事情.如果模棱两可,我想澄清一下(希望如此).

I hope my question is clear and that I am not asking for the impossible. If it's ambiguous, I care to elucidate (hope so).

推荐答案

我不确定您的意思,但是可以通过键入index(以0开头)来引用特定列,如下所示:$row[0], $row[1]其中0表示返回的记录集的第一列,1表示返回的记录集的第二列.

I'm not sure what you mean, but it is possible to reference specifc columns by typing index (starting with 0) something like this: $row[0], $row[1] where 0 indicates the first column, and 1 indicates the second column from the returned recordset.

示例: 如果您有这样的选择语句:

Example: If you have a select-statement like this:

SELECT title, author FROM books

您可以使用$row[0], $row[1]

如果尝试获取$row[2]的值,则将获得未分配的值,因为记录集中只有两列(0和1).

If you try to get the value of $row[2] you will get an unassigned value because there are only two columns (0 and 1) from the recordset.

如果您具有这样的选择语句:

If you have a select-statement like this:

SELECT * FROM book_categories

,记录集返回三列,然后您可以使用$row[0], $row[1] and $row[2]访问它们. $row[3]不存在,因为只有三列(0、1和2)

and the recordset returns three columns, then you could access these with $row[0], $row[1] and $row[2]. $row[3] does not exist because there are only three columns (0,1 and 2)

这篇关于在不知道列名的情况下获取Mysql结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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