duandongjin5647 2012-04-11 23:59 采纳率: 0%
浏览 68
已采纳

php中的mysql语法错误,输入phpmyadmin时工作正常

I'm trying to query my database using a an array that increments in a for loop. for some reason this will not work no matter what I do,

I get the the 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 ')' at line 1"

when the database is queried in PHPMyAdmin everything comes out fine so im assuming something in the for loop is screwing it up. Any ideas?

My code :-

 <?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;
  • 写回答

2条回答 默认 最新

  • dtq7387 2012-04-12 00:02
    关注

    You have an extra parentheses here, just after '%s':

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

    Just remove it and it should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?