Problem with a query
-
Ok, I give up... If I use the commented out SQL I get nothing, however if I use the SELECT * the "match" is hit. I'm running out of ideas. I've tried removing the ' around the variable, I added periods around it and still nothing. If I just type the value in I'm looking for 'John Smith' for example it finds it, just not when I use the variable...
echo "
myusername should = " . $empUserName . "
";
//$SQL = "SELECT * FROM empComm WHERE name = '$empUserName'";
$SQL = "SELECT * FROM empComm";
$result = mysql_query($SQL);
echo "Connected";
while ($db_field = mysql_fetch_assoc($result)) {
//echo "TEST";
if($db_field['name'] = $empUserName){echo "match
";}
print $db_field['ecID'] . "
";
print $db_field['name'] . "
";
print $db_field['password'] . "
";
//print $db_field['Address'] . "
";
}That first line is correct it displays myusername should = John Smith The table only has 3 fields, ecID, name, password and really I only want to get the ecID. Well actually I was doing a join to make life easier but I wasn't getting anything so I just created this simple query and still nothing. I'm sure I'm forgetting something simple. Thanks in advance!!!
-
Ok, I give up... If I use the commented out SQL I get nothing, however if I use the SELECT * the "match" is hit. I'm running out of ideas. I've tried removing the ' around the variable, I added periods around it and still nothing. If I just type the value in I'm looking for 'John Smith' for example it finds it, just not when I use the variable...
echo "
myusername should = " . $empUserName . "
";
//$SQL = "SELECT * FROM empComm WHERE name = '$empUserName'";
$SQL = "SELECT * FROM empComm";
$result = mysql_query($SQL);
echo "Connected";
while ($db_field = mysql_fetch_assoc($result)) {
//echo "TEST";
if($db_field['name'] = $empUserName){echo "match
";}
print $db_field['ecID'] . "
";
print $db_field['name'] . "
";
print $db_field['password'] . "
";
//print $db_field['Address'] . "
";
}That first line is correct it displays myusername should = John Smith The table only has 3 fields, ecID, name, password and really I only want to get the ecID. Well actually I was doing a join to make life easier but I wasn't getting anything so I just created this simple query and still nothing. I'm sure I'm forgetting something simple. Thanks in advance!!!
Check for leading/trailing spaces in the variable and in the database name column. Your
if($db_field ...
test is confusing things because you should be using==
to test the field - by using=
you are changing the value you get from the database to the$empUserName
value, which you then print out. -
Ok, I give up... If I use the commented out SQL I get nothing, however if I use the SELECT * the "match" is hit. I'm running out of ideas. I've tried removing the ' around the variable, I added periods around it and still nothing. If I just type the value in I'm looking for 'John Smith' for example it finds it, just not when I use the variable...
echo "
myusername should = " . $empUserName . "
";
//$SQL = "SELECT * FROM empComm WHERE name = '$empUserName'";
$SQL = "SELECT * FROM empComm";
$result = mysql_query($SQL);
echo "Connected";
while ($db_field = mysql_fetch_assoc($result)) {
//echo "TEST";
if($db_field['name'] = $empUserName){echo "match
";}
print $db_field['ecID'] . "
";
print $db_field['name'] . "
";
print $db_field['password'] . "
";
//print $db_field['Address'] . "
";
}That first line is correct it displays myusername should = John Smith The table only has 3 fields, ecID, name, password and really I only want to get the ecID. Well actually I was doing a join to make life easier but I wasn't getting anything so I just created this simple query and still nothing. I'm sure I'm forgetting something simple. Thanks in advance!!!
I would write it like so:
$SQL = "SELECT * FROM empComm WHERE name = '".$empUserName."'";
and make sure no spaces sneak in. If that doesn't work, I'd seriously doubt your database content, so print out empComm.name (both string value and string length) for a visual check. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum iSad
-
Ok, I give up... If I use the commented out SQL I get nothing, however if I use the SELECT * the "match" is hit. I'm running out of ideas. I've tried removing the ' around the variable, I added periods around it and still nothing. If I just type the value in I'm looking for 'John Smith' for example it finds it, just not when I use the variable...
echo "
myusername should = " . $empUserName . "
";
//$SQL = "SELECT * FROM empComm WHERE name = '$empUserName'";
$SQL = "SELECT * FROM empComm";
$result = mysql_query($SQL);
echo "Connected";
while ($db_field = mysql_fetch_assoc($result)) {
//echo "TEST";
if($db_field['name'] = $empUserName){echo "match
";}
print $db_field['ecID'] . "
";
print $db_field['name'] . "
";
print $db_field['password'] . "
";
//print $db_field['Address'] . "
";
}That first line is correct it displays myusername should = John Smith The table only has 3 fields, ecID, name, password and really I only want to get the ecID. Well actually I was doing a join to make life easier but I wasn't getting anything so I just created this simple query and still nothing. I'm sure I'm forgetting something simple. Thanks in advance!!!
Dude, You have made a classic mistake. Very funny. It is
if($db_field['name'] = $empUserName){echo "match
";}and it should be
if($db_field['name'] == $empUserName){echo "match
";}few more things, make sure your Query execution is successfull. so the line should be
$result = mysql_query($SQL) or die("Error In Query :".$SQL."
".mysql_error()); -
Ok, I give up... If I use the commented out SQL I get nothing, however if I use the SELECT * the "match" is hit. I'm running out of ideas. I've tried removing the ' around the variable, I added periods around it and still nothing. If I just type the value in I'm looking for 'John Smith' for example it finds it, just not when I use the variable...
echo "
myusername should = " . $empUserName . "
";
//$SQL = "SELECT * FROM empComm WHERE name = '$empUserName'";
$SQL = "SELECT * FROM empComm";
$result = mysql_query($SQL);
echo "Connected";
while ($db_field = mysql_fetch_assoc($result)) {
//echo "TEST";
if($db_field['name'] = $empUserName){echo "match
";}
print $db_field['ecID'] . "
";
print $db_field['name'] . "
";
print $db_field['password'] . "
";
//print $db_field['Address'] . "
";
}That first line is correct it displays myusername should = John Smith The table only has 3 fields, ecID, name, password and really I only want to get the ecID. Well actually I was doing a join to make life easier but I wasn't getting anything so I just created this simple query and still nothing. I'm sure I'm forgetting something simple. Thanks in advance!!!