tjodolv Posted September 18, 2007 Share Posted September 18, 2007 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 https://forums.phpfreaks.com/topic/69821-solved-mysql-arrays-and-how-to-handle-them/ Share on other sites More sharing options...
cooldude832 Posted September 18, 2007 Share Posted September 18, 2007 Query for it and then you have an array called $row ($row = mysql_fetch_array($result) where $result is a mysql query. However if you are drawing multiple rows you will need to use a while loop to get all the data. This isi very basic php/mysql and must tutorials will help you Link to comment https://forums.phpfreaks.com/topic/69821-solved-mysql-arrays-and-how-to-handle-them/#findComment-350737 Share on other sites More sharing options...
solarisuser Posted September 18, 2007 Share Posted September 18, 2007 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 https://forums.phpfreaks.com/topic/69821-solved-mysql-arrays-and-how-to-handle-them/#findComment-350745 Share on other sites More sharing options...
Fadion Posted September 18, 2007 Share Posted September 18, 2007 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 https://forums.phpfreaks.com/topic/69821-solved-mysql-arrays-and-how-to-handle-them/#findComment-350748 Share on other sites More sharing options...
tjodolv Posted September 19, 2007 Author Share Posted September 19, 2007 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 https://forums.phpfreaks.com/topic/69821-solved-mysql-arrays-and-how-to-handle-them/#findComment-350760 Share on other sites More sharing options...
cooldude832 Posted September 19, 2007 Share Posted September 19, 2007 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 https://forums.phpfreaks.com/topic/69821-solved-mysql-arrays-and-how-to-handle-them/#findComment-350763 Share on other sites More sharing options...
tjodolv Posted September 19, 2007 Author Share Posted September 19, 2007 Ah, ok. Didn't know that about MySQL. Thanks for the help Link to comment https://forums.phpfreaks.com/topic/69821-solved-mysql-arrays-and-how-to-handle-them/#findComment-350769 Share on other sites More sharing options...
cooldude832 Posted September 19, 2007 Share Posted September 19, 2007 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 https://forums.phpfreaks.com/topic/69821-solved-mysql-arrays-and-how-to-handle-them/#findComment-350801 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.