Jump to content

[SOLVED] Select query not finding results where searching for number?


Recommended Posts

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?!

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();
}

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!

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.