I looked at select from mysql db with 300 tables using a default prefix but I still don't understand.
Here's my problem: The database has 5 tables named as pbtest01, pbtest02, pbtest03, pbtest04 and pbtest05. I use the following code to loop the tables:
$x = 3;
for($k = 1; $k <= $x; $k++){
$sql5 = "SELECT *
FROM CONCAT('pbtest0',$k)
WHERE id = '930820105627'
";
$data5 = mysql_query($sql5) or die(mysql_error().$sql5);
$list5 = mysql_fetch_array($data5);
$var[$k] = $sql5['value'];
}
echo $var[1];
echo $var[2];
echo $var[3];
but get the following error:
You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near
'CONCAT('pbtest0',1) WHERE id = '930820105627'' at line 1
SELECT *
FROM CONCAT('pbtest0',1)
WHERE id = '930820105627'
Can someone help me?
Try this:
for($k = 1; $k <= $x; $k++){
$sql5 = "SELECT *
FROM pbtest0" . $k . "
WHERE id = '930820105627'
";
...
}
CONCAT()
is a MySQL function, and you need to build the table names in PHP.
You can't use CONCAT()
for a table name. Instead, just build the string with PHP string concatenation or string interpolation.
You're specifying your database incorrectly. Try this:
"SELECT * FROM 'pbtest0".$k."' WHERE id='930820105627'"
This will put the single quotes around the correct string remove the unneeded CONCAT()
function.