gpt4 book ai didi

php - PDO 类中的调用值

转载 作者:行者123 更新时间:2023-11-30 21:53:06 25 4
gpt4 key购买 nike

类函数

public function bind($param, $value, $type = null)
{
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}

调用方式

`$this->bind(":id","1","is_int");` 

//output = $this->stmt->bindValue(:id,1,is_int);

问题:我如何传递值以便我可以将值获取到$this->stmt->bindValue(":id","1",PDO::PARAM_INT);

最佳答案

您的代码中有几个错误。首先,您使用 is_null() 检查 $type 是否为 null,这似乎与您想要的相反,因为 switch只有当 $typeNULL 时才会执行,如果是,则不会满足任何条件。此外,该调用是多余的,因为您仍然在 switch 中处理 NULL$type

在您的 switch 中,您在 $type 上调用了几个不同的函数,但没有一个会得到您想要的。如果你想传递一个字符串来指示你需要什么样的 PARAM,那么这样做:

public function bind($param, $value, $type = null)
{
switch ($type) {
case "is_int":
$type = PDO::PARAM_INT;
break;
case "is_bool":
$type = PDO::PARAM_BOOL;
break;
case "is_null":
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
$this->stmt->bindValue($param, $value, $type);
}

作为最后的评论,我将 switchdefault 设置为 PDO::PARAM_NULL,因为这是您的默认值用于函数中的参数:

switch ($type) {
case "is_int":
$type = PDO::PARAM_INT;
break;
case "is_bool":
$type = PDO::PARAM_BOOL;
break;
case "is_null":
$type = PDO::PARAM_NULL;
break;
case "is_string":
$type = PDO::PARAM_STR;
break;
default:
$type = PDO::PARAM_NULL;
}

关于php - PDO 类中的调用值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46334895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com