1

Is it possible to ask for all data in my database and make objects from it and save it into an array or something, so I just need to call the database once and afterwards I just use my local array? If yes, how is it done?

public function getAllProjects(){

   $query="SELECT * FROM projects";
   $result=mysql_query($query);
   $num=mysql_numrows($result);
   while ($row = mysql_fetch_object($result)) {
      // save object into array
   }
}

public function fetchRow($row){
  include("Project.php");
  $project = new Project();

  $id=$row->id;
  $project->setId($id); 

  $title=$row->title;
  $project->setTitle($title);   

  $infos=$row->info;
  $project->setInfo($infos);

  $text=$row->text;
  $project->setText($text);

  $cate=$row->category;
  $project->setCategory($cate);

  return $project;
}

If I have for example this code. How do i store the objects correctly into an array, where I grab the data from? And why can't I make more than one object of type "Project"?

4
  • Do you want Mysql Cache? Commented Sep 25, 2012 at 9:33
  • Yes, sure, this is possible. What problem do you have implementing something like this? But mirroring the entire database seems quite pointless. What's the database for then? Commented Sep 25, 2012 at 9:33
  • Add it to the $_SESSION or just use memcached. Views are often a good solution for speed too. Commented Sep 25, 2012 at 9:34
  • You can also use APC as it is now part of PHP. So can use serialize() and unserialize() to save it a a sting. But I think if you want to bring over your entire database you have a design flow. Commented Sep 25, 2012 at 9:37

2 Answers 2

2

Let's ignore the fact that you will run out of memory.

If you have everything in an array you will no longer have the functionalities of a relational database.
Try a search over a multi megabytes, multi dimensional array in php and be prepared for a extended coffee break. If you are thinking in doing something like that is because you feel that the database is slow... You should learn about data normalization and correct use of indexes then.
And no NoSQL is not the answer.
Sorry to pop your balloon.

Edited to add: What you CAN to is use memcache to store the final product of some expensive processes. Don't bother storing the result of trivial queries, the internal cache of mysql is very optimized for those.

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

3 Comments

I don't need the database to edit data or somewhat. I just have information of my projects in there as you can see in my added example code. I have title, info and a text. And I tried to call the database every time for every project I click, bu tin this case title, info and text swap randomly and do not refer to the right project. I couldn't figure out why.
That's an entirely different question would you post the output of SHOW CREATE TABLE projects?
And the output of print_r($object_array) I thing that you are reading the numerical id of the array instead of the database one
1

You should use the $_SESSION vars in php, To use them, add a session_start() at the beginning of your code. Then you can set vars with $_SESSION['selectaname'] = yourvar

Nothing prevent you to make a sql query like "SELECT username FROM users WHERE id = 1" and then set a $_SESSION['user'] = $queryresult

Then you'll have this :

echo $_SESSION['user'];

"mylogin"

2 Comments

Can i access this variable from different php files?
Yep, you just need to use session_start() one time each time you make a request to the server, so it depends on your code, and don't forget to put the session_start on the TOP of your code

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.