MySQLi:返回嵌套数组的准备语句

发布于 2024-10-19 05:51:39 字数 3706 浏览 0 评论 0原文

我使用这个类方法返回一个嵌套/多维数组,

public function fetch_all_stmt($sql,$types = null,$params = null)
    {
        # create a prepared statement
        $stmt = parent::prepare($sql);

        if($stmt)
        {
            if($types&&$params)
            {
                $bind_names[] = $types;
                for ($i=0; $i<count($params);$i++) 
                {
                    $bind_name = 'bind' . $i;
                    $$bind_name = $params[$i];
                    $bind_names[] = &$$bind_name;
                }
                $return = call_user_func_array(array($stmt,'bind_param'),$bind_names);
            }

            # execute query 
            $stmt->execute();

            # these lines of code below return multi-dimentional/ nested array, similar to mysqli::fetch_all()
            $stmt->store_result();

            $variables = array();
            $data = array();
            $meta = $stmt->result_metadata();

            while($field = $meta->fetch_field())
                $variables[] = &$data[$field->name]; // pass by reference

            call_user_func_array(array($stmt, 'bind_result'), $variables);

            $i=0;
            while($stmt->fetch())
            {
                $array[$i] = array();
                foreach($data as $k=>$v)
                    $array[$i][$k] = $v;
                $i++;
            }
            return $array;

            # close statement
            $stmt->close();
        }
        else
        {
            return self::get_error();
        }
    }

这就是我调用这个方法的方式,

$sql = "
SELECT *
FROM root_contacts_cfm
WHERE root_contacts_cfm.cnt_suspended = ?
ORDER BY cnt_id DESC
";

print_r($mysqli->fetch_all_stmt($sql,'s',array('0')));

它返回正确,

Array
(
    [0] => Array
        (
            [cnt_id] => 1
            [cnt_email1] => [email protected]
            [cnt_email2] => 
            [cnt_fullname] => Lau T
            [cnt_firstname] => TK
            [cnt_lastname] => Lau
            [cnt_suspended] => 0
            [cnt_created] => 2011-02-04 00:00:00
            [cnt_updated] => 2011-02-04 13:53:49
        )

   [1] => Array
        (
            [cnt_id] => 2
            [cnt_email1] => [email protected]
            [cnt_email2] => 
            [cnt_fullname] => Lau Txx
            [cnt_firstname] => T
            [cnt_lastname] => Lau
            [cnt_suspended] => 0
            [cnt_created] => 2011-02-04 00:00:00
            [cnt_updated] => 2011-02-04 13:53:49
        )
)

但是当我循环它时它返回一条错误消息,

$sql = "
SELECT *
FROM root_contacts_cfm
WHERE root_contacts_cfm.cnt_id = ?
";

for ( $i = 1; $i <= 3; ++$i ) 
{ 
   print_r($mysqli->fetch_all_stmt($sql,'s',array($i)));
}

注意:未定义的变量:数组 C:\wamp\www\000_TEST\php\php_export_excel\class_database.php 在第 377 行

第 377 行引用了 return $array; 之后,

$i=0;
            while($stmt->fetch())
            {
                $array[$i] = array();
                foreach($data as $k=>$v)
                    $array[$i][$k] = $v;
                $i++;
            }
 return $array;

如果我将 return $array; 移动到 while{} 内,则不会发生错误,但是第一个实例将不会返回嵌套数组列表(这是两个嵌套数组),而只会返回一个嵌套数组。

为什么?我该如何修复它?

谢谢。

I use this class method to return a nested/ multi-dimensional array,

public function fetch_all_stmt($sql,$types = null,$params = null)
    {
        # create a prepared statement
        $stmt = parent::prepare($sql);

        if($stmt)
        {
            if($types&&$params)
            {
                $bind_names[] = $types;
                for ($i=0; $i<count($params);$i++) 
                {
                    $bind_name = 'bind' . $i;
                    $bind_name = $params[$i];
                    $bind_names[] = &$bind_name;
                }
                $return = call_user_func_array(array($stmt,'bind_param'),$bind_names);
            }

            # execute query 
            $stmt->execute();

            # these lines of code below return multi-dimentional/ nested array, similar to mysqli::fetch_all()
            $stmt->store_result();

            $variables = array();
            $data = array();
            $meta = $stmt->result_metadata();

            while($field = $meta->fetch_field())
                $variables[] = &$data[$field->name]; // pass by reference

            call_user_func_array(array($stmt, 'bind_result'), $variables);

            $i=0;
            while($stmt->fetch())
            {
                $array[$i] = array();
                foreach($data as $k=>$v)
                    $array[$i][$k] = $v;
                $i++;
            }
            return $array;

            # close statement
            $stmt->close();
        }
        else
        {
            return self::get_error();
        }
    }

This is how I call this method,

$sql = "
SELECT *
FROM root_contacts_cfm
WHERE root_contacts_cfm.cnt_suspended = ?
ORDER BY cnt_id DESC
";

print_r($mysqli->fetch_all_stmt($sql,'s',array('0')));

it return correctly,

Array
(
    [0] => Array
        (
            [cnt_id] => 1
            [cnt_email1] => [email protected]
            [cnt_email2] => 
            [cnt_fullname] => Lau T
            [cnt_firstname] => TK
            [cnt_lastname] => Lau
            [cnt_suspended] => 0
            [cnt_created] => 2011-02-04 00:00:00
            [cnt_updated] => 2011-02-04 13:53:49
        )

   [1] => Array
        (
            [cnt_id] => 2
            [cnt_email1] => [email protected]
            [cnt_email2] => 
            [cnt_fullname] => Lau Txx
            [cnt_firstname] => T
            [cnt_lastname] => Lau
            [cnt_suspended] => 0
            [cnt_created] => 2011-02-04 00:00:00
            [cnt_updated] => 2011-02-04 13:53:49
        )
)

But it returns an error message when I loop it,

$sql = "
SELECT *
FROM root_contacts_cfm
WHERE root_contacts_cfm.cnt_id = ?
";

for ( $i = 1; $i <= 3; ++$i ) 
{ 
   print_r($mysqli->fetch_all_stmt($sql,'s',array($i)));
}

Notice: Undefined variable: array in
C:\wamp\www\000_TEST\php\php_export_excel\class_database.php
on line 377

line 377 refers to return $array; after,

$i=0;
            while($stmt->fetch())
            {
                $array[$i] = array();
                foreach($data as $k=>$v)
                    $array[$i][$k] = $v;
                $i++;
            }
 return $array;

No error occurs if I move return $array; inside while{} but the first instance then will not return a list of nested array (which is two nested arrays), only one nested array.

Why? How can I fix it?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2024-10-26 05:51:39

需要 $array = null;...

$i=0;
            $array = null;
            while($stmt->fetch())
            {
                $array[$i] = array();
                foreach($data as $k=>$v)
                    $array[$i][$k] = $v;
                $i++;
            }
            return $array;

不知道为什么,但它有效!

$array = null; is needed...

$i=0;
            $array = null;
            while($stmt->fetch())
            {
                $array[$i] = array();
                foreach($data as $k=>$v)
                    $array[$i][$k] = $v;
                $i++;
            }
            return $array;

don't know why but it works!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文