Jump to content

Why won't this work.


justAnoob

Recommended Posts

The echo messages explains what I'm trying to do. Why are the simple things so difficult for me.... I think I over think them.

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
include "connection.php";
$krazycool = $_SESSION['id'];
$sql='SELECT id FROM member_messages WHERE user_id = "$krazycool" and status = "NEW"';
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count >= 1)
{
     echo "You have " . $count . " new messages.";
}
mysql_close();
?>

Link to comment
Share on other sites

Switch your single and double quotes around, your variable isn't being interpolated.

 

$sql="SELECT id FROM member_messages WHERE user_id = '$krazycool' and status ='NEW'";

Link to comment
Share on other sites

i echoed out both variables,,, $count and $krazycool.... my count keeps coming back as 0... I know that there is 1 new message in the inbox.... in mysql table the column called status keeps the record called "NEW" in it if the message is new... So what is going on?

<?php  // it is not reading the status column correctly for some reason
$sql="SELECT id FROM member_messages WHERE user_id = '$krazycool' and status = 'NEW'";
?>

Link to comment
Share on other sites

What would this output?

 

<?php
include "connection.php";
$sql = 'SELECT status FROM member_messages LIMIT 5;';
$results = mysql_query($sql);
while ($row = mysql_fetch_assoc($results)) {
     var_dump($row['status']);
     echo "\n";
}

Link to comment
Share on other sites

What about this?

<?php
session_start();
include "connection.php";
$krazycool = intval($_SESSION['id']);
$sql="SELECT COUNT(*) as count FROM member_messages WHERE user_id = '$krazycool' WHERE status='NEW'";
$result=mysql_query($sql);
if($row = mysql_fetch_assoc($result)) var_dump($row['count']);
else echo 'no results';
mysql_close();

Link to comment
Share on other sites

Uh... there must be a SQL error.

 

<?php
session_start();
include "connection.php";
$krazycool = intval($_SESSION['id']);
$sql="SELECT COUNT(*) as count FROM member_messages WHERE user_id = '$krazycool' WHERE status='NEW'";
$result=mysql_query($sql) or die(mysql_error());
if($row = mysql_fetch_assoc($result)) var_dump($row['count']);
else echo 'no results';
mysql_close();

Link to comment
Share on other sites

Ah I am stupid!

 

<?php
session_start();
include "connection.php";
$krazycool = intval($_SESSION['id']);
$sql="SELECT COUNT(*) as count FROM member_messages WHERE user_id = '$krazycool' AND status='NEW'";
$result=mysql_query($sql) or die(mysql_error());
if($row = mysql_fetch_assoc($result)) var_dump($row['count']);
else echo 'no results';
mysql_close();

 

Sorry justAnoob.

Link to comment
Share on other sites

////////////// this is what I get now    string(1) "0"

 

<?php
session_start();
include "connection.php";
$krazycool = intval($_SESSION['id']);
$sql="SELECT COUNT(*) as count FROM member_messages WHERE user_id = '$krazycool' AND status='NEW'";
$result=mysql_query($sql) or die(mysql_error());
if($row = mysql_fetch_assoc($result))
{
   var_dump($row['count']);
}
else
{
   echo 'no results';
}
mysql_close();

?>

Link to comment
Share on other sites

I put it back the way you had it and the result is the same from what I last posted... How does yours work without the curly brackets? I though you needed them.

<?php
session_start();
include "connection.php";
$krazycool = intval($_SESSION['id']);
$sql="SELECT COUNT(*) as count FROM member_messages WHERE user_id = '$krazycool' AND status='NEW'";
$result=mysql_query($sql) or die(mysql_error());
if($row = mysql_fetch_assoc($result)) var_dump($row['count']);  // no curly's around var_dump????
else echo 'no results';  //no curly brackets around the echo ???
mysql_close();
?>

Link to comment
Share on other sites

You don't need the curly braces if it's a one liner, which mine is. If you want to perform multiple lines of codes after them, then you need the curly braces. You also don't need the ending ?> if it's at the end of the script.

 

<?php
session_start();
include "connection.php";
$krazycool = intval($_SESSION['id']);
$sql="SELECT COUNT(*) as count FROM member_messages WHERE user_id = '$krazycool'";
var_dump($sql);
$result=mysql_query($sql) or die(mysql_error());
if($row = mysql_fetch_assoc($result)) var_dump($row['count']);
else echo 'no results';
mysql_close();

Link to comment
Share on other sites

Ok,,, this is what I got now,, and it keeps returning a "1",,, when I know that now I have "2" NEW messages.

<?php
session_start();
include "connection.php";
$krazycool = intval($_SESSION['id']);
$sql_1="SELECT id FROM members WHERE username = '$krazycool'";
$result_1=mysql_query($sql_1) or die(mysql_error());
$go_cavs = $result_1;

$sql="SELECT COUNT(*) as count FROM member_messages WHERE user_id = '$go_cavs'";
$result=mysql_query($sql) or die(mysql_error());
$count = mysql_num_rows($result);
if($count >= 1) 
{
   echo $count;
}
else
{ 
   echo "Not yet.";
}
mysql_close();
	?>

Link to comment
Share on other sites

<?php
session_start();
include "connection.php";
$id = intval($_SESSION['id']);
$sql="SELECT COUNT(*) AS count FROM members WHERE username = '$id' AND status = 'NEW'";
$result=mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_row($result)) {
     echo $row['count'] . "\n";
}

Link to comment
Share on other sites

Oooh oops!

 

<?php
session_start();
include "connection.php";
$id = intval($_SESSION['id']);
$sql="SELECT COUNT(*) AS count FROM members m INNER JOIN member_messages mm ON (m.id = mm.user_id AND mm.status = 'NEW') WHERE m.username = '$id' LIMIT 1;";
$result=mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_row($result)) {
     echo $row['count'] . "\n";
}

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.