why this mysql query is not working?

匿名 (未验证) 提交于 2019-12-03 10:10:24

问题:

Please help me regarding the specified problem:

The code section:

$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 "

No events right now.

\n"; } else { $lasteventmonth = ''; while ($row = mysql_fetch_array($result)) { $eventmonth=""; $eventmonth = date("F Y",$row['eventdate']); if ($lasteventmonth != $eventmonth) { print "

$eventmonth

"; } $lasteventmonth = $eventmonth; showEvent($row); } } ?> ........................ ........................//other codes

when the code evaluates as follows:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Users\Fagun\Desktop\UsbWebserver\Root\mapcal\mapcal.php on line 122 No events right now.++++++++

回答1:

After your call to mysql_query, use this:

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

this will tell you exactly, why MySQL won't run your query.



回答2:

is ID a String or int? Either way I guess you shouldn't include a trailing semicolon?

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



回答3:

I assume it's an issue with how you're using building the query and concatenating the id. Try this instead (notice how the ID is concatenated):

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

You don't have to break it into 2 pieces, but - this should be easier to read and understand. You can even echo out the query before running it to see what query is actually being created, and try it manually on your database.

The or die(mysql_error()) part will give you specifics on what the issue is (if it wasn't the ID issue).



回答4:

Quote values properly :

$_GET[id] should be $_GET['id']

Try below:

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


回答5:

Try this:

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

I'm assuming that id does not come from user input. If it does, this is vulnerable to a SQL injection attack.



回答6:

try:

$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 } 

use intval() to make sure $_GET['id'] is an integer.
use the if statement to make sure the query has executed correctly.



回答7:

Try this

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!