Forum Moderators: coopster
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/username/mysite-www/includes/pageFooter.inc on line 16
Please send any comments about these pages to russgri@mysite.com
Copyright © 1998-2002 - My Site All Rights Reserved -Webmaster
<?
if(isset($action)&&$action=="Add Comment"){
mysql_query("insert into comments (name,email,url,comment,page,datecreated)
values('$form_name','$form_email','$form_url','$form_comment','$PHP_SELF',now())");
mail("russgri@sportsmenafield.com","comment - $PHP_SELF","name : $form_name\nemail : $form_email\nurl : $form_url\n
comment:$form_comment","From: $form_email");
}
$q=mysql_query("select * from <comments where page='$PHP_SELF' order by datecreated desc"); if(mysql_num_rows($q)>0){ //Line 16
while($r=mysql_fetch_array($q)){
echo "On $r[datecreated], ";
if($r['email'])echo "<a href=\"mailto:$r[email]\">";
echo ($r['name'])?$r['name']:"someone anonymous";
if($r['email']){?></a><?}
if($r['url']){
$r['url']=ereg_replace("^http://","",$r['url']);
echo " (<a href=\"http://$r[url]\">$r[url]</a>)";
}
echo " said:<br />".nl2br(htmlspecialchars($r['comment']))."<hr />";
}
}
?>
I took this verbatim from a working page.
I need a little help with syntax.
$q = mysql_query("select * from <comments where page='$PHP_SELF' order by datecreated desc");
Whats that < doing in there?
Also you should really check whether the query was successful.
Try this..
$sql = "select * from comments where page='$PHP_SELF' order by datecreated desc";
$q = mysql_query($sql) or die('SQL Error: '.mysql_error());
or be a bit nicer to your users like this..
if ( $q = mysql_query($sql) ) {
...all the other stuff...
}
else {
print '<p>Sorry, I was not able to access the database properly. Please try again later.</p>';
}
Also I really think you ought to read this SitePoint article [sitepoint.com] before you make your script public.
Putting variables from a form straight into a query without checking them first is a bad idea.
$q=mysql_query("select * from comments where page='$PHP_SELF' order by datecreated desc");
if(mysql_num_rows($q)>0)else {
print '<p>Sorry, I was not able to access the database properly. Please try again later.</p>';
}
{
while($r=mysql_fetch_array($q)){
echo "On $r[datecreated], ";
if($r['email'])echo "<a href=\"mailto:$r[email]\">";
echo ($r['name'])?$r['name']:"someone anonymous";
if($r['email']){?></a><?}
if($r['url']){
$r['url']=ereg_replace("^http://","",$r['url']);
echo " (<a href=\"http://$r[url]\">$r[url]</a>)";
}
echo " said:<br />".nl2br(htmlspecialchars($r['comment']))."<hr />";
}
}
?>
$q=mysql_query("select * from comments where page='$PHP_SELF' order by datecreated desc");
if(mysql_num_rows($q)>0)
}else {
print '<p>Sorry, I was not able to access the database properly. Please try again later.</p>';
}
I still get this error:
Parse error: parse error in /home/username/mysite-www/includes/pageFooter.inc on line 17
...hum~mmmmm?
[pre]
if ( $q = mysql_query($sql) ) {
while( $r = mysql_fetch_array($q) ){
echo "On $r[datecreated], ";
if( $r['email'] )
echo "<a href=\"mailto:$r[email]\">";
echo ($r['name'])? $r['name'] : "someone anonymous";
if( $r['email'] )
echo '</a>';
if( $r['url'] ){
$r['url'] = ereg_replace("^http://","",$r['url']);
echo " (<a href=\"http://$r[url]\">$r[url]</a>";
}
echo " said:<br />".nl2br(htmlspecialchars($r['comment']))."<hr />";
}
}
else {
print '<p>Sorry, I was not able to access the database properly. Please try again later.</p>';
}
[/pre]
Also this line
if($r['email']){?></a><?} is wrong. It will always output a </a> tag. if ($r['email']) echo '</a>'; Also use the isset function to check if these variable are set
i.e. use
if ( isset($r['email']) ) {} if ( $r['email'] ) {}