Jump to content

[SOLVED] MySQL, Arrays and how to handle them...?


tjodolv

Recommended Posts

Hello, and thanks for reading :)

 

I am practicing my php skills, and i decided to write a simple CMS. I was planning to use a table in the database to store all the settings, but how do I go about retrieving them in a single query to an array?

 

For example, i have a table like so:

----------------------------------------

propertyvalue

allow_guest_commentsyes

maintenance_modeno

file_uploadsno

----------------------------------------

And so on and so forth.

 

My question is: how do I retrieve this information to an array that makes it easy to use it further on?

For example:

<?php
if ($settings['allow_guest_comments'] == 1) {
     // show the comment form
}
?>

Or something along those lines..

Link to comment
Share on other sites

Any beginner MySQL + PHP book out there , in addition to google, will give you the answer.

 

example:

 

$username = $_SESSION['the_username'];

$result = mysql_query("SELECT file_uploads,field2,field3 FROM tables WHERE username = '$username'");

$row = mysql_fetch_array($result))

 

$file_upload = $row['file_uploads'];

 

if($file_upload == 1) { echo "You have file upload permission!<br><br><input type=file blah blah>"; }

Link to comment
Share on other sites

U can pass the database fields values to an array but it will keep querying every time the page is refreshed. I find it better to use a config file with constants ex:

 

define('guest_comments', 'no');

define('file_uploads', 'yes');

 

include that file and use those constants in code. To modify those values (which probably will need admin access on the site) u can write to that config file.

Link to comment
Share on other sites

The file approach could work, I guess.

 

I knew how to get the specific thing I wanted from the DB, maybe I was a bit unclear in my question. I would like to get all the values at once, a "SELECT * FROM table" and put it in an array I can reuse, to minimize the amount of queries executed every time the page is loaded.

Link to comment
Share on other sites

Minimizing queries doesn't mean anything if you make one huge query that gets to much data.  Mysql is a fininky beast.  You want to get all the data you need and nothing more or less at a time.  So using the * from Table could be a devisating query, whats best is to use select all needed fields where values match and limit to nubmer of results needed. The limit section is generally reserved for pagnation type deals, however if you are hunting for a single row applying a limit 1 to it will help the software know once it finds a match to stop.

Link to comment
Share on other sites

and in the case of pagination 2 queries is actually better than 1. 

in a 2 query system query 1 gets the count of results query 2 gets the data to display based on the pagination

 

However in a 1 query system a single query captures all data and the and you have to sort out only that page of results.  It uses less queries, but a lot more data is gotten that is un used

Link to comment
Share on other sites

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.