0

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?

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Ant
  • 1
  • 1
  • For posterity: I answered that the `execute` needed a `:`, and that seemed to help the OP, but @yourcommonsense mentioned that it can't be like that. Also see http://stackoverflow.com/questions/9778887/is-the-leading-colon-for-parameter-names-passed-to-pdostatementbindparam-opt . – Nanne Feb 21 '14 at 09:35

2 Answers2

3

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

function checkID($sid) {
                 ^

vs

$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.

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
-1

PHP PDO and mysql can not select id with letters

Actually they can.
Your code is all right.
Your question is not a real one as it caused by some typo or mistake in testing. Probably there were no data by the time you tested first time.
The other answer is wrong as PDO works both ways. Check it and see.

Actually, if PDO were unable to find a placeholder with that name, an error would be thrown.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • YCS is right, sorry about that. I was sure I had come across this and therefore new the answer, but it seems I'm wrong, my bad – Nanne Feb 21 '14 at 09:38