Php 数据库连接类:绑定问题
我有一个DB connect类,一切正常,除了绑定值时的步骤外,它在所有字段中插入最后一个字段数据,加上绑定时的类型返回数字2,而不是我指定的(INT、BOOL、NULL…): 因此,应插入: 在田地里倒水 把果酱倒进田里 把果酱倒在地里 以此类推,下面是代码:Php 数据库连接类:绑定问题,php,class,pdo,bind,Php,Class,Pdo,Bind,我有一个DB connect类,一切正常,除了绑定值时的步骤外,它在所有字段中插入最后一个字段数据,加上绑定时的类型返回数字2,而不是我指定的(INT、BOOL、NULL…): 因此,应插入: 在田地里倒水 把果酱倒进田里 把果酱倒在地里 以此类推,下面是代码: <?php final class crud { public function __construct($connexionName) { $this->connexionName = $connexi
<?php
final class crud {
public function __construct($connexionName) {
$this->connexionName = $connexionName;
}
public final function insert($tableName, $fields=array()){
$this->tableName = $tableName;
$this->fields = $fields;
foreach ($this->fields as $vf) {
$inKeys[] = $vf;
$inKeysDotted[] = ':' . $vf;
$insImKeys = implode(', ', $inKeys);
$insImKeysDotted = implode(', ', $inKeysDotted);
$this->insImKeys = $insImKeys;
$this->insImKeysDotted = $insImKeysDotted;
}
$this->insertedKeys = $inKeys;
$this->insertedKeysDotted = $inKeysDotted;
//print_r($this->insertedKeys);
//echo '<br />';
$sql = "INSERT INTO `$this->tableName` ($this->insImKeys) VALUES ($this->insImKeysDotted);";
//echo $sql.'<br />';
$insertItems = $this->connexionName->prepare($sql);
$this->insertItems = $insertItems;
//print_r($insertItems).'<br />';
} // end prepareStm()
public final function bindParams($setValues=array()){
$combine = array_combine($this->insertedKeys, $setValues);
foreach ($combine as $getKey => $getVal) {
switch ($getVal) {
case is_int($getVal):
//echo $getVal .' is INT<br />';
$setType = PDO::PARAM_INT;
//return PDO::PARAM_INT;
break;
case is_bool($getVal):
//echo $getVal .' is BOOL<br />';
$setType = PDO::PARAM_BOOL;
//return PDO::PARAM_BOOL;
break;
case is_null($getVal):
//echo $getVal .' is NULL<br />';
$setType = PDO::PARAM_NULL;
//return PDO::PARAM_NULL;
break;
default:
//echo $getVal .' is STR<br />';
$setType = PDO::PARAM_STR;
//return PDO::PARAM_STR;
break;
return $setType;
}
echo "this->insertItems->bindParam($getKey, $getVal, $setType)<br />";
$this->insertItems->bindParam($getKey, $getVal, $setType);
//echo '<pre>';
//print_r($this->insertItems);
//echo '</pre>';
}
} // end bindParams()
public final function executeQuery(){
return $this->insertItems->execute();
}
}
require_once '../Included_Files/Connect.php';
$con = new crud($connexion);
echo '<br />';
$con->insert('test', array('field1', 'field2', 'field3'));
$con->bindParams(array('pour field1', 'pour field2', 'pour field3'));
$con->executeQuery();
?>
感谢您的支持您的代码中有两个问题,一个主要问题和一个次要问题 首先,因为您正在使用,所以绑定的是一个变量,而不是一个值。这意味着当你打电话时
switch ($getVal) {
case is_int($getVal):
...
在循环的三次连续迭代中,所有这三个字段都绑定到变量$getVal
,该变量的值在循环中每次都会更改
你要做的是打电话。这将把$getVal
(在调用时)的值绑定到参数,而不是变量本身
这是您的主要问题,解决它将使您的代码(大部分)正常工作
您的小问题是您的switch语句: 这相当于在($getVal==is_int($getVal))时编写
。这意味着,如果,$getVal==='0'
(也就是说,$getVal
是一个字符串,其值在布尔上下文中的计算结果为false),那么'0'==is_int($getVal)
('0'不是int,因此is_int返回false),您将尝试将字符串“0”绑定为整数
相反,您应该使用一系列if/else if语句替换switch语句,或者使用switch(true)
我使用的if/else,仍然保持相同的结果:if(is_int($getVal)){$setType=PDO::PARAM_int;}
但是您是否将bindParam()
更改为bindValue()
?这是代码中的主要问题,它根本无法工作。如果我引用参数:$setType=“PDO::PARAM_STR”;回显它们,它返回corect值,我认为,因为它不是一个字符串,它只是一个参数,它将返回与该参数等价的数字,如果我错了,请纠正我pdo::PARAM_STR不应该在其周围加引号。您不需要进行更改,也不清楚您为什么这样做。我只是为了测试我是否得到了正确的参数类型,我认为绑定问题已经解决,它得到了与该类型等效的数字
$this->insertItems->bindParam("field1", $getVal, PDO::PARAM_STR);
$this->insertItems->bindParam("field2", $getVal, PDO::PARAM_STR);
$this->insertItems->bindParam("field3", $getVal, PDO::PARAM_STR);
switch ($getVal) {
case is_int($getVal):
...