SELECT 查询中的 MySQL 语法错误
大家好,
我遇到了这个语法问题,但我的查询没有发现任何问题。
我对 SQL 有点陌生,因为我还是一名本科生。如果您能帮我解决这个问题,我将不胜感激。
这是代码:
$results = mysql_query("SELECT event.nameevent,event.eventid
FROM event,sub
WHERE sub.userid=$userid AND event.eventid=sub.orgid AND
EXTRACT(YEAR FROM startdate)=$year AND EXTRACT(MONTH FROM startdate)=$month
AND EXTRACT(DAY FROM startdate)=$list_day")
or die(mysql_error());
if(mysql_num_rows($results) >0 )
{
while($row=mysql_fetch_assoc($results))
{
$nameevent = $row['nameevent'];
$eventid = $row['eventid'];
$calendar.="<a href='memberview.php?eventid=$eventid'>".$nameevent."</a>";
$calendar.= "<br>";
}
}
else
{
$calendar.='you have no events today';
}
它返回此错误:
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 'AND event.eventid=sub.orgid AND
EXTRACT(YEAR FROM startdate)=2010 AND EXTRACT( at line 3
我在空白页面中测试了此代码,它可以工作,但是当我将其集成到日历页面中时,它不起作用。有什么建议吗?
我测试了 $userid、$year、$month 和 $day 并且都返回一个值。
Hi everyone
I'm getting this syntax problem and I don't see anything wrong with my query.
I'm kind of new at SQL as I'm still an undergrad. I'd really appreciate it if you could help me with this.
This is the code:
$results = mysql_query("SELECT event.nameevent,event.eventid
FROM event,sub
WHERE sub.userid=$userid AND event.eventid=sub.orgid AND
EXTRACT(YEAR FROM startdate)=$year AND EXTRACT(MONTH FROM startdate)=$month
AND EXTRACT(DAY FROM startdate)=$list_day")
or die(mysql_error());
if(mysql_num_rows($results) >0 )
{
while($row=mysql_fetch_assoc($results))
{
$nameevent = $row['nameevent'];
$eventid = $row['eventid'];
$calendar.="<a href='memberview.php?eventid=$eventid'>".$nameevent."</a>";
$calendar.= "<br>";
}
}
else
{
$calendar.='you have no events today';
}
It returns this 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 'AND event.eventid=sub.orgid AND
EXTRACT(YEAR FROM startdate)=2010 AND EXTRACT( at line 3
I tested this code in a blank page and it works however when I integrate it inside the calendar page it doesn't work. Any suggestions?
I have tested $userid, $year, $month and $day and all returns a value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该转义您的数据和数据库字段。
You should escape your data and DB fields.
确保 $userid 变量包含某些内容。如果它是一个空字符串,你最终会得到
并且这将引发错误。
Make sure that the $userid variable contains something. If it's an empty string you'll end up with
And this will throw an error.
当开始创建包含 sql 语句的变量时,您会发现它很有用。
这使您能够
进入该页面,在其中您可以
并不是说这回答了你的问题,而是为你提供了一种区分和克服 PHP 和 SQL 错误的策略 - 特别是常见的引用错误。
在零结果的情况下,作为健全性检查非常有用,数据库中实际上存在相应的数据。
You'll find it useful when starting out to create a variable containing your sql statement.
This affords you the ability to
onto the page, where you can
Not saying this is answering your question, but providing you with a strategy to divide and conquer PHP and SQL errors - especially common quoting errors.
Jolly useful as a sanity check that there actually is corresponding data in your database in the case of zero results.