phpnoob
phpnoob

Reputation: 141

How to turn table names into variables?

I'm trying to get a list of my tables in my database and turn those names into variables, but no matter what I try I get the "unexpected T_LNUMBER, expecting T_VARIABLE" error. This is the problematic part:

$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];}

What am I doing wrong?

Upvotes: 0

Views: 59

Answers (2)

Veshraj Joshi
Veshraj Joshi

Reputation: 3589

// 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];}

Upvotes: 0

badandyomega
badandyomega

Reputation: 252

The error is occurring because PHP variables cannot start with numbers, only letters or underscores.

If you're going to stick with your current naming convention, try using $one instead of $01.

Edit

Link to docs: http://php.net/manual/en/language.variables.basics.php#language.variables.basics

Upvotes: 2

Related Questions