不能将标量值用作数组,但数据已成功更新


Cannot Use a Scalar Value as an Array, But Data Successfully Updated

我有这段代码:

public function updateSegmentGender ($product_id, $segment_gender) {
    $this->connect();
    $product_id = $this->escapeString($product_id);
    $row_count = count($segment_gender);
    for ($row = 0; $row < $row_count; $row++) {
        $this->select('product_seg_gender', '*', NULL, 
                      'product_id = "'.$product_id.'" AND gender = "'.$segment_gender[$row][0].'"', NULL, NULL);
        $res = $this->getResult();
        $res = count($res);
        $gender = $segment_gender[$row][0];
        $status = $segment_gender[$row][1];
        if ($res <> 0) {
            // UPDATE
            $data = array ('status'=>$status);
            $this->update('product_seg_gender', $data, 'product_id = "'.$product_id.'" AND gender = "'.$gender.'"');
        }else {
            // INSERT
            $data = array ('product_id'=>$product_id, 'gender'=>$gender, 'status'=>$status);
            $this->insert('product_seg_gender', $data);
        }
    }
}

我像这样使用这种方法:

$user = new Product($db_server, $db_user, $db_password, $db_name);
$user->connect();
$segment_gender = array ( array ("all", "active"),
                          array ("female", "active"));
$res = $user->updateSegmentGender ('303', $segment_gender);
print_r($res);

但是为什么我总是收到此错误消息:

Warning: Cannot use a scalar value as an array in /home/***/public_html/class/Database.class.php on line 130

但是,数据库已成功更新。 我做错了什么?

更新:这是数据库的完整行97-145.class.php

// Function to SELECT from the database
public function select($table, $rows = '*', $join = null, $where = null, $order = null, $limit = null){
    // Create query from the variables passed to the function
    $q = 'SELECT '.$rows.' FROM '.$table;
    if($join != null){
        $q .= ' JOIN '.$join;
    }
    if($where != null){
        $q .= ' WHERE '.$where;
    }
    if($order != null){
        $q .= ' ORDER BY '.$order;
    }
    if($limit != null){
        $q .= ' LIMIT '.$limit;
    }
    // echo $table;
    $this->myQuery = $q; // Pass back the SQL
    // Check to see if the table exists
    if($this->tableExists($table)){
        // The table exists, run the query
        $query = $this->myconn->query($q);    
        if($query){
            // If the query returns >= 1 assign the number of rows to numResults
            $this->numResults = $query->num_rows;
            // Loop through the query results by the number of rows returned
            for($i = 0; $i < $this->numResults; $i++){
                $r = $query->fetch_array();
                $key = array_keys($r);
                for($x = 0; $x < count($key); $x++){
                    // Sanitizes keys so only alphavalues are allowed
                    if(!is_int($key[$x])){
                        if($query->num_rows >= 1){
                            $this->result[$i][$key[$x]] = $r[$key[$x]];
                        }else{
                            $this->result[$i][$key[$x]] = null;
                        }
                    }
                }
            }
            return true; // Query was successful
        }else{
            array_push($this->result,$this->myconn->error);
            return false; // No rows where returned
        }
    }else{
        return false; // Table does not exist
    }
}

注意:我希望您需要$res = $user->updateSegmentGender ('303', $segment_gender);需要$data值,并且基于假设我使用$data作为返回部分。

也许是由于$data未初始化。我正在尝试将变量 $data 声明为数组,在使用它之前,我还进行了另一项更改,function updateSegmentGender()需要返回部分,所以我把它放了。

public function updateSegmentGender ($product_id, $segment_gender) {
    $this->connect();
    $product_id = $this->escapeString($product_id);
    $row_count = count($segment_gender);
    $data = array();//Initialize variable...
    for ($row = 0; $row < $row_count; $row++) {
        $this->select('product_seg_gender', '*', NULL, 
                      'product_id = "'.$product_id.'" AND gender = "'.$segment_gender[$row][0].'"', NULL, NULL);
        $res = $this->getResult();
        $res = count($res);
        $gender = $segment_gender[$row][0];
        $status = $segment_gender[$row][1];
        if ($res <> 0) {
            // UPDATE
            $data = array ('status'=>$status);
            $this->update('product_seg_gender', $data, 'product_id = "'.$product_id.'" AND gender = "'.$gender.'"');
        }else {
            // INSERT
            $data = array ('product_id'=>$product_id, 'gender'=>$gender, 'status'=>$status);
            $this->insert('product_seg_gender', $data);
        }
    }
    //Return funal value in array format...
    return $data;
}