PHP数组引用问题

时间:2014-04-25 15:14:39

标签: php arrays

我对发生的事情一无所知!

这是错误:

Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in C:\xxxx\htdocs\xxxxx\xxxxx\class.mysql.php on line 51

Fatal error: Uncaught exception 'ReflectionException' with message 'Invocation of method mysqli_stmt::bind_param() failed' in C:\xampp\htdocs\assemblyx.net\class\class.mysql.php:51 Stack trace: #0 C:\xxxx\htdocs\xxxxx\xxxxx\class.mysql.php(51): ReflectionMethod->invokeArgs(Object(mysqli_stmt), Array) #1 C:\xxxx\htdocs\xxxxx\xxxxx\index.php(17): mysql->Insert('test', Array) #2 {main} thrown in C:\xxxx\htdocs\xxxxx\xxxxx\class.mysql.php on line 51

index.php中的代码:

$a=array();
$a[0]=array();
$a[0]["test_value"]="helloMysqli";
$a[0]["test_int"]="2";
$a[0]["test_dec"]="3.1";
$db->Insert("test", $a);

class.mysql.php中的方法:

function Insert($table, $arrInsert){
    $types = $cols = $q = "";
    $value = array();
    foreach($arrInsert as $k => $v){
        foreach($v as $k2 => $v2){
            $cols .= ($cols=="")?$k2:", ".$k2;
            $q .= ($q=="")?"?":", ?";
            $value[] = $v2;
            $types .= substr(gettype($v2),0,1);
        }
    }
    $sql = "insert into ".$table." (".$cols.") value (".$q.");";
    $stmt = $this->prepare($sql);

    $refArr = array();
    $refArr[0] = $types;
    foreach($value as $k => $v){
        $refArr[] = $v;
    }

    $ref = new ReflectionClass('mysqli_stmt'); 
    $method = $ref->getMethod("bind_param");
    $method->invokeArgs($stmt,$refArr); 
    $stmt->execute();
}

现在,如果我将数组编码到Insert方法中,则每个工作正常:

function Insert($table, $arrInsert){
    $types = $cols = $q = "";
    $value = array();
    foreach($arrInsert as $k => $v){
        foreach($v as $k2 => $v2){
            $cols .= ($cols=="")?$k2:", ".$k2;
            $q .= ($q=="")?"?":", ?";
            $value[] = $v2;
            $types .= substr(gettype($v2),0,1);
        }
    }
    $sql = "insert into ".$table." (".$cols.") value (".$q.");";
    $stmt = $this->prepare($sql);

    $refArr = array();
    $refArr[] = $types;
    foreach($value as $k => $v){
        $refArr[] = $v;
    }
//****************
// added array here
//*****************     
    $refArr = array();
    $refArr[] = "sss";
    $refArr[] = "helloMysqli";
    $refArr[] = "2";
    $refArr[] = "3.1";

    $ref = new ReflectionClass('mysqli_stmt'); 
    $method = $ref->getMethod("bind_param");
    $method->invokeArgs($stmt,$refArr); 
    $stmt->execute();
}

我显然不理解数组是如何被引用的。请有人赐教!

以下是print_r输出,表明两个数组的输出相同:

    function Insert($table, $arrInsert){
        $types = $cols = $q = "";
        $value = array();
        foreach($arrInsert as $k => $v){
            foreach($v as $k2 => $v2){
                $cols .= ($cols=="")?$k2:", ".$k2;
                $q .= ($q=="")?"?":", ?";
                $value[] = $v2;
                $types .= substr(gettype($v2),0,1);
            }
        }
        $sql = "insert into ".$table." (".$cols.") value (".$q.");";
        $stmt = $this->prepare($sql);

        $refArr = array();
        $refArr[] = $types;
        foreach($value as $k => $v){
            $refArr[] = $v;
        }
        print_r($refArr);
        exit;

// print_r output : Array ( [0] => sss [1] => helloMysqli [2] => 2 [3] => 3.1 )

        $refArr = array();
        $refArr[] = "sss";
        $refArr[] = "helloMysqli";
        $refArr[] = "2";
        $refArr[] = "3.1";


// print_r output : Array ( [0] => sss [1] => helloMysqli [2] => 2 [3] => 3.1 )



        $ref = new ReflectionClass('mysqli_stmt'); 
        $method = $ref->getMethod("bind_param");
        $method->invokeArgs($stmt,$refArr); 
        $stmt->execute();
    }

1 个答案:

答案 0 :(得分:1)

当我添加&在forloop看起来像这样:

foreach($value as $k => $v){$refArr[] = &$value[$k];}