PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php-自动分配PDO类型大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我的PDO类具有一个名为bindParams的函数,该函数具有2个参数:SETVALues和setType,

输出结果应类似于:

$insertNews->bindParam(':news_title', $newstitle, PDO::PARAM_STR);

因此,我希望为它们的值自动分配“ PDO :: PARAM_STR”,在我的情况下,“ news_title”是变量SETVALues,而“ PDO :: PARAM_STR”是setType:

public final function bindParams($SETVALues=array(), $setType = null){

    //print_r($SETVALues);

    foreach ($SETVALues as $getVal) {

        echo $getVal.'<br />';

    if (is_null($setTypE)) {
        switch ($getVal) {
          case is_int($getVal):
              echo $getVal.' is INT<br />';
            $setType = PDO::PARAM_INT;
            break;
          case is_bool($getVal):
              echo $getVal.' is BOOL<br />';
            $setType = PDO::PARAM_BOOL;
            break;
          case is_null($getVal):
              echo $getVal.' is NULL<br />';
            $setType = PDO::PARAM_NULL;
            break;
          default:
              echo $getVal.' is STR<br />';
            $setType = PDO::PARAM_STR;
        }
    } 


    }

} // end bindParams()

$con = new crud($dbCon);
$con->insert('ban_ip', array('visitor_type', 'ip'));
$con->bindParams(array('Visitor_Type', 1));

输出结果为:

它不会循环另一个值1.

编辑:正确的代码

我认为这可行:

public final function bindParams($SETVALues=array(), $setType = null){


    $combine = array_combine($this->insertedKeys, $SETVALues);

    foreach ($combine as $getKey => $getVal) {

        //echo 'key '.$getKey.' val '.$getVal.'<br />';

        switch ($getVal) {
        case is_int($getVal):
            echo $getVal .' is INT<br />';
            $setType = 'PDO::PARAM_INT';
            break;
        case is_bool($getVal):
            $setType = 'PDO::PARAM_BOOL';
            echo $getVal .' is BOOL<br />';
            break;
        case is_null($getVal):
            echo $getVal .' is NULL<br />';
            $setType = 'PDO::PARAM_NULL';
            break;
        default:
            echo $getVal .' is STR<br />';
            $setType = 'PDO::PARAM_STR';
            break;
    }


    echo "this->stmt->bindParams($getKey, $getVal, $setTypE)<br />";


    }


} // end bindParams()

结果是:

Visitor_Type is STR
this->stmt->bindParams(visitor_type, Visitor_Type, PDO::PARAM_STR)
1 is INT
this->stmt->bindParams(ip, 1, PDO::PARAM_int)

而且,如果我没有记错的话,我应该只执行代码而无需任何回声即可运行它.

谢谢您的帮助

解决方法:

发生的情况是,在第一次通过循环设置$setType之后,在后续迭代中,is_null($setTypE)返回false,因此switch语句从不求值.

您可以执行几项不同的操作,具体取决于您是否实际打算传递$setType.@R_934_7724@,则应删除$setType参数和is_null检查,然后在switch语句之后添加对bindParam($getVal,$setTypE)调用.

另外,请注意您的switch语句值:您可能想要switch(true)(或仅使用if语句),而不是switch($getVal),因为您还会根据实际值(而不仅仅是类型)获得不同的结果. $getVal.

大佬总结

以上是大佬教程为你收集整理的php-自动分配PDO类型全部内容,希望文章能够帮你解决php-自动分配PDO类型所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: