Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy can't instantiate my object to an array[]?
    primarykey
    data
    text
    <p>Why can't initiate my object to an array[]?</p> <p>This is my work: clothing_product.php </p> <pre><code>include('../database.php'); class Clothing_Product { public $db; public $procode; public $probprice; public $proprice; public $prodes; public $propath; public $prostatus; public $image; public function __construct() { $this-&gt;db = new MySqlDatabase(); } public function get_all(){ return self::get_by_query('SELECT * FROM tblclothing_product;'); } public function get_by_query($sql=""){ $result_set = $this-&gt;db-&gt;query($sql); $object_array = array(); while($row = $this-&gt;db-&gt;fect_array($result_set)){ $object_array[] = $this-&gt;instantiate($row); echo var_dump($object_array); } return $object_array; } private function instantiate($record){ //Dynamic control $object = new self; foreach($record as $attribute=&gt;$value){ if($object-&gt;has_attribute($attribute)){ $object-&gt;attribute = $value; } } return $object; } private function has_attribute($attribute){ $object_vars = get_object_vars($this); return array_key_exists($attribute,$object_vars); } public function add_new($a,$b,$c,$d,$e,$f){ $this-&gt;procode = $a; $this-&gt;probprice = $b; $this-&gt;proprice = $c; $this-&gt;prodes = $d; $this-&gt;propath = $e; $this-&gt;prostatus = $f; $sql="INSERT INTO `tblclothing_product`(`proCode`,`proBPrice`,`proPrice`,`proDes`,`proPath`,`proStatus`) VALUES( '" . $a ."', '" . $b ."', '" . $c ."', '" . $d ."', '" . $e ."', " . $f ." )"; $this-&gt;db-&gt;query($sql); } public function image_string($a,$b,$c,$d,$e){ $this-&gt;image=''; if($a!=""){ $this-&gt;image = $this-&gt;image. $a .";"; } if($b!=""){ $this-&gt;image = $this-&gt;image. $b .";"; } if($c!=""){ $this-&gt;image = $this-&gt;image. $c .";"; } if($d!=""){ $this-&gt;image = $this-&gt;image. $d .";"; } if($e!=""){ $this-&gt;image = $this-&gt;image. $e; } return $this-&gt;image; } public function remove_by_id($id){ $this-&gt;db-&gt;query('DELETE FROM tblclothing_product WHERE proId={$id};'); } public function get_by_id($id=0){ $result_array = self::get_by_query('SELECT * FROM tblclothing_product WHERE proId={$id} LIMIT 1;'); return !empty($result_array) ? array_shift($result_array) : FALSE; } public function image_exploit_string(){ $arr = array(); if($this-&gt;image != ""){ $arr = explode(";",$this-&gt;image); } return $arr; } } </code></pre> <p>I try to echo the above script var_dump($object_array) my array and it has shown null to every object. It looks like it can't initiate at all.</p> <p>In clothing_view.php it is a script to view my array of object but it has shown null.</p> <pre><code>require_once("../include/function.php"); require_once("../include/database.php"); $buttonname = $_POST["submit"]; $image1 = ''; $image2 = ''; $image3 = ''; $image4 = ''; $image5 = ''; $image1 = $_FILES['image1']['name']; $image2 = $_FILES["image2"]['name']; $image3 = $_FILES["image3"]['name']; $image4 = $_FILES["image4"]['name']; $image5 = $_FILES["image5"]['name']; $obj = new Clothing_Product(); $items = $obj-&gt;get_all(); foreach($items as $item){ echo "ProId:".$item-&gt;procode."&lt;br /&gt;"; echo "ProPath:".$item-&gt;propath."&lt;br /&gt;"; } </code></pre> <p>In database.php:</p> <p>require_once('config.php');</p> <pre><code>class MySqlDatabase { private $connection; private $last_query; private $magic_quotes_active; private $real_escape_string_exist; function __construct(){ $this-&gt;open_connection(); $this-&gt;magic_quotes_active = get_magic_quotes_gpc(); $this-&gt;real_escape_string_exist = function_exists("mysql_real_escape_string"); } private function open_connection() { $this-&gt;connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS); if (!$this-&gt;connection){ die("Connection failed!". mysql_error()); } else{ $db_select = mysql_select_db(DB_NAME); if(!$db_select){ die("Select database failed". mysql_error()); } } } public function query($sql){ $this-&gt;last_query = $sql; $result = mysql_query($sql,$this-&gt;connection); $this-&gt;confirm_query($result); return $result; } public function confirm_query($result){ if(!$result){ $output = "Database query failed: " . mysql_error()."&lt;br /&gt;&lt;br /&gt;"; $output.= "Last query that fail is:" . $this-&gt;last_query; die($output); } } private function escape_value($value) { if ($this-&gt;real_escape_string_exist) { if($this-&gt;magic_quotes_active) {$value = stripslashes($value);} $value = mysql_real_escape_string($value); } else { if (!$this-&gt;magic_quotes_active) {$value = addslashes($value);} } return $value; } public function fect_array($result){ return mysql_fetch_array($result); } public function num_rows($result){ return mysql_num_rows($result); } public function last_id(){ return mysql_insert_id($this-&gt;connection); } public function affected_rows(){ return mysql_affected_rows($this-&gt;connection); } public function close_connection(){ if(isset($this-&gt;connection)){ mysql_close($this-&gt;connection); unset($this-&gt;connection); } } } </code></pre> <p>Is there problem with $item->? It always returns null.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload