问题
I am new to PDO and I was wondering if there was an equivalent to a mysql statement that checks if a parameter is not null such as:
SELECT * FROM table
WHERE param IS NOT NULL
I tried this:
$pdo->prepare('SELECT * FROM ' . $tablename . ' WHERE ' . $field . ' = :' . $field . 'AND param IS NOT NULL');
without any success. I also looked on the web, but did not find anything relevan, can anyone help please ?
回答1:
You can use any query in pdo that you could use in mysql directly but your way of doing it is defeating the purpose of using PDO in the first place, you should phrase the query as
$q = $pdo->prepare("SELECT * FROM $tablename WHERE field = :field AND param IS NOT NULL");
$q->execute(array('field' => $field));
回答2:
Is…
$sql = "SELECT * FROM `tablename` WHERE :field IS NOT NULL";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':field', $field);
…What you're looking for?
来源:https://stackoverflow.com/questions/12462291/pdo-and-is-not-null-function