2016-06-16 35 views
0

我想在我的数据库中获取我的表的列表,并将这些名称变成变量,但无论我尝试什么,我都会收到“意外的T_LNUMBER,期待T_VARIABLE”错误。 这是有问题的部分:如何将表名变成变量?

$result1 = mysqli_query($connection, "SHOW TABLES LIKE '%".$date3."%'"); 
$row1 = mysqli_fetch_row($result1); 

if (isset($row1[0])) { $01 = $row1[0];} 
if (isset($row1[1])) { $02 = $row1[1];} 

我在做什么错?

+0

可能是你需要阅读这http://stackoverflow.com/questions/2468497/php-using-regex-to- get-the-tablename-from-a-mysql-query –

+0

根据$ date3'的分配情况,你可能会打开SQL代码注入。您应该使用参数化查询。 – chris85

+0

假设你的表名是用户,你需要用户作为$ user或其他东西。 –

回答

2

由于PHP变量不能以数字,只能是字母或下划线开头,因此发生错误。

如果你要坚持你目前的命名规则,尝试使用$的一个,而不是$ 01

编辑

链接到文档: http://php.net/manual/en/language.variables.basics.php#language.variables.basics

+1

可能还想添加一个链接到手册,http://php.net/manual/en/language.variables.basics.php。 – chris85

+0

@ chris85添加链接。谢谢先生。 – badandyomega

0
// you can try following one to 
$result1 = mysqli_query($connection, "SHOW TABLES LIKE '%".$date3."%'"); 
$row1 = mysqli_fetch_row($result1); 
//$$row1[0] will create a variable with table name 
//if $row1[0] hold the value user then $$row1[0] is the variable $user and you can access in below the declaration 
if (isset($row1[0])) { $$row1[0] = $row1[0];} 
if (isset($row1[1])) { $$row1[1] = $row1[1];}