Jump to content

Selective MySQL queries versus arrays (accessing data)


Recommended Posts

I wanted an opinion on this. I have created a text based game which requires multiple attributes to be used in the scripts.

 

My question is whether or not it would be better just to pull all the data at the top of each page and store it in multi-dimensional arrays for example.... array(name of user array(data of user))

and then access this using array[user][data]

 

or to use mysql queries throughout the script to select specific row data?

 

Opinions appreciated

Deciding how much to pull from a database is a zen art in it's own right. You'll need to find balance between limiting the number of queries and loading unnecessary data.

 

If you think you might need it, and you can pull it with the same query, do so, unless that means allocating large quantities of memory. If you can save yourself a trip to the database, you should always seriously consider doing so, even if it is not absolutely certain you will need the data.

<?php
class Users implements ArrayObject {
  function offsetGet($index) {
    $q = mysql_query('SELECT * FROM users WHERE username=\''.addslashes($index).'\'');
    if(mysql_num_rows($q))
      return mysql_fetch_assoc($q);
  }
}
$users = new Users();
var_dump($users['myusername']);
?>

 

http://www.php.net/spl

 

Look at the ArrayObject.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.