Jump to content

Selective MySQL queries versus arrays (accessing data)


Tjk

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.