2016-06-16 38 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];}