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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.