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;

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

我收到错误: p>

“你有一个 SQL语法中的错误;检查与MySQL服务器版本对应的手册,以便在数据库中第1行“ p> blockquote>

附近的')'附近使用正确的语法 在PHPMyAdmin中查询一切都很好,所以我假设for循环中的某些东西搞砸了。 有什么想法吗? p>

我的代码: - p>

 &lt;?php 
 $ submitclick = $ _ GET [“submitclick”]; \  NIF($ submitclick == 1)
 {
require( 'json.php'); \ N $ SELEC = $ _ GET [ “SELEC”]; 
 $的SELEC = str_replace函数( “'”, “”,$ SELEC  ); 
error_reporting(E_ALL); 
mysql_connect(“localhost”,“seamus”,“password”)或
 die(“无法连接:”。mysql_error()); 
mysql_select_db(“seamus”)或die(  “没有这样的数据库”); 
 $ sql1 = sprintf(“SELECT event FROM at WHERE student ='%s'”,$ selec); 
 $ result1 = mysql_query($ sql1)
或die(mysql_error()  ); 
 
而($ row1 = mysql_fetch_array($ result1))
 {
 $ eve [] = $ row1 ['event']; 
} 
 
for($ f = 0; $ f&lt;  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教ON(event.id = teaches.event)JOIN参加ON(event.id = attends.event)WHERE event.id ='%s')“,$ eve  [$ f]); 
 $ result = mysql_query($ sql)
或die(mysql_error());  
 $ i = 0; 
 
而($ 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; 
  code>  pre  > 
  div>

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

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

Just remove it and it should work.

Change the line:

$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]);

to:

$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]);

since event.id is an integer.

Hope that helps.