Jump to content

select only rows where certain field is empty


nadz

Recommended Posts

Basically i need to select the rows from a table where the user is "$curr_user" and the "seen" field is empty. Then i want to count the rows and use the count in a javascript alert. Heres what i got so far:

 

<?php
$result = mysql_query($query = "SELECT * FROM user WHERE user=''.$curr_user.'' AND seen=''") or trigger_error(mysql_error()."<PRE>".$query."</PRE>", E_USER_ERROR);

$num = mysql_num_rows($result)

if($num > 0)
{
<script language="Javascript">

alert ("You have $num new messages")

</script>
}

?>

 

can anyone see whats wrong? any help appreciated

No offense, but that code is full of errors. At the very least you should have received some type of error on screen. You should always post the error messages in these situations.

 

Try this:

<?php

$query = "SELECT count(*) as count FROM user WHERE user='".$curr_user."' AND seen='' GROUP BY user";

$result = mysql_query($query) or trigger_error(mysql_error()."<PRE>".$query."</PRE>", E_USER_ERROR);

if(mysql_num_rows($result)) {

  $record = mysql_fetch_assoc($result);

  echo "<script language=\"Javascript\">\n";
  echo "alert ('You have $num new messages');\n";
  echo "</script>\n";
}

?>

<?php

 

$query = "SELECT count(*) as count FROM user WHERE user='".$curr_user."' AND seen='' GROUP BY user";

 

$result = mysql_query($query) or trigger_error(mysql_error()."<PRE>".$query."</PRE>", E_USER_ERROR);

 

if(mysql_num_rows($result)) {

 

  $record = mysql_fetch_assoc($result);

 

  echo "<script language=\"Javascript\">\n";

  echo "alert ('You have $num new messages');\n";

  echo "</script>\n";

}

 

?>

This code was good but there was no initialization for the variable $num

So try this:

 

<?php

 

$query = "SELECT count(*) as count FROM user WHERE user='".$curr_user."' AND seen='' GROUP BY user";

 

$result = mysql_query($query) or trigger_error(mysql_error()."<PRE>".$query."</PRE>", E_USER_ERROR);

$num=mysql_num_rows($result);

if($num)) {

 

  $record = mysql_fetch_assoc($result);

 

  echo "<script language=\"Javascript\">\n";

  echo "alert ('You have $num new messages');\n";

  echo "</script>\n";

}

 

?>

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.