Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中创建像bind_param()这样的函数?_Php_Mysql_Mysqli - Fatal编程技术网

如何在PHP中创建像bind_param()这样的函数?

如何在PHP中创建像bind_param()这样的函数?,php,mysql,mysqli,Php,Mysql,Mysqli,我正在尝试为MySQL编写一个小shell,它有一些代码所需的基本功能。这是为了进行测试,并进一步了解PHP 我遇到的问题如下: bool mysqli_stmt::bind_param ( string $types , mixed &$var1 [, mixed &$... ] ) 上面是我在互联网上找到的确切定义,但我不能让我的函数像这样语法错误,或者服务器说:我需要一个“mixed”类型的var public function bind($types, $values)

我正在尝试为MySQL编写一个小shell,它有一些代码所需的基本功能。这是为了进行测试,并进一步了解PHP

我遇到的问题如下:

bool mysqli_stmt::bind_param ( string $types , mixed &$var1 [, mixed &$... ] )
上面是我在互联网上找到的确切定义,但我不能让我的函数像这样语法错误,或者服务器说:我需要一个“mixed”类型的var

public function bind($types, $values)
{
    $this->statement->bind_param($types, $values);
}

以上是我大致想要实现的,但即使搜索web,我也无法找到解决方案。

混合类型不存在,它只是说传递的参数可能是字符串,也可能是整数或其他任何类型。您应该使用一个参数$types编写函数,所有其他参数都来自func\u get\u args。 这将传递给函数的所有参数,以便您可以像处理数组一样使用它们:

function bind_param(string $types)
{
$args = func_get_args();
print_r($args);
}

bind_param("foo", "bar", 123, false, array("a","b"));
请注意,它还将包括第一个$types参数

public function bindParam($types)
{
    /* For types, the next rules count:
    i   corresponding variable has type integer
    d   corresponding variable has type double
    s   corresponding variable has type string
    b   corresponding variable is a blob and will be sent in packets
     */
    $fetchedParameters = func_get_args();
    $parameters = "\"$types\"";//Create a variable that holds the types of the statement
    for($i = 1; $i < count($fetchedParameters);$i++)
    {
            $parameters .= ",\$fetchedParameters[$i]";   //Each variable name gets added to parameters(it needs to be the variable name, not the value!!)               
    }
    eval("\$this->statement->bind_param($parameters);");//Assemble the statement and execute it.     
}

这几乎就是我所做的,我的参数不是由用户提供的,所以不应该有任何安全问题。这完全解决了我的问题。

什么类型的$values?是字符串、整数、double和blob@Rob比尔曼:你能把你的代码写得更详细一点吗?其中$this->statement=$this->connection->prepareSelect*来自产品,其中productname=$产品=t恤$这个->绑定$value;这就是“statement”的初始化方式,以及我希望如何使用bind函数。诀窍是我希望它有尽可能多的变量,变量的数量是可变的。php5.6+有,如果你的版本更早或者你需要兼容性,那么func_get_args就是答案。