Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. Problem with a query

Problem with a query

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
databasemysqlregexhelptutorial
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MacRaider4
    wrote on last edited by
    #1

    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!!!

    G L M A 4 Replies Last reply
    0
    • M MacRaider4

      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!!!

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M MacRaider4

        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!!!

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • M MacRaider4

          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!!!

          M Offline
          M Offline
          Mohibur Rashid
          wrote on last edited by
          #4

          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());

          1 Reply Last reply
          0
          • M MacRaider4

            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!!!

            A Offline
            A Offline
            arya1685
            wrote on last edited by
            #5

            $SQL = "SELECT * FROM empComm WHERE name = '".$empUserName."'";

            Is the correct Answer

            Thanks & Regards Arya1685 “You are not only responsible for what you say, but also for what you do not say”

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups