0

我正在尝试使用在 for 循环中递增的数组来查询我的数据库。出于某种原因,无论我做什么,这都行不通,

我得到错误:

“您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 ')' 附近使用正确的语法”

当在 PHPMyAdmin 中查询数据库时,一切都很好,所以我假设 for 循环中的某些东西搞砸了。有任何想法吗?

我的代码:-

 <?php
$submitclick=$_GET["submitclick"];
if($submitclick==1)
{
require('json.php');
$selec=$_GET["selec"];
$selec=str_replace("'","",$selec);
error_reporting(E_ALL);
mysql_connect("localhost", "seamus", "password") or
    die("Could not connect: " . mysql_error());
mysql_select_db("seamus") or die("No such database");
$sql1 = sprintf("SELECT event FROM attends WHERE student = '%s'",$selec);
$result1 = mysql_query($sql1)
  or die(mysql_error());

while ($row1 = mysql_fetch_array($result1))
 {
  $eve[] = $row1['event'];
 }

for($f=0;$f<count($eve);$f++)
{
$sql = sprintf("SELECT event.id, teaches.staff, day, start, duration, room FROM event JOIN module ON (event.module=module.id) JOIN isin ON (event.id=isin.event) JOIN teaches ON (event.id=teaches.event) JOIN attends ON (event.id=attends.event) WHERE event.id = '%s')",$eve[$f]);
$result = mysql_query($sql)
  or die(mysql_error());  
 $i=0;

while ($row = mysql_fetch_array($result))
    {
     $key =$row['day'].$row['start'];
     $event[$key] = $row['id'];
     $room[$row['id']] = $row['room'];
     $lect[$row['id']] = $row['staff'];
   $time[$i]= $row['duration'];
   $i++;
    }
}     
   $i=0;
4

2 回答 2

1

你在这里有一个额外的括号,就在'%s'

... WHERE event.id = '%s')",$eve[$f]);

只需将其删除,它应该可以工作。

于 2012-04-12T00:02:27.593 回答
1

换行:

$sql = sprintf("SELECT event.id, teaches.staff, day, start, duration, room FROM event JOIN module ON (event.module=module.id) JOIN isin ON (event.id=isin.event) JOIN teaches ON (event.id=teaches.event) JOIN attends ON (event.id=attends.event) WHERE event.id = '%s')",$eve[$f]);

至:

$sql = sprintf("SELECT event.id, teaches.staff, day, start, duration, room FROM event JOIN module ON (event.module=module.id) JOIN isin ON (event.id=isin.event) JOIN teaches ON (event.id=teaches.event) JOIN attends ON (event.id=attends.event) WHERE event.id = %d)",$eve[$f]);

因为 event.id 是一个整数。

希望有帮助。

于 2012-04-12T00:03:03.037 回答