Merdok Posted December 17, 2009 Share Posted December 17, 2009 Hi Guys, I'm trying to streamline my CMS's code and as I was writing a new page it occured to me that there might be an easier way to get variables from a mysql query and that if anyone knew how to do it, it would be you guys. Here is the code I use now: // Pulls the data from the database $dblookup = "SELECT moduleID, menu_name, stylesheet_name, display_name, has_menu, access_control, installed, compatible, active FROM core_modules WHERE moduleID=" . $_GET['ID']; $data = mysql_query($dblookup) or die('Failed to return data: ' . mysql_error()); /* sorts the data into variables and puts them in an array ready to be called when needed */ list($moduleID, $menu_name, $stylesheet_name, $display_name, $has_menu, $access_control, $installed, $compatible, $active) = mysql_fetch_array($data, MYSQL_NUM); } Is there a way I can just do SELECT * FROM and then have it automatically extract the data into variables based on the column name? Quote Link to comment https://forums.phpfreaks.com/topic/185454-simpler-method-of-getting-variables-from-mysql/ Share on other sites More sharing options...
Adam Posted December 17, 2009 Share Posted December 17, 2009 The extract function may be what you're looking for? Quote Link to comment https://forums.phpfreaks.com/topic/185454-simpler-method-of-getting-variables-from-mysql/#findComment-979102 Share on other sites More sharing options...
Merdok Posted December 17, 2009 Author Share Posted December 17, 2009 Oh yeah that does look like the kind of thing I'm after. Cheers mate, i'll look into it. Quote Link to comment https://forums.phpfreaks.com/topic/185454-simpler-method-of-getting-variables-from-mysql/#findComment-979527 Share on other sites More sharing options...
PFMaBiSmAd Posted December 17, 2009 Share Posted December 17, 2009 You should use the EXTR_PREFIX_ALL 2nd parameter and specify a unique prefix as the 3rd parameter to insure you don't overwrite any existing program variables (someone recently had that happen here in the Forum.) Quote Link to comment https://forums.phpfreaks.com/topic/185454-simpler-method-of-getting-variables-from-mysql/#findComment-979528 Share on other sites More sharing options...
Merdok Posted December 22, 2009 Author Share Posted December 22, 2009 oh yeah thats a good idea, it means re-writing a lot of my code but it sounds like a worthy reason. thank you Quote Link to comment https://forums.phpfreaks.com/topic/185454-simpler-method-of-getting-variables-from-mysql/#findComment-982263 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.