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
https://forums.phpfreaks.com/topic/160216-why-wont-this-work/
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
https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845401
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
https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845412
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
https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845444
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
https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845462
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
https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845470
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
https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845474
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
https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845476
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
https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845497
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
https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845500
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
https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845512
Share on other sites

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.