我正在尝试使用一个在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;
答案 0 :(得分:1)
此处有一个额外的括号,就在'%s'
之后:
... WHERE event.id = '%s')",$eve[$f]);
只需删除它就可以了。
答案 1 :(得分: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是一个整数。
希望有所帮助。