justAnoob Posted May 30, 2009 Share Posted May 30, 2009 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/ Share on other sites More sharing options...
Maq Posted May 30, 2009 Share Posted May 30, 2009 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'"; Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845394 Share on other sites More sharing options...
justAnoob Posted May 30, 2009 Author Share Posted May 30, 2009 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'"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845401 Share on other sites More sharing options...
roopurt18 Posted May 30, 2009 Share Posted May 30, 2009 Why are the simple things so difficult for me Programming is all in the details! Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845403 Share on other sites More sharing options...
Ken2k7 Posted May 30, 2009 Share Posted May 30, 2009 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845405 Share on other sites More sharing options...
justAnoob Posted May 30, 2009 Author Share Posted May 30, 2009 string(3) "NEW" string(0) "" is what is says.... I checked mysql and right now I have 2 messages in my table,, and one of them is marked as NEW Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845407 Share on other sites More sharing options...
Ken2k7 Posted May 30, 2009 Share Posted May 30, 2009 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(); Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845412 Share on other sites More sharing options...
justAnoob Posted May 30, 2009 Author Share Posted May 30, 2009 Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource on line 271 no results Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845435 Share on other sites More sharing options...
Ken2k7 Posted May 30, 2009 Share Posted May 30, 2009 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(); Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845444 Share on other sites More sharing options...
justAnoob Posted May 30, 2009 Author Share Posted May 30, 2009 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE status='NEW'' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845461 Share on other sites More sharing options...
Ken2k7 Posted May 30, 2009 Share Posted May 30, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845462 Share on other sites More sharing options...
justAnoob Posted May 30, 2009 Author Share Posted May 30, 2009 lol,, that is the change that I made also.. give me a couple minutes. Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845467 Share on other sites More sharing options...
justAnoob Posted May 30, 2009 Author Share Posted May 30, 2009 ////////////// 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845470 Share on other sites More sharing options...
justAnoob Posted May 30, 2009 Author Share Posted May 30, 2009 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845474 Share on other sites More sharing options...
Ken2k7 Posted May 30, 2009 Share Posted May 30, 2009 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(); Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845476 Share on other sites More sharing options...
fullyscintilla Posted May 30, 2009 Share Posted May 30, 2009 but is good programming habit to just always use them.. Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845477 Share on other sites More sharing options...
justAnoob Posted May 30, 2009 Author Share Posted May 30, 2009 string(65) "SELECT COUNT(*) as count FROM member_messages WHERE user_id = '0'" string(1) "0" Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845478 Share on other sites More sharing options...
justAnoob Posted May 30, 2009 Author Share Posted May 30, 2009 crap,, i messed up,, session id is the username not the id of the username Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845488 Share on other sites More sharing options...
justAnoob Posted May 30, 2009 Author Share Posted May 30, 2009 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845497 Share on other sites More sharing options...
Ken2k7 Posted May 30, 2009 Share Posted May 30, 2009 <?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"; } Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845500 Share on other sites More sharing options...
justAnoob Posted May 30, 2009 Author Share Posted May 30, 2009 did you get my last post where i said i messed up.. I'm working with 2 tables Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845506 Share on other sites More sharing options...
Ken2k7 Posted May 30, 2009 Share Posted May 30, 2009 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845512 Share on other sites More sharing options...
justAnoob Posted May 30, 2009 Author Share Posted May 30, 2009 Wow,, that is a new one for me,, never seen anything like that before. I'll give it a shot.. Could you explain what that does,, I mean I get the idea,, but what the characters mean (m) Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845517 Share on other sites More sharing options...
Ken2k7 Posted May 30, 2009 Share Posted May 30, 2009 m and mm are aliases. I could have used anything I want really, except a column name (I think) or a mysql reserved word. Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845520 Share on other sites More sharing options...
justAnoob Posted May 30, 2009 Author Share Posted May 30, 2009 Will you be on tomorrow?,, maybe so we can figure this out.. Need to put my kid to sleep. Quote Link to comment https://forums.phpfreaks.com/topic/160216-why-wont-this-work/#findComment-845523 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.