0

Here is the query:

$query = "SELECT name,deleted,COUNT(message) as message FROM guestbook_message WHERE name='".$name."' AND deleted=1 GROUP BY name";  
$result = mysql_query($query) or die(mysql_error());
$deletedtotal_row = mysql_fetch_array($result);

Here when I use it:

echo "You have had ".($deletedtotal_row['deleted']) ? $deletedtotal_row['deleted'] : '0'." messages deleted";

This errors, doesn't show an syntax error, but doesn't display any results.

But when I use:

echo ".$totaldeleted_row['deleted'].";

It works fine. But if there is no messages deleted for that user, it returns nothing, but I want it to display '0'.

Any ideas where I'm going wrong?

5 Answers 5

1
echo "You have had ".intval($deletedtotal_row['deleted'])." messages deleted";
Sign up to request clarification or add additional context in comments.

1 Comment

Not sure why someone downvoted this, but we do not have to correct typo's but suggest better code.
0

change this:

echo "You have had ".($deletedtotal_row['deleted'] ? $deletedtotal_row['deleted'] : '0')." messages deleted";

Also, this echo ".$totaldeleted_row['deleted']."; is very bad. Do this instead:

echo $totaldeleted_row['deleted'];

10 Comments

It's not very bad. It may not be optimal. It may not be great to read. But it's by no means very bad (especially related to your alternative)...
Thanks for your answer. Is using name=".$name." ok to use in the query?
It seems to throw all my divs out of sync when I use that. It's fine until I try to display the '0' result. The divs are also inside the php.
@Tom, Depends where $name is coming from. If it's correct sanitized then yes. But you should really be using prepared statements rather than plain old xml. Look at the mysqli or pdo libraries. They're packaged with PHP 5.
@ircmaxell, my alternative is the correct an only syntax that wont return any kind of error/notice/warning. For this to be anywhere near correct usage it would have to be "{$totaldeleted_row['deleted']}". As it is it would output .x. and return a warning or notice I believe.
|
0
if(mysql_num_rows($result)==0) {
   echo 'no rows';
} else {
   echo "You have had " . $deletedtotal_row['deleted'] ." messages deleted";
}

1 Comment

You can just set a var and then use that in your echo - but I think using intval() is probably a better solution.
0

Thanks everybody. I got it working with:

echo "You have had ".($deletedtotal_row['deleted'] ? $deletedtotal_row['deleted'] : '0')." messages deleted";

1 Comment

If you use someone's answer you should acknowledge it by accepting their answer. It helps your acceptance rating, which inturn makes people more inclined to help you. It's also the foundation of the stack exchange network.
0

use:

sprintf("You have had %d messages deleted", $deletedtotal_row['deleted');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.