0

I know this is a really basic question but I am completely at my wits end. I have been working on this for two days. The if statement always returns false. I have tried === and == and copy to an $array and process outside the fetch $result loop. I have compared to a direct "name". I have outputed the ascii value of each letter of the returned string and compared that way to make sure I wasn't putting a space or something in somewhere. I've tried mysql and mysqli ways. I've tried OO style and Procedural style but here's the rub, This code was copy and pasted from my own site where it's working just fine in three other programs in my site.

<?php
SESSION_START();
if(!isset($_SESSION["uname"])){
require ('redirect.html');
die();
}// session is set close
$uname = $_SESSION["uname"];

$user_name = "xxxxxx";
$password = "xxxxxxx";
$database = "usersdata";
$server = "xxxxxxxxxx.ipagemysql.com";


$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

$SQL = "SELECT * FROM messages";
$result = mysql_query($SQL);

while ( $db_field = mysql_fetch_assoc($result) ) {

//echo out values for visual comparison
echo $db_field['recipient']." ".$uname." ";

if($db_field['recipient'] === $uname){echo " match ";} else {echo " no match ";}

}// while loop db_field close
}// if db_found loop close

mysql_close($db_handle);

?>

As I stated this code works just fine in three other programs running on this very site. All I did was copy paste and change the db and fields. I even tried retyping it all from scratch just to make sure. Help me I'm melting....

10
  • What you get in echo $db_field['recipient']." ".$uname." ";? Commented Jul 24, 2014 at 4:58
  • SESSION_START(); <= that must be in lowercase session_start(); Commented Jul 24, 2014 at 5:01
  • Nope @Fred-ii- that doesn't matter. PHP function names are case-insensitive. Commented Jul 24, 2014 at 5:01
  • @Hanky웃Panky I stand corrected. One never ceases to learn ;-) Commented Jul 24, 2014 at 5:03
  • var_dump($_SESSION["uname"]); or var_dump($uname); see what's being passed through. My guess, it'll be empty. Commented Jul 24, 2014 at 5:06

1 Answer 1

2

Why would you return all the messages and then compare with every single message if the name matches. You can simply add a where clause to your query

SELECT * FROM messages WHERE recipient='uname value here'

Then you can check for the number of rows returned. If 0 rows are returned, there was no match.

Sign up to request clarification or add additional context in comments.

7 Comments

The WHERE clause makes a lot of sense, since OP has nothing (column) to match up against.
Yeah that code is like buying 10000 apples just to come home and pick up a one which is 125.10 grams exactly. Why not tell that specification to the shopkeeper and buy only that one.
Got that right. Plus, all it takes is one bad apple to ruin it all ;-)
This is the striped down code so I can get to the basic operations. Whenever I hit a problem I can't solve I strip it till I find the cause.
I think I just found it. I just typed a message into the database through my php admin and it came up true. So it's got to be something in the insert step.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.