Jump to content

why is this not working ????


shorty3

Recommended Posts

right this is from my database

Database: inbox

 

right i did this code:

<?php $totalmess2 = mysql_num_rows(mysql_query("SELECT read='0' FROM inbox")); ?>

 

then

<?php echo "$totalmess2"; ?>

 

ive got read in my inbox which just shows whether some one as read the message or not 1=read 0=unread

 

 

i want to know the total unread message there is in my inbox database whats going wrong

Link to comment
https://forums.phpfreaks.com/topic/211472-why-is-this-not-working/
Share on other sites

So many things wrong with this code.

 

Firstly, executing mysql_query() within mysql_num_rows() like that is ridiculous. it gives you absolutely no opportunity to check your query for success before attempting to use its result.

 

Speaking of which, your query is failing because its syntax is incorrect. You might try.....

 

SELECT id FROM inbox WHERE read = '0'

 

So, that would be....

 

if ($result = mysql_query("SELECT id FROM inbox WHERE read = '0'")) {
  echo mysql_num_rows($result);
}

 

Now, considering you only want a count and not the actual data, this would be even better.

 


if ($result = mysql_query("SELECT COUNT(id) AS cnt FROM inbox WHERE read = '0'")) {
  if (mysql_num_rows($result)) {
    $row = mysql_fetch_assoc($result);
    echo $row['cnt'];
  }
}

nope it didn`t work ?

 

Thats helpful. Try debugging it.

 

if ($result = mysql_query("SELECT COUNT(id) AS cnt FROM inbox WHERE read = '0'")) {
  if (mysql_num_rows($result)) {
    $row = mysql_fetch_assoc($result);
    echo $row['cnt'];
  } else {
    // no records found.
  }
} else {
  trigger_error(mysql_error());
}

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.