简体   繁体   English

来自函数的PHP PDO参数返回数组

[英]PHP PDO Parameters from a function returned array

I've got a function written that runs a query based on parameters passed to the function. 我编写了一个函数,该函数根据传递给该函数的参数运行查询。

I can't seem to figure out why doing the following returns a result: 我似乎无法弄清楚为什么执行以下操作会返回结果:

function test($function_returned_array)
{
    $variable = 'Hello World';
    $sql = 'SELECT `name`, `pid`
            FROM `products`
            WHERE `name` IN (?)';
    $found = $this->db->get_array($sql, $variable);
}

While this doesn't return any results: 虽然这不会返回任何结果:

function test2($function_returned_array)
{
    $sql = 'SELECT `name`, `pid`
            FROM `products`
            WHERE `name` IN (?)';
    $found = $this->db->get_array($sql, $function_returned_array[0]);
}

$function_returned_array[0] is also equal to 'Hello World'. $ function_returned_array [0]也等于'Hello World'。 Shouldn't they both return the same results? 他们都不应该返回相同的结果吗?

When I echo the values of $variable and $function_returned_array[0], they are both 'Hello World' 当我回显$ variable和$ function_returned_array [0]的值时,它们都是“ Hello World”

Here's the relevant parts of my PDO wrapper: 这是我的PDO包装器的相关部分:

public function query(&$query, $params)
{
    $sth = $this->_db->prepare($query);

    if(is_null($params))
    {
        $sth->execute();
    }
    else if(is_array($params))
    {
        $sth->execute($params);
    }
    else
    {
        $sth->execute(array($params));
    }

    $this->_rows = $sth->rowCount();
    $this->_counter++;
    return $sth;
}

public function get_array(&$query, $params, $style = PDO::FETCH_ASSOC)
{
    $q = $this->query($query, $params);
    return $q->fetchAll($style);
}

I'm using PHP 5.3.5. 我正在使用PHP 5.3.5。

Any help would be appreciated. 任何帮助,将不胜感激。

i have witnessed the same problem in PDO. 我在PDO中目睹了同样的问题。 PDO only accepts the local variable for parametrized query and not by reference or argument not sure why though. PDO仅接受参数化查询的局部变量,但不接受引用或自变量,不确定为什么。

the workaround for it can be. 解决方法可以是。

function test2($function_returned_array)
{
    $variable = $function_returned_array[0];
    $sql = 'SELECT `name`, `pid`
            FROM `products`
            WHERE `name` IN (?)';
    $found = $this->db->get_array($sql, $variable);
}

Update: 更新:

You are doing it wrong. 你做错了。 you have to Use PDO prepare() statement for parameterized query whereas i see you are using the PDO query() directly into your get_query() method. 您必须使用PDO prepare()语句进行参数化查询,而我看到您正在将PDO query()直接用于get_query()方法中。

you should use query() method only if you don't want to pass any parameter to the query.and according to php documentation here is the syntax 仅当您不想将任何参数传递给query()才应使用query()方法。根据php文档,这是语法

PDOStatement PDO::query ( string $statement )

the second argument you are trying to pass is wrong. 您尝试传递的第二个参数是错误的。

so instead use prepare() method, and here is the link. 因此,请改用prepare()方法,这是链接。

http://in.php.net/manual/en/pdo.prepare.php http://in.php.net/manual/zh/pdo.prepare.php

Just a heads up, the problem was that $function_returned_array[0] and $variable did not really equal the same thing. 请注意,问题在于$ function_returned_array [0]和$ variable并不完全相同。 $function_returned_array[0] needed to be passed through an html_entity_decode(). $ function_returned_array [0]需要通过html_entity_decode()传递。

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

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