3

PHP PDO and mysql can not select the ID with letters

 2 years ago
source link: https://www.codesd.com/item/php-pdo-and-mysql-can-not-select-the-id-with-letters.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

PHP PDO and mysql can not select the ID with letters

advertisements

in phpMyAdmin the query

SELECT id
FROM `column`
WHERE `id` = "RM3zc7e8"
LIMIT 1

works as intended finding a result. However when using PDO in a function

function checkID($sid) {
  try {
    $conn = new PDO('mysql:host=' . HOST . ';dbname=' . DATABASE, USER, PASSWORD);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   

    $stmt = $conn->prepare("SELECT id FROM column WHERE id = :sID");
    $stmt->execute(array('sID' => $sID ));
    $result = $stmt->fetch();

  } catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
  }

  return $result;
}

I can't get it to return anything. Any thoughts?


It is not working because you have a wrong variable name. Compare the following

function checkID($sid) {
                 ^

$stmt->execute(array('sID' => $sID ));
                              ^

There is no variable named $sID in your function and hence no value is bound to your statement. Make it $sid.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK