如何使用 php 循环具有默认前缀的 mysql 表
我查看了 从 300 个表的 mysql 数据库中选择使用默认前缀但我仍然不明白。
这是我的问题: 该数据库有 5 个表,分别命名为 pbtest01、pbtest02、pbtest03、pbtest04 和 pbtest05。我使用以下代码循环表:
$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];
但出现以下错误:
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'
有人可以帮助我吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
CONCAT()
是一个 MySQL 函数,您需要在 PHP 中构建表名称。Try this:
CONCAT()
is a MySQL function, and you need to build the table names in PHP.您不能使用
CONCAT()
作为表名。相反,只需使用 PHP 字符串连接或字符串插值来构建字符串。You can't use
CONCAT()
for a table name. Instead, just build the string with PHP string concatenation or string interpolation.您指定的数据库不正确。试试这个:
这会将单引号放在正确的字符串周围,删除不需要的
CONCAT()
函数。You're specifying your database incorrectly. Try this:
This will put the single quotes around the correct string remove the unneeded
CONCAT()
function.