0

I'm trying to get the user's ID number from my table. For some reason the value always comes up "NULL", and it shouldn't be but I can't figure out what I'm doing wrong here.

Here is how my table 'users' looks:

enter image description here

<?php
  ...........................................
  ....connection details (connection is not the problem as other SQL queries in my code work fine)
  ...........................................
  ...........................................

  $getvals = $db->prepare("SELECT MFG_LINE, PM_MECHANICAL, PM_DESIGN, PM_APPLICATIONS, PM_PROGRAM, DESCRIPTION FROM new_schedule WHERE ITEM = '$jobnum'");
    $getvals->execute();
    while ($row = $getvals->fetch(PDO::FETCH_ASSOC)){
        $prod_line=$row["MFG_LINE"];
        $pe=$row["PM_MECHANICAL"];
        $de=$row["PM_DESIGN"];
        $ae=$row["PM_APPLICATIONS"];
        $ce=$row["PM_PROGRAM"];
        $model=$row["DESCRIPTION"];
    }



  /*Is job PE, DE, or CE?*/
    $engtype = rand(1,3);

    if ($engtype===1) { $engineer = $pe; }
    else if ($engtype===2) { $engineer = $de; }
    else if ($engtype===3) { $engineer = $ce; }
    else { $engineer = "Error 1005"; }

    echo $engineer;

    if ($engineer == null || $engineer = "") {
        $theengineer = 0;
        echo "nope";
    } else {        
        $getidnum = $db->prepare("SELECT USERID FROM users WHERE fullname LIKE '$engineer'");
        $getidnum->execute();
        $getthenum = $getidnum->fetch(PDO::FETCH_ASSOC);
        $theengineer = $getthenum['USERID'];
    }      

?>

The value is returning a NULL value when it should be returning "12". What am I missing here?

4
  • var_dump($getthenum) what do you get? Commented May 1, 2014 at 19:16
  • If you run SELECT USERID FROM users WHERE fullname LIKE 'BRAD DAVIS' from a MySQL client, whats the output? Commented May 1, 2014 at 19:17
  • hmm...when I do it that way it works. So I guess I need to update how I actually get my $engineer value Commented May 1, 2014 at 19:21
  • Yes indeed. You should post the code that you actually used, not code that is mostly the same as your actual code, but modified a little bit before you post it. That's like having a problem with your car which is making a funny noise, and taking it to the mechanic, but instead of bring the problem car, you bring one similar to the problem car. Commented May 1, 2014 at 19:25

3 Answers 3

1

Based on the comments, give this a try:

$getidnum = $db->prepare("SELECT USERID FROM users WHERE fullname LIKE :engineer");
$getidnum->execute(array(':engineer' => $engineer));
Sign up to request clarification or add additional context in comments.

6 Comments

how do i retrieve the value from the array?
You define the value before the query, just as you're currently doing.
Then what are you not showing us? If the variable $engineer contains only "BRAD DAVIS" and nothing else, then there is nothing wrong with the query.
I added more code (see above). I'm pulling engineer names from another jobs table. Then choosing a random position ($de,$pe,$ce). The engineer will be the random chosen one. All names are in the user database. I echo the engineer's name to make sure it is pulling a name and it always does.
Ok...its stupid...but if you look at the "if" statement above the SQL call, I only had one "=" in there which was tripping it up yet not giving me the error.
|
0

In your code, the line that is
$getidnum = $db->prepare("SELECT USERID FROM users WHERE fullname LIKE '$engineer'");.
Change that to
$getidnum = $db->prepare("SELECT USERID FROM users WHERE fullname LIKE $engineer");

2 Comments

I still get a null value.
Could you also echo the resulting SQL query.
0

It maybe that there is spaces where not expected, so your query fails.

You need to add a wildcard front and end and the middle of the fullname. So replace the space, or spaces (could be several, never trust user input), in the middle with "%". So you have this:

$engineer = "%BRAD%DAVIS%";

Try that and let me know if it works?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.