数据库连接 class:绑定问题

DB connect class: bind issue

我有一个数据库连接 class,一切都很好,除了绑定值时的步骤,它在所有字段中插入最后一个字段数据加上类型,同时绑定 returns 数字 2 和不是我指定的 (INT, BOOL, NULL,...):

因此,它应该插入:

将 fied1 倒入 field1 将 fied2 倒入 field2 将 fied3 倒入 field3

等等,这里是代码:

<?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();

?>

echo和print_r的结果是:

INSERT INTO `test` (field1, field2, field3) VALUES (:field1, :field2, :field3);
this->insertItems->bindParam(field1, pour field1, 2)

this->insertItems->bindParam(field2, pour field2, 2)

this->insertItems->bindParam(field3, pour field3, 2) 

感谢您的支持

你的代码有两个问题,一大一小。

首先,因为您使用的是 PDOStatement::bindParam(),所以您绑定的是 变量 ,而不是值。这意味着当你调用

$this->insertItems->bindParam("field1", $getVal, PDO::PARAM_STR);
$this->insertItems->bindParam("field2", $getVal, PDO::PARAM_STR);
$this->insertItems->bindParam("field3", $getVal, PDO::PARAM_STR);

在循环的三个连续迭代中,这些字段的所有三个 都绑定到变量 $getVal,其值在每次循环中都会发生变化。

您要做的是调用 PDOStatement::bindValue()。这会将 $getVal 值(在您进行调用时)绑定到参数,而不是变量本身。

这是您的主要问题,解决该问题将使您的代码(大部分)正常工作。


你的小问题是你的 switch 语句:

switch ($getVal) {
   case is_int($getVal):
   ...

这相当于写if($getVal == is_int($getVal))。这意味着如果 $getVal === '0'(即 $getVal 是一个字符串,其值在布尔上下文中计算为 false),则 '0' == is_int($getVal)('0' 不是 int,所以 is_int returns false),最后你试图将字符串 '0' 绑定为整数。

您应该将 switch 语句替换为一系列 if/else if 语句,或者使用 switch(true).