Edward Posted January 17, 2009 Share Posted January 17, 2009 Hi, I'm using a select query to find rows which have the 'from' field matching $_SESSION['id']. I know there are matches, as I have compared the two in a separate IF code, but no matches are coming up. Does anyone know why?? Here is the code: $sql = 'SELECT * FROM chat_messages WHERE \'from\' = "'.$_SESSION['id'].'" ORDER BY date ASC;'; $result = mysql_query($sql); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $chat_message_from = $row['from']; $chat_message_alias = $row['alias']; $chat_message_date = $row['date']; $chat_message = $row['message']; echo '<p>'; if (empty($chat_message_from)) { echo 'Guest 1'; } else { echo $chat_message_from; } echo ' on '.$chat_message_date.': '.$chat_message.'</p>'; } The above outputs no matches, however if I run the following, the word 'MATCH' outputs, which means the two values being compared above must be the same! $sql = 'SELECT * FROM chat_messages ORDER BY date ASC;'; $result = mysql_query($sql); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $chat_message_from = $row['from']; $chat_message_alias = $row['alias']; $chat_message_date = $row['date']; $chat_message = $row['message']; echo '<p>Session: '.$_SESSION['id'].'</p>'; echo '<p>From: '.$chat_message_from.'</p>'; if ($_SESSION['id'] == $chat_message_from) { echo '<p>MATCH!</p>'; } } Is it not possible to search for numeric values or something?! Quote Link to comment https://forums.phpfreaks.com/topic/141159-solved-select-query-not-finding-results-where-searching-for-number/ Share on other sites More sharing options...
trq Posted January 17, 2009 Share Posted January 17, 2009 Is it not possible to search for numeric values or something?! Of course its possible. The problem is the word from has special meening to sql. Id suggest changing the field name (recomended) or surrounding it with `backticks`. You really ought to wrap your function calls in some form of error handling as well. At its simplest..... $sql = "SELECT * FROM chat_messages WHERE `from` = '{$_SESSION['id']}' ORDER BY `date` ASC;"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $chat_message_from = $row['from']; $chat_message_alias = $row['alias']; $chat_message_date = $row['date']; $chat_message = $row['message']; echo '<p>'; if (empty($chat_message_from)) { echo 'Guest 1'; } else { echo $chat_message_from; } echo ' on '.$chat_message_date.': '.$chat_message.'</p>'; } } else { echo "No results found"; } } else { echo mysql_error(); } Quote Link to comment https://forums.phpfreaks.com/topic/141159-solved-select-query-not-finding-results-where-searching-for-number/#findComment-738849 Share on other sites More sharing options...
Edward Posted January 17, 2009 Author Share Posted January 17, 2009 Thanks Thorpe, I renamed the column and it's working fine. I knew it had to be possible but I had no idea why it wasn't working. I have got error handling in place but I stripped it out to find the problem, I'll put it back in now. Thanks very much, I've learned something today! Quote Link to comment https://forums.phpfreaks.com/topic/141159-solved-select-query-not-finding-results-where-searching-for-number/#findComment-738854 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.