whats wrong with this query

Posted on 2008-11-03
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.


  $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

Question by:syedasimmeesaq
    LVL 16

    Expert Comment

    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."' ";
    LVL 9

    Author Comment

    That still does the same thing.. no records.
    LVL 16

    Expert Comment

    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 . "' ";;
    LVL 7

    Expert Comment

      $nuser = "Name= '$users' " ; //to quote the name.
    LVL 9

    Author Comment

    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.
    LVL 16

    Accepted Solution

    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 . "' ";
    LVL 9

    Author Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now