...

View Full Version : mysql_num_rows supplied argument is not a valid MySQL result resource?



LJackson
10-13-2009, 03:53 PM
Im Getting the above message on my page dispite it actually printing out the correct data as well?

here is my code but im not sure why im getting the warning? any ideas


<?php
$getNews = "SELECT event_headline FROM events WHERE YEAR(dateSubmitted) = YEAR(CURDATE()) ORDER BY dateSubmitted ASC";
$sql = mysql_query($getNews);?>
<div class="currentNews_Container">
<div class="currentNews_Header"><?php print "Events ". date('Y');?></div>
<?php
if(mysql_num_rows($sql) <> 0)
{
while($eventStories = mysql_fetch_array($sql))
{?>
<div class="NewsItem_Container">
<?php print $eventStories['news_headline'];?>
</div><?php
}
}
else
{
print "No Current Events";
}

?>
</div>


thanks
Luke

SKDevelopment
10-13-2009, 04:04 PM
Try to change the line


$sql = mysql_query($getNews);

to


$sql = mysql_query($getNews) or die(mysql_error());

Some error would be output which should hint what is wrong ...

Edit: Of course or die(mysql_error()) should be commented or removed in the Production environment when debugging is over.

LJackson
10-13-2009, 04:08 PM
thanks mate managed to solve it from the error returned :)

really need to start getting into the habbit of adding
"or die(mysql_error())" saves alot of time :D

cheers

LJackson
10-13-2009, 06:26 PM
ok again probably something simple,

but any ideas as to why this wont work

<?php
$getnews = "SELECT * FROM $type WHERE headline = $headline";
$sql = mysql_query($getnews) or die(mysql_error());
while($row = mysql_fetch_array($sql)){
print $row['content'];
}?>


its printing out

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 'a test news story' at line 1

ive tried adding single quotes around the varibles but it still wont work?
any ideas please

thanks
Luke

LJackson
10-13-2009, 06:33 PM
ok i finally solved it by using this

$getnews = "SELECT * FROM ".$type." WHERE headline = '".$headline."'";


cheers

LJackson
10-14-2009, 02:25 PM
one problem with my above solution is that if a headline has an ' in it it breaks the sql

and its returning an 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.

here is my code of the page before the one with the error with the data being sent via the url:

while($newsStories = mysql_fetch_array($sql))
{?>
<div class="NewsItem_Container">
<a href="information.php?type=news&amp;headline=<?php echo urlencode($newsStories['headline'])?>">
<?php print $newsStories['headline'];?>
</a>
</div><?php
}


and my other page where the error is appearing is:

<?php

if(isset($_GET['type'])){
$type = $_GET['type'];
$type = strtolower($type);
}

if(isset($_GET['headline'])){
$headline = urldecode($_GET['headline']);
$headline = htmlspecialchars($headline);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Kyushindo to Mayo Shin Do Karate Judo Martial Arts in UK</title>
<link rel="stylesheet" type="text/css" href="page_styling.css" />
<?php require("functions.php");
include_once("db_info.php");?>
</head>

<body>
<?php header_info(); ?>

<div id="wrapper">

<div id="left">
<?php include("menu.php");?>
</div>

<div id="right">
<div class="pageinformation">
<div class="pageinformationHeader"><?php print $headline ?></div>
<div class="infocontainer">
<div class="pageinfomationIMG"><img src="testkarate.jpg" /></div>

<?php
$getnews = "SELECT * FROM ".$type." WHERE headline = '".$headline."'";
$sql = mysql_query($getnews) or die(mysql_error());
while($row = mysql_fetch_array($sql)){
print htmlspecialchars($row['content']);
}?>
</div>
</div>
</div>

<?php footer();?>
</div>
</body>
</html>


any ideas how to solve this please
thanks
Luke

SKDevelopment
10-14-2009, 04:30 PM
This is because you need to escape $headline before using it in the query. I would also escape $type just in case. Please see mysql_real_escape_string() (http://php.net/mysql_real_escape_string).


$headline = mysql_real_escape_string($headline);
$type = mysql_real_escape_string($type);

LJackson
10-14-2009, 05:20 PM
ok that has got rid of the error but now its not displaying the value of $headline?

here is the updated code

<?php
require("functions.php");
include_once("db_info.php");

if(isset($_GET['type'])){
$type = $_GET['type'];
$type = strtolower($type);
}

if(isset($_GET['headline'])){
$headline = urldecode($_GET['headline']);
$headline = htmlspecialchars($headline);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Kyushindo to Mayo Shin Do Karate Judo Martial Arts in UK</title>
<link rel="stylesheet" type="text/css" href="page_styling.css" />
</head>

<body>
<?php header_info(); ?>

<div id="wrapper">

<div id="left">
<?php include("menu.php");?>
</div>

<div id="right">
<div class="pageinformation">
<div class="pageinformationHeader"><?php print $headline ?></div>
<div class="infocontainer">
<div class="pageinfomationIMG"><img src="testkarate.jpg" /></div>

<?php
$getnews = "SELECT * FROM ".mysql_real_escape_string($type)." WHERE headline = '".mysql_real_escape_string($headline)."'";
$sql = mysql_query($getnews) or die(mysql_error());
while($row = mysql_fetch_array($sql)){
print htmlspecialchars($row['content']);
}?>
</div>
</div>
</div>

<?php footer();?>
</div>
</body>
</html>

any ideas
Luke

EDIT
====

Ok got it working by removing

$headline = htmlspecialchars($headline);


thanks mate
Luke



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum