whats wrong with this query

I have this query and I want to look at the Name field and get only the result where name field matches the name in the session
the session is $_Session['userb'] and gives me names like John Devon etc

the problem is that even though I have John Devon in my database in the Name field, it doesn't show me any records and it says 0 records.
I think I am doing something wrong in this query and may be it is not taking the name string correctly
Anyhelp will be appreciated.
  $users = $_SESSION['userb'];
 $nuser = "Name= ".$users;
 $sql = "SELECT ID, `Name`, `Phone`, `Email`, `ipaddress`, `completed`, `City`, `Training`, `Date`,  FROM `Table1` where '".$nuser."' ";
  if (isset($order) && $order!='') $sql .= " order by `" .sqlstr($order) ."`";
    if (isset($ordtype) && $ordtype!='') $sql .= " " .sqlstr($ordtype);
  $res = mysql_query($sql, $conn) or die("error users " .mysql_error());
  return $res;  

Open in new window

Who is Participating?
brad2575Connect With a Mentor Commented:
maybe it is NOT NULL but blank, you can try this:

"SELECT ID, `Name`, `Phone`, `Email`, `ipaddress`, `completed`, `City`, `Training`, `Date`,  FROM `Table1` where `Phone` is not null AND LTRIM(`Phone`) <> '' AND `Name` = '". $users . "' ";
you need to change this:

 $sql = "SELECT ID, `Name`, `Phone`, `Email`, `ipaddress`, `completed`, `City`, `Training`, `Date`,  FROM `Table1` where '".$nuser."' ";

to this:

 $sql = "SELECT ID, `Name`, `Phone`, `Email`, `ipaddress`, `completed`, `City`, `Training`, `Date`,  FROM `Table1` where 'Name' = '".$nuser."' ";
syedasimmeesaqAuthor Commented:
That still does the same thing.. no records.
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

sorry my fault I did not see you were setting the variable to include the field name.

try this:

$sql = "SELECT ID, `Name`, `Phone`, `Email`, `ipaddress`, `completed`, `City`, `Training`, `Date`,  FROM `Table1` where `Name` = '". $users . "' ";;
  $nuser = "Name= '$users' " ; //to quote the name.
syedasimmeesaqAuthor Commented:
oh sorry guys the problem was in other query. which was

$sql = "SELECT COUNT(*) FROM `tatracking_survey`  Name = '".$users."' ";
and I was missing where
$sql = "SELECT COUNT(*) FROM `tatracking_survey` where Name = '".$users."' ";

however now I am getting the records but I am also getting the rows where Phone field is empty. How can I rewrite the above query so it doesn't show any records where Phone field is null. I tried
$sql = "SELECT ID, `Name`, `Phone`, `Email`, `ipaddress`, `completed`, `City`, `Training`, `Date`,  FROM `Table1` where `Phone` is not null AND `Name` = '". $users . "' ";
but that didn't work.
syedasimmeesaqAuthor Commented:
thanks. In addition to that I also had to but that in here too
$sql = "SELECT ID, `Name`, `Phone`, `Email`, `ipaddress`, `completed`, `City`, `Training`, `Date`,  FROM `Table1` where `phone` <> '' AND `Name` = '". $users . "' ";;
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.