kingb00zer Posted December 5, 2011 Share Posted December 5, 2011 hey, I am trying to add messaging into a site I am building and so far I have a table in the database with the fields date, to, from, message, and category. What I am having trouble with is figuring out how to select everything where to is equal to $_SESSION['username'] but also only where category is equal to personal. what should I do? Link to comment Share on other sites More sharing options...
thescientist Posted December 5, 2011 Share Posted December 5, 2011 what do you have so far? what part of you having trouble with? It seems like it would be a simple SELECT query with a WHERE clause for the category. Link to comment Share on other sites More sharing options...
kingb00zer Posted December 6, 2011 Author Share Posted December 6, 2011 The where clause is what i use to select all from messages where the field "to" is equal to $_SESSION['username']But i cant work out how to refine that search so that on the inbox page i only get messages from where category= "personal" and where to= 'my username'. Link to comment Share on other sites More sharing options...
thescientist Posted December 6, 2011 Share Posted December 6, 2011 if category is a field in the same table, then as per my suggestion, SELECT and use the WHERE clause for both of them. If that field is in a different category, then you will probably need a join, and most likely a trip over to the SQL forum. Link to comment Share on other sites More sharing options...
kingb00zer Posted December 8, 2011 Author Share Posted December 8, 2011 OMG lol I didnt even need to post this problem. I forgot that i had read about sql operpators for and and or and assumed that I remembered them from reading php tutorials (hense this question in the php forum.) now that I reallise "AND" can be used I now understand how to tackle my problem. Thank you Link to comment Share on other sites More sharing options...
kingb00zer Posted December 8, 2011 Author Share Posted December 8, 2011 OMG lol I didnt even need to post this problem. I forgot that i had read about sql operpators for and and or and assumed that I remembered them from reading php tutorials (hense this question in the php forum.) now that I reallise "AND" can be used I now understand how to tackle my problem. Thank you ok after attempting to write this script I reallise it wasnt as straight forward as I thought before. Ok so far I ahve this code echo "<table class='centrepage' width='600px' align='center'>";echo "<tr><td class='seven' align='center' colspan='8'> Inbox - Sent - Alliance - Message Board - Attacks In - Attacks Out <br /></td></tr>"; $count= 0 ;$query = mysql_query("SELECT * FROM messages WHERE to='" . $_SESSION['username'] . "' AND WHERE category='personal' ");$rowcount= mysql_num_rows($query); // THIS IS ROW 95while ($count <= $rowcount){while($row = mysql_fetch_array($query)) { // THIS IS ROW 99$from= $row['from'];$message= $row['message'];$date= $row['date'];}echo "<tr bgcolor='#006622'><td colspan='8' class='seven'><b>From: $from</b> $date</td></tr>";echo "<tr><td colspan='8' class='seven'>$message</td></tr>";$count++;}echo "</table>"; EDIT: (forgot to add the result) Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in F:\Ashs stuff\Web Dev Stuff\School ######\xampp\htdocs\dealer game\messages.php on line 95 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in F:\Ashs stuff\Web Dev Stuff\School ######\xampp\htdocs\dealer game\messages.php on line 99 So basically in this table I want it to generate 2 rows on each loop, the top one to say who the message is form and at what time and date it arrived, then in the next row display the message. The table in the database this information is being retrieved from may contain many diferent messages for the user so it is important that the messages not only match the users name in the "to" field but the category fields needs to match to the word "personal" also. Any idea what to do here? Link to comment Share on other sites More sharing options...
thescientist Posted December 8, 2011 Share Posted December 8, 2011 are you selecting and connecting to a database? the errors are trying to tell you that there is problem with the return value of mysql_query. Link to comment Share on other sites More sharing options...
kingb00zer Posted December 8, 2011 Author Share Posted December 8, 2011 Yeah I have a connection file I included into the script. From what I can gather this script is giving me 0 rows that match the crtiera even though there are 3 personal messages sent to my account (visible when i observe the database) which is telling me the query on line 94 is somehow incorrect. I have not yet tried to use AND in sql or multiple WHERE clauses in the same statement until now so you think that might be the problem? Link to comment Share on other sites More sharing options...
justsomeguy Posted December 8, 2011 Share Posted December 8, 2011 A SQL statement can only have 1 WHERE clause. Check the examples in the SQL tutorial. Each condition does not have its own WHERE clause, they are all part of the same clause. Link to comment Share on other sites More sharing options...
thescientist Posted December 8, 2011 Share Posted December 8, 2011 doh! yes, you need to use AND. silly me. Link to comment Share on other sites More sharing options...
kingb00zer Posted December 9, 2011 Author Share Posted December 9, 2011 hmmmm... the AND is already in there and I removed the 2nd where and I am still getting the same error. This is what my code is now. echo "<table class='centrepage' width='600px' align='center'>";echo "<tr><td class='seven' align='center' colspan='8'> Inbox - Sent - Alliance - Message Board - Attacks In - Attacks Out <br /></td></tr>"; $count= 0 ;$query = mysql_query("SELECT * FROM messages WHERE to='" . $_SESSION['username'] . "' AND category='personal' ");$rowcount= mysql_num_rows($query); // LINE 95while ($count <= $rowcount){while($row = mysql_fetch_array($query2)) {$from= $row['from'];$message= $row['message'];$date= $row['date'];}echo "<tr bgcolor='#006622'><td colspan='8' class='seven'><b>From: $from</b> $date</td></tr>";echo "<tr><td colspan='8' class='seven'>$message</td></tr>"; $count++;} Link to comment Share on other sites More sharing options...
kingb00zer Posted December 9, 2011 Author Share Posted December 9, 2011 Nevermind guys, I spoke with a friend and it turns out that i cant use the word "to" for a field name asit is a reserved word in sql. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.