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

Link to comment
Share on other sites

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";
}

?>

Link to comment
Share on other sites

<?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";

}

 

?>

Link to comment
Share on other sites

hi, thanks for the help guys. im getting this error with the code qbove me. any ideas?

 

Parse error: syntax error, unexpected ')' in /home/.loekie/nexman/D******.co.uk/file/templates_c/%%112%%1125780542_footer.html.php on line 73

Link to comment
Share on other sites

$query3="SELECT * FROM np1 WHERE Clinton='';";

 

ok, in the empty fields of your table, is null written it. if not, than remove the word null from above...it will look like this :

 

$query3="SELECT * FROM np1 WHERE Clinton=' ';";

Link to comment
Share on other sites

$query3="SELECT * FROM np1 WHERE Clinton='';";

 

ok, in the empty fields of your table, is null written it. if not, than remove the word null from above...it will look like this :

$query3="SELECT * FROM np1 WHERE Clinton=' ';";

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.