-1

请帮助我解决指定的问题:

代码部分:

$result = mysql_query("SELECT *, UNIX_TIMESTAMP(eventdate) AS eventdate,    
  UNIX_TIMESTAMP(throughdate) AS throughdate FROM events ORDER BY eventdate where 
  id='$_GET[id];'");

// the above query is not working   

if ( mysql_num_rows($result) == 0 ) {  
   print "<p>No events right now.</p>\n";
   }
else {

    $lasteventmonth = '';
    while ($row = mysql_fetch_array($result)) { 
        $eventmonth="";
        $eventmonth = date("F Y",$row['eventdate']);

        if ($lasteventmonth != $eventmonth) {
            print "<p style='font-size: 18px;'><b>$eventmonth</b></p>";
        }
        $lasteventmonth = $eventmonth;

        showEvent($row);    
        }
    }
   ?>
........................
........................//other codes

当代码评估如下:

警告:mysql_num_rows():在第 122 行的 C:\Users\Fagun\Desktop\UsbWebserver\Root\mapcal\mapcal.php 中,提供的参数不是有效的 MySQL 结果资源 目前没有事件。++++++++

4

7 回答 7

3

致电后mysql_query,请使用以下命令:

if (! $result) {
    echo mysql_errno() . ": " . mysql_error(). "\n";
}

这将准确地告诉您,为什么 MySQL 不会运行您的查询。

于 2012-04-21T16:31:43.147 回答
0

ID 是字符串还是整数?不管怎样,我猜你不应该包含一个尾随分号?

尝试如下更改... $result = mysql_query("SELECT *, UNIX_TIMESTAMP(eventdate) AS eventdate, UNIX_TIMESTAMP(throughdate) AS throughdate
FROM events ORDER BY eventdate where id='$_GET[id]'");

于 2012-04-21T16:32:33.520 回答
0

我认为这是您如何使用构建查询和连接 id 的问题。试试这个(注意 ID 是如何连接的):

$query = "SELECT *, UNIX_TIMESTAMP(eventdate) AS eventdate,    
    UNIX_TIMESTAMP(throughdate) AS throughdate FROM events
    ORDER BY eventdate where 
    id='".$_GET[id]."'";

$result = mysql_query($query) or die(mysql_error());

您不必将其分成两部分,但是 - 这应该更易于阅读和理解。您甚至可以在运行查询之前回显查询以查看实际创建的查询,并在您的数据库上手动尝试。

ordie(mysql_error())部分将为您提供有关问题的详细信息(如果不是 ID 问题)。

于 2012-04-21T16:33:48.420 回答
0

尝试:

$result = mysql_query("SELECT   *,
                                UNIX_TIMESTAMP(eventdate) AS eventdate,    
                                UNIX_TIMESTAMP(throughdate) AS throughdate
                       FROM     events
                       ORDER BY eventdate
                       where    id= '" . intval($_GET['id']) . "'");

if($result)
{
    //Do code
}

使用 intval() 确保$_GET['id']是整数。
使用 if 语句确保查询已正确执行。

于 2012-04-21T16:33:52.707 回答
0

尝试这个

 $result = mysql_query("SELECT *, UNIX_TIMESTAMP(eventdate) AS eventdate,    
 UNIX_TIMESTAMP(throughdate) AS throughdate FROM events where 
 id='".$_GET['id']."' ORDER BY eventdate");
于 2012-04-21T16:35:58.777 回答
0

正确引用值:

$_GET[id] 应该$_GET['id']

试试下面:

$result = mysql_query("SELECT *, UNIX_TIMESTAMP(eventdate) AS eventdate,    
  UNIX_TIMESTAMP(throughdate) AS throughdate FROM events ORDER BY eventdate where 
  id='".$_GET['id']."');
于 2012-04-21T16:36:18.387 回答
0

尝试这个:

"SELECT *, UNIX_TIMESTAMP(eventdate) AS eventdate, 
    UNIX_TIMESTAMP(throughdate) AS throughdate FROM events ORDER BY eventdate where 
    id='".$_GET['id'].";'"

我假设这不是id来自用户输入。如果是这样,这很容易受到 SQL 注入攻击。

于 2012-04-21T16:37:23.053 回答