0

I have this class in PHP, and I have been trying to get all the variables using a function, but the problem is in my database there are more than 1000 rows, so I will use the SELECT statement each time the class called. For instance

        class person
        {
            var $id; // this id will be passed to function get_all()

            var $name;
            var $age;
            var $salary;

            function get_all($id)
            {
                $Query = 'SELECT FROM `person` WHERE `id` = '.$id.'';
                /*
                code to excute the query
                */

                $this->name = somthing;
                $this->age = somthing;
                $this->salary = somthing;
            }
        }

So now I don't know if this method it's the same when I use this method in my PHP code ?

    $Query = 'SELECT * FROM `person`';

    /* excute the Query .. */

    foreach($person_array as $person)
    {
        // I don't know how to make it especially beacause it's dynamic
    }

Is there any possible way to use the same class with use 1 Query ? or if the first method will not make any problem please tell me.

2 Answers 2

1

Just modify your first function:

function get_all($id) {
    $Query = 'SELECT * FROM `person`;';
    /*
    code to excute the query
    */
    foreach (/*your foreach statements here*/) {
        $person = new Person; // Create a new person object for each record
        $person->name = somthing;
        $person->age = somthing;
        $person->salary = somthing;
        $persons[] = $person; // Add this person to an array
    }
    return $persons; // Return an array of person objects, containing everyone
}

Using this method is efficient, and will return an array of all the persons you want.

Sign up to request clarification or add additional context in comments.

5 Comments

Good. I don't think you need to pass $id here though.
@samyi, Yeah, if he wants, he can do that. This is just an example. The function was called get_all though, so I'm not really sure that's what he wants. I'll let him decide. :-D
@OthmanAbahossain, No problem. Also, for basic functions like this, I usually make them static. That way you don't have to create a new object, just to go and get all the other objects. You could call it directly, such as Person::get_all().
it's not basic as you think it's a big class, but i just want to know if I hit the database 1000 times will be the same or not ?
@OthmanAbahossain, Big class, small class, doesn't matter unless you need constructor parameters (or something else) that require it not to be static. Also, it is far more efficient to handle the full result in PHP. Don't go out to your DB 1,000 times each page load... that's asinine.
0

These two methods are not same.If that id is your indexed/primary key,first one is better only id you have to fire one query.In case you need all rows,second one is better.

4 Comments

So if I executed 1000 SQL line better than one and analyze it using PHP?
I disagree. You're iterating through 1000 records in both examples. Except, in the first one you're hitting the database each time. I think you'll notice a significant performance upgrade by iterating through a dataset verses hitting the database each time. Not to mention if there is latency in accessing the database.
Looping the DB query result in PHP is certainly not slower in all but a few edge cases. Hitting your database is expensive.
sorry,initially got it wrong..i thought you just wanted to fire a single query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.