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

时间:2016-01-21 05:24:52

标签: php

我有这段代码:

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

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

更新:这是Database.class.php的完整第97-145行

// 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
    }
}

1 个答案:

答案 0 :(得分:1)

注意:我希望您需要$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;
}