脚本宝典收集整理的这篇文章主要介绍了PHP – 如何在预处理语句中将数组替换为主机参数,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
// a helPEr function to map sqlITe data type function getargType($arg) { switch (gettype($arg)) { case 'double': return sqlite3_FLOAT; case 'integer': return sqlite3_INTEGER; case 'boolean': return sqlite3_INTEGER; case 'NULL': return sqlite3_NULL; case 'string': return sqlite3_TEXT; default: throw new \InvalidargumentException('Argument is of invalid type '.gettype($arg)); } } $sql = "SELECT * From table_name WHERE id IN (?)"; $params = [[10,9,6]]; // array of array $dbpath = '/path/to/sqlite.sqlite'; $db = new sqlite3($dbPath,sqlite3_OPEN_READONLY); $stmt = $db->PRepare($sql); try { foreach ($params as $index => $val) { if (is_array($val)) { /************* I am stuck here *************/ $ok = $stmt->bindParam($index + 1,$val); // Using bindValue also didn't worked! } else { $ok = $stmt->bindValue($index + 1,$val,getArgType($val)); } if (!$ok) { throw new Exception("Unable to bind param: $val"); } } } catch (Exception $ex) { // NO exception is thrown from bindValue() or bind@R_547_1403@ $reason = "Error in binding statement. " . $ex->getMessage(); die($reason); } $result = $stmt->execute(); $data = []; while ($row = $result->fetchArray($mode)) { $data[] = $row; } var_dump($data);
编辑:我已经尝试过替换单身?在Param数组中包含所需数量的问号,但只有当我的数组的值小于1000时它才有效!我认为这是PHP中sqlite3中语句编写方式的限制.
>使用数组中的每个值创建一个占位符(?)的SQL查询
>通过遍历数组来绑定每个值.
但是还有另外一种选择(例如子SELECT)
More information here(即使它是一个Java问题,它几乎是相同的主题/问题,因为在这种情况下数据库类型无关紧要)
编辑:通常,绑定参数的sql限制设置为999,但是你是can change it if you need to.
以上是脚本宝典为你收集整理的PHP – 如何在预处理语句中将数组替换为主机参数全部内容,希望文章能够帮你解决PHP – 如何在预处理语句中将数组替换为主机参数所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。