繁体   English   中英

将布尔true / false插入Postgres和MySQL

[英]Insert boolean true / false to Postgres and MySQL

我与数据库打架,试图用布尔值列将真值和假值插入到我的表中,但总是会出现错误:

无效的参数号

经过测试:

$value = true
$value = "true"
$value = 1

有人可以建议我吗? 谢谢

编辑:完整代码如下所示:

// adding value to variabile
                    if (empty($row['vin']))
                        $vin = 0;                   
                    else
                        $vin = 1;
//calling insert method
$this->insertToTable($model_code, $typ, $kind, $ts, $vin,  $smr, $ire, $manufacturer_code);

//full insert method:
    public function insertToTable($code, $name, $kind, $ts, $vin, $smr, $ire, $manufacturer_code)
    {                   
        try {                                       
            $con = new PDO( DB_HOST, DB_USER, DB_PASS );            
            $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );            
            $sql = "INSERT INTO r_vehicle_model(code, name, kind, ts, vin, smr, ire, manufacturer_code) VALUES(:code, :name, :kind, :ts, :smr, :ire, :manufacturer_code)";
            $stmt = $con->prepare( $sql );
            $stmt->bindValue( 'code', $code, PDO::PARAM_STR );                
            $stmt->bindValue( 'name', $name, PDO::PARAM_STR );                
            $stmt->bindValue( 'kind', $kind, PDO::PARAM_STR );                
            $stmt->bindValue( 'ts', $ts, PDO::PARAM_STR );                
            $stmt->bindValue( 'vin', $vin, PDO::PARAM_STR );                
            $stmt->bindValue( 'smr', $smr, PDO::PARAM_STR );                
            $stmt->bindValue( 'ire', $ire, PDO::PARAM_STR );                
            $stmt->bindValue( 'manufacturer_code', $manufacturer_code, PDO::PARAM_STR );                
            $stmt->execute();                
            }
        catch( PDOException $e ) {
                echo $e->getMessage();
            }           
    }   

完全错误:

SQLSTATE [HY093]:无效的参数编号::vin

在插入查询中忘记:vin 值中的参数数量不等于bindValue

 $sql = "INSERT INTO r_vehicle_model(code, name, kind, ts, vin, smr, ire, manufacturer_code) VALUES(:code, :name, :kind, :ts, ,:vin ,:smr, :ire, :manufacturer_code)";

您当前正在使用PDO::PARAM_STR来指定要传递的所有参数都是字符串。

您应该为该字段选择适当的类型,因此对于布尔值,请考虑使用PDO::PARAM_BOOL

http://php.net/manual/en/pdo.constants.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM