我看了看,但还是不明白。
我的问题是:
数据库有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'

有人能帮我吗?

最佳答案

试试这个:

for($k = 1; $k <= $x; $k++){

   $sql5 = "SELECT *
            FROM pbtest0" . $k . "
            WHERE id = '930820105627'
            ";
   ...
}

CONCAT()是一个MySQL函数,需要用PHP构建表名。

10-06 08:39