Link to home
Start Free TrialLog in
Avatar of syedasimmeesaq
syedasimmeesaqFlag for United States of America

asked on

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.
Thanks
if(isset($_SESSION['userb'])){
 
  
  $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

Avatar of brad2575
brad2575
Flag of United States of America image

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."' ";
 
Avatar of syedasimmeesaq

ASKER

That still does the same thing.. no records.
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 . "' ";;
 
Try
  $nuser = "Name= '$users' " ; //to quote the name.
 
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.
Thanks
ASKER CERTIFIED SOLUTION
Avatar of brad2575
brad2575
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 . "' ";;