Jump to content

can get else statment to work


contra10

Recommended Posts

im trying to get my else statement to work if there is nothing in the query

 

<?php
 mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("registration") or die(mysql_error());

$queryi = "SELECT * FROM `users` WHERE `username` = '$usernamec'";
$resulti = mysql_query($queryi);
if($comcount = mysql_num_rows($resulti)){
while($rowid = mysql_fetch_assoc($resulti))
{
$com= "{$rowid['comment']}";
$cname= "{$rowid['cusername']}";
echo"$cname thinks $com";
}
}else{
echo "no comments have been made";
}

?>

theres nothing in my query so the second line should show but it shouws the first echo without the variables present

 

Link to comment
https://forums.phpfreaks.com/topic/146443-can-get-else-statment-to-work/
Share on other sites

my statment shows but i get error with this

 

<?php	
 mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("registration") or die(mysql_error());

$queryi = "SELECT * FROM `users_should_comment` WHERE `username` = '$usernamec'";
$resulti = mysql_query($queryi);
if($comcount = mysql_num_rows($resulti)){
while($rowid = mysql_fetch_assoc($resulti))
{
$com= "{$rowid['comment']}";
$cname= "{$rowid['cusername']}";
echo"$cname thinks that $username should $com";
}
}else{
echo "no comments have been made to this user";
}

?>

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\profile\index.php on line 205

When you get that error it means your query has a problem. Rewrite your code to be something like this:

<?php
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("registration") or die(mysql_error());
$queryi = "SELECT * FROM `users_should_comment` WHERE `username` = '$usernamec'";
$resulti = mysql_query($queryi) or die("Problem with the query: $queryi<br>" . mysql_error());
$comcount = mysql_num_rows($resulti);
if ($comcount > 0)
   {
while($rowid = mysql_fetch_assoc($resulti))
{
                 $com= $rowid['comment'];
                 $cname= $rowid['cusername'];
                 echo "$cname thinks that $username should $com";
}
   }else{
echo "no comments have been made to this user";
}
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.