Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9207857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:28:51+00:00 2026-06-18T00:28:51+00:00

Why can’t initiate my object to an array[]? This is my work: clothing_product.php include(‘../database.php’);

  • 0

Why can’t initiate my object to an array[]?

This is my work: clothing_product.php

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->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->db->query($sql);
            $object_array = array();
            while($row = $this->db->fect_array($result_set)){

                $object_array[] = $this->instantiate($row);
                echo var_dump($object_array);
            }
            return $object_array;
        }

        private function instantiate($record){
            //Dynamic control
            $object = new self;
            foreach($record as $attribute=>$value){
                if($object->has_attribute($attribute)){
                    $object->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->procode = $a;
            $this->probprice = $b;
            $this->proprice = $c;
            $this->prodes = $d;
            $this->propath = $e;
            $this->prostatus = $f;
            $sql="INSERT INTO `tblclothing_product`(`proCode`,`proBPrice`,`proPrice`,`proDes`,`proPath`,`proStatus`) VALUES(
                                            '" . $a ."',
                                            '" . $b ."',
                                            '" . $c ."',
                                            '" . $d ."',
                                            '" . $e ."',
                                            " . $f ."
                                            )";
            $this->db->query($sql);

        }



        public function image_string($a,$b,$c,$d,$e){

            $this->image='';
            if($a!=""){
                $this->image = $this->image. $a .";";
            }
            if($b!=""){
                $this->image = $this->image. $b .";";
            }
            if($c!=""){
                $this->image = $this->image. $c .";";
            }
            if($d!=""){
                $this->image = $this->image. $d .";";
            }
            if($e!=""){
                $this->image = $this->image. $e;
            }

            return $this->image;
        }



        public function remove_by_id($id){
            $this->db->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->image != ""){
                $arr = explode(";",$this->image);
            }

            return $arr;

        }   




    }

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.

In clothing_view.php it is a script to view my array of object but it has shown null.

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->get_all();

foreach($items as $item){
    echo "ProId:".$item->procode."<br />";
    echo "ProPath:".$item->propath."<br />";
}

In database.php:

require_once(‘config.php’);

class MySqlDatabase
{
    private $connection;
    private $last_query;
    private $magic_quotes_active;
    private $real_escape_string_exist;

        function __construct(){

            $this->open_connection();
            $this->magic_quotes_active = get_magic_quotes_gpc();
            $this->real_escape_string_exist = function_exists("mysql_real_escape_string");


        }

        private function open_connection()
        {

            $this->connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
            if (!$this->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->last_query = $sql;
            $result = mysql_query($sql,$this->connection);

            $this->confirm_query($result);
            return $result;

        }

        public function confirm_query($result){
            if(!$result){
                $output = "Database query failed: " . mysql_error()."<br /><br />";
                $output.= "Last query that fail is:" . $this->last_query;
                die($output);
            }
        }

        private function escape_value($value) {

            if ($this->real_escape_string_exist) {
                if($this->magic_quotes_active) {$value = stripslashes($value);}
                $value = mysql_real_escape_string($value);
            }
            else {
                if (!$this->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->connection);
        }

        public function affected_rows(){
            return mysql_affected_rows($this->connection);
        }

        public function close_connection(){
            if(isset($this->connection)){
                mysql_close($this->connection);
                unset($this->connection);
            }
        }



}

Is there problem with $item->? It always returns null.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:28:52+00:00Added an answer on June 18, 2026 at 12:28 am

    Finally, I got what you trying to do.. If you need db result as object (ORM-like), you can use another class for this (just an advise).

    Pseudo;

    // Let's call another class as CProduct
    class CProduct
    {
        public $procode, $probprice,
               $proprice, $prodes,
               $propath, $prostatus,
               $image;
    
        public function __construct($row) {
            foreach ($row as $key => $val) {
                if ($this->_hasAttr($key)) {
                    $this->$key = $val;
                }
            }
        }
    
        private function _hasAttr($key) {
            return array_key_exists($key, get_class_vars($this));
        }
    
        // you can also extend your class here
        // and use it anywhere
        public function printCode() {
            print $this->procode;
        }
    }
    

    And using in your case;

    while ($row = $this->db->fect_array($result_set)) {
        $object_array[] = new CProduct($row);
    }
    

    Html part;

    <p>Product Code: <?php $cp->printCode(); ?></p>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can find why i get this error can someone help? package Android.data; public class
Can anyone help me trying to find out why this doesn't work. The brushes
Can I order my users in the database, so I don't have to say
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can anyone explain to me why this program: for(float i = -1; i <
Can someone maybe tell me why this is not working? I have used echo
Can anybody tell me a regular expression to use within some PHP to find
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
can someone explain what means this line, or provide with source where it is
Can someone please explain how this code is working for me? I don't understand

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.