php - How do I add values to class objects then put them in an array? -
i'm trying embed queries variables in class.
like so:
class foobar{ public $bar; } $result = array(); $db = mysqli_connect("127.0.0.1", "foo", "foo", "farm"); $sql = "select * `lot`;"; $result = mysqli_query($db, $sql); while($row = mysqli_fetch_assoc($result)){ $foo = new foobar; $foo->$bar = $row["cow"]; array_push($result, $foo); }
but when print array using print_r($result)
, i'm getting output:
array( )
i hoping see objects stored in array.
where did go wrong? should doing?
you're reusing $result
variable. it's an array reuse mysqli resource.
also $foo->$bar
should $foo->bar
.
class foobar{ public $bar; } $result = array(); $db = mysqli_connect("127.0.0.1", "foo", "foo", "farm"); $sql = "select * `lot`;"; $query_result = mysqli_query($db, $sql); while($row = mysqli_fetch_assoc($query_result)){ $foo = new foobar; $foo->bar = $row["cow"]; array_push($result, $foo); } var_dump($result);
Comments
Post a Comment