SELECT 查询中的 MySQL 语法错误

发布于 2024-09-14 16:28:45 字数 1188 浏览 4 评论 0原文

大家好,

我遇到了这个语法问题,但我的查询没有发现任何问题。
我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事和酒 2024-09-21 16:28:45
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 . "'
");

您应该转义您的数据和数据库字段。

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 . "'
");

You should escape your data and DB fields.

分開簡單 2024-09-21 16:28:45

确保 $userid 变量包含某些内容。如果它是一个空字符串,你最终会得到

SELECT event.nameevent,event.eventid
FROM event,sub
WHERE sub.userid= AND

并且这将引发错误。

Make sure that the $userid variable contains something. If it's an empty string you'll end up with

SELECT event.nameevent,event.eventid
FROM event,sub
WHERE sub.userid= AND

And this will throw an error.

喜爱纠缠 2024-09-21 16:28:45

当开始创建包含 sql 语句的变量时,您会发现它很有用。

$sql = "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" ; 
$results = mysql_query( $sql ) ;`

这使您能够

echo $sql ; 

进入该页面,在其中您可以

  • 仔细查看它将
  • 语句直接粘贴到您的 SQL 管理器中。

并不是说这回答了你的问题,而是为你提供了一种区分和克服 PHP 和 SQL 错误的策略 - 特别是常见的引用错误。

在零结果的情况下,作为健全性检查非常有用,数据库中实际上存在相应的数据。

You'll find it useful when starting out to create a variable containing your sql statement.

$sql = "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" ; 
$results = mysql_query( $sql ) ;`

This affords you the ability to

echo $sql ; 

onto the page, where you can

  • take a close look at it
  • paste the statement directly into you sql manager.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文