[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

whats wrong with this query

Posted on 2008-11-03
7
Medium Priority
?
233 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:syedasimmeesaq
  • 3
  • 3
7 Comments
 
LVL 16

Expert Comment

by:brad2575
ID: 22868529
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."' ";
 
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22868553
That still does the same thing.. no records.
0
 
LVL 16

Expert Comment

by:brad2575
ID: 22868613
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 . "' ";;
 
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 7

Expert Comment

by:ahalya
ID: 22868640
Try
  $nuser = "Name= '$users' " ; //to quote the name.
 
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22868813
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
0
 
LVL 16

Accepted Solution

by:
brad2575 earned 2000 total points
ID: 22868896
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 . "' ";
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22868912
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 . "' ";;
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question