Question

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?

Was it helpful?

Solution

Based on the comments, give this a try:

$getidnum = $db->prepare("SELECT USERID FROM users WHERE fullname LIKE :engineer");
$getidnum->execute(array(':engineer' => $engineer));

OTHER TIPS

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

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top