1

Finally taking the time to mess with php classes, and curious to know if something like the following is the most efficient way to repeatedly use mysql results:

class ProjectDetails {
private $ProjectDetails;
function getDetails($projectid) {
    $query_GetDetails = 
    "SELECT * FROM projects WHERE ProjectID = $projectid";

    $results_ProjectDetails = mysql_query($query_GetDetails);
    $ProjectDetails = mysql_fetch_array($results_ProjectDetails);
    $this->ProjectDetails = $ProjectDetails;

}

    function getDetail($el) {
         $this->ProjectDetails[$el];
    }

}

While reading up on the topic, I found some people would call their query function in each of their desired result functions, which in my case would be calling GetDetails() within GetDetail(), just to have access to that returned array. Am I correct in assuming the above posted method is more efficient? And also, is there a better way to do this?

Thanks as always,

1
  • As Liam noted, don't use mysql_query. Prepared statements are the best, and can precompile queries so if you're running the same query (with different parameters multiple times) it can cache the query. I wouldn't worry about getting the absolutely fastest method - if you're doing a lot of stuff with databases I'd recommend a framework or library. For example, take a look at Zend DB. Saves the developer a lot of time, which is far more important than shaving a little CPU time Commented Jul 16, 2013 at 17:50

2 Answers 2

1

Firstly don't use mysql_query it is depreciated! Use PDO also don't make multiple requested to your connect. I'm not sure if it is the best way but I have found the better way is to have a database class. Have a constructor which connects to your database then have a query function which you pass in your query and returns your results so in a way all you need is 2 methods! There are a few class examples on http://php.net/manual/en/class.pdo.php

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

2 Comments

Thanks all, I'll try this PDO stuff and see where I get. I'd just prefer not to learn bad habits from the get-go.
Just a quick comment to save you time the pdo execute accepts an array with a key to match the key (item =>) with the placement (:item)
1

You are not going to get a clear answer as to what is "best" or "most efficient" here, as this would really depend on the needs of your application - the data access pattern, the size of the data you are going to work with, your tolerance for latency in accessing the data, etc.

You are always going to have trade-offs. For example, if you store that result array in the class, you are going to need to utilize memory to do so. So if the array is small and you will need to access the data frequently through the processing of the request, this might be your best bet. If however the array is substantially large and you have a high concurrency of requests that would store this data in memory, you may not want to allocate that much server memory to storing the result for each request. Who knows, you might even be best served using a cache for certain results.

The bottom line is that you really have to understand the trade-offs you are making and test them in your environment to determine what works best to meet your goals, whether it be reduced response latency, reduced memory usage, or whatever.

Comments

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.