Jeff
Jeff

Reputation: 165

Problems with a PHP property which is an array

I get an "Array to string conversion" error when calling the method below. The line of the error is the only line within the method (the one with the database query).

public function setStatus($status) {
    $this->mysqli->query("UPDATE projects SET status='$status' WHERE id='$this->data[id]' LIMIT 1");
}

If I modify the method a bit, it works fine:

public function setStatus($status) {
    $id = $this->data['id'];
    $this->mysqli->query("UPDATE projects SET status='$status' WHERE id='$id' LIMIT 1");
}

What am I doing wrong? Here's more information about the "data" property:

    $result = $this->mysqli->query("SELECT * FROM projects WHERE id='123' LIMIT 1");
    $this->data = $result->fetch_assoc();

Upvotes: 0

Views: 29

Answers (1)

Kyle Aldridge
Kyle Aldridge

Reputation: 107

public function setStatus($status) {
    $this->mysqli->query("UPDATE projects SET status='$status' WHERE id='$this->data[id]' LIMIT 1");
}

to

public function setStatus($status) {
    $this->mysqli->query("UPDATE projects SET status='$status' WHERE id='".$this->data['id']."' LIMIT 1");
}

notice at the id field I changed it.

Upvotes: 3

Related Questions