PDO and IS NOT NULL Function

爱⌒轻易说出口 提交于 2019-12-23 16:09:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!