简体   繁体   中英

MYSQL PHP PDO Select Statement calling null

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:

在此处输入图片说明

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

Based on the comments, give this a try:

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM