Forum Moderators: coopster

Message Too Old, No Replies

MySql/Php comment at bottom of each page (blog)

Warning on line 16

         

russgri

10:40 am on Apr 25, 2003 (gmt 0)

10+ Year Member




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 &lt;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.

grahamstewart

11:06 am on Apr 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd say that this is your problem..
$q = mysql_query("select * from &lt;comments where page='$PHP_SELF' order by datecreated desc"); 

Whats that &lt; 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.

russgri

5:44 pm on Apr 25, 2003 (gmt 0)

10+ Year Member



I inserted you 'be nice' snippet
Is it correct?


$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 />";
}
}
?>

russgri

6:46 pm on Apr 25, 2003 (gmt 0)

10+ Year Member



...Modifying the else statement:


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

RussellC

9:15 pm on Apr 25, 2003 (gmt 0)

10+ Year Member



Dont you need another bracket in there?

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

grahamstewart

3:08 am on Apr 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No no.. when I said "...all the other stuff..." I meant do this..

[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.
What you want to do is
if ($r['email']) echo '</a>';

Also use the isset function to check if these variable are set
i.e. use

if ( isset($r['email']) ) {}

instead of
if ( $r['email'] ) {}