tommytx Posted October 29, 2015 Share Posted October 29, 2015 (edited) When I build an array manually then apply the php command extract all is well.. however when I query a data base in mysql the extract does not work.. I do notice a slight difference when viewing the print_r version.. but not sure if that is causing a problem. You can see an image of the resutls of the run here: http://screencast.com/t/0JZMLQQV71uand you can actually run the program here: http://foreclosure-city.info/wp-content/plugins/idx-plus/tom-test.php Here is the code I am having problems with: <?phprequire("../../../wp-load.php");global $wpdb;define('WP_DEBUG', TRUE);$my_array = array("option_id" => "1", "option_name" => "siteurl", "option_value" => "http://foreclosure-city.info");print "<pre>";print_r($my_array);print "</pre>";extract($my_array);echo "\$option_id = $option_id; \$option_name = $option_name; \$option_value = $option_value";echo "<br><h1>Begin part II of the test...</h1><br>"; The 3 variables were blanked here to get rid of the values from the first part of the program..$option_id="";$option_name="";$option_value="";$siteurl = $wpdb->get_results("SELECT * FROM `wp_options` WHERE `option_name` LIKE 'siteurl'");print "<pre>";print_r($siteurl);print "</pre>";extract($siteurl);echo "\$option_id = $option_id; \$option_name = $option_name; \$option_value = $option_value"; Edited October 29, 2015 by tommytx Quote Link to comment https://forums.phpfreaks.com/topic/298897-why-manual-array-works-with-extract-but-auto-download-does-not/ Share on other sites More sharing options...
tommytx Posted October 29, 2015 Author Share Posted October 29, 2015 this image shows differences that I noted in the two print_r outputs.. however i have no idea if this is relevant..http://screencast.com/t/nTwy1gGg Quote Link to comment https://forums.phpfreaks.com/topic/298897-why-manual-array-works-with-extract-but-auto-download-does-not/#findComment-1524639 Share on other sites More sharing options...
benanamen Posted October 29, 2015 Share Posted October 29, 2015 (edited) IMO using extract is lazy, makes the code hard to read and it can make it difficult to know where your variables came from. The very same issues that we had with register_globals which has rightfully been removed from php. Just dont use it. Edited October 29, 2015 by benanamen 1 Quote Link to comment https://forums.phpfreaks.com/topic/298897-why-manual-array-works-with-extract-but-auto-download-does-not/#findComment-1524640 Share on other sites More sharing options...
0x00 Posted October 29, 2015 Share Posted October 29, 2015 How does this work? extract($siteurl[0]); The db returns an array of row objects, I think!? ** Never seen or used extract() and not sure how WP returns results... Quote Link to comment https://forums.phpfreaks.com/topic/298897-why-manual-array-works-with-extract-but-auto-download-does-not/#findComment-1524641 Share on other sites More sharing options...
tommytx Posted October 29, 2015 Author Share Posted October 29, 2015 (edited) Do you have other simple ways of getting that data.. I tried that unlazy man's way and it worked fine for the manual input but not for the autofor example here is the result..for the manual array.....my_array[option_id] = 1my_array[option_name] = siteurlmy_array[option_value] = http://foreclosure-city.infofor the automated array....siteurl[option_id] =siteurl[option_name] =siteurl[option_value] =so can you see anything i am doing wrong or offer any useful suggestions that might help me... I must not be doing something right.. Edited October 29, 2015 by tommytx Quote Link to comment https://forums.phpfreaks.com/topic/298897-why-manual-array-works-with-extract-but-auto-download-does-not/#findComment-1524642 Share on other sites More sharing options...
tommytx Posted October 29, 2015 Author Share Posted October 29, 2015 (edited) I think I tried extract(siteurl[0] but let me do it again.. just to make sure... thanks for the suggestion..Nope siteurl[0] did not work.. but not sure if that would change the variable names...and yes.. extract works great on wordpress.. normally .. have used it a lot.. and even now its working great when I manually build the array myself.. it is auto naming the varialbes like below:it takes the key and makes it the variable name... like if key = a and variable = horse then it becomes $a = "horse" so each key value adds the dollar sign and becomes the variable name and in the variable is the actual variable.. really slick.. Edited October 29, 2015 by tommytx Quote Link to comment https://forums.phpfreaks.com/topic/298897-why-manual-array-works-with-extract-but-auto-download-does-not/#findComment-1524643 Share on other sites More sharing options...
benanamen Posted October 29, 2015 Share Posted October 29, 2015 (edited) really slick Not really slick, really bad programming practice. Edited October 29, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/298897-why-manual-array-works-with-extract-but-auto-download-does-not/#findComment-1524645 Share on other sites More sharing options...
0x00 Posted October 29, 2015 Share Posted October 29, 2015 I think I tried extract(siteurl[0] but let me do it again.. just to make sure... thanks for the suggestion.. Nope siteurl[0] did not work.. but not sure if that would change the variable names... and yes.. extract works great on wordpress.. normally .. have used it a lot.. and even now its working great when I manually build the array myself.. it is auto naming the varialbes like below: it takes the key and makes it the variable name... like if key = a and variable = horse then it becomes $a = "horse" so each key value adds the dollar sign and becomes the variable name and in the variable is the actual variable.. really slick.. As Benanamen says, bad practice! So, you have a set of variables (an array or object say), what you are essentially doing is 1) redeclaring the same variables, e.g. doubling up memory usage and wasting CPU cycles, 2) magically declaring new variables which to the casual reader will look like they are appearing out of thin air undeclared, and all for what, to be slick... personally clarity and efficiency are more desirable attributes to me, especially when revising code 3yrs later... Quote Link to comment https://forums.phpfreaks.com/topic/298897-why-manual-array-works-with-extract-but-auto-download-does-not/#findComment-1524646 Share on other sites More sharing options...
tommytx Posted October 29, 2015 Author Share Posted October 29, 2015 My friend you already told me I was lazy and a horrible programmer... so let it go.. if you have nothing useful to say please say nothing.. and speakng of 281 posts and a member less that 30 days means nearly 10 posts per day.. I certanily hope you are not just throwing out NON useful comments just to advance your status...looks like the goal is to get as many posts as possible so you look advanced... so please offer useful help or go away.. thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/298897-why-manual-array-works-with-extract-but-auto-download-does-not/#findComment-1524647 Share on other sites More sharing options...
benanamen Posted October 29, 2015 Share Posted October 29, 2015 (edited) The fact that you consider the correct information NON useful shows a complete unwillingness to learn and get better. You obviously didn't even take two seconds to google what hundreds to thousands of other programmers say about using extract. You are going to have a long hard road ahead of you. Good luck getting anyone to help you. * I dont need to "look" advanced. My posts speak for themselves as to my level of expertise. Edited October 29, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/298897-why-manual-array-works-with-extract-but-auto-download-does-not/#findComment-1524648 Share on other sites More sharing options...
QuickOldCar Posted October 29, 2015 Share Posted October 29, 2015 I'll vouch for benanamen knows what saying and not trying to build posts. Quote Link to comment https://forums.phpfreaks.com/topic/298897-why-manual-array-works-with-extract-but-auto-download-does-not/#findComment-1524653 Share on other sites More sharing options...
Solution Ch0cu3r Posted October 29, 2015 Solution Share Posted October 29, 2015 @tommytx the reason extract is not working is because by default $wpdb->get_results() returns the query results as an object. extract requires an (associative) array. You need to set the second argument (called output type) for $wpdb->get_results() to ARRAY_A. See documentation on various output types here: https://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results I do agree with benanamen comment though. Quote Link to comment https://forums.phpfreaks.com/topic/298897-why-manual-array-works-with-extract-but-auto-download-does-not/#findComment-1524661 Share on other sites More sharing options...
tommytx Posted October 29, 2015 Author Share Posted October 29, 2015 (edited) Thanks for the information of about the Array A the original program writer chimed in and gave me that very same info... but thanks anyway.. it is working great now... I am sure that when enough folks complain about the extract being a bad command.. they will eventually get around to removing it... meanwhile.. for a very simple ten line program I will contine to use it .. since it is so much faster and simpler to extract the data line by line... Edited October 29, 2015 by tommytx Quote Link to comment https://forums.phpfreaks.com/topic/298897-why-manual-array-works-with-extract-but-auto-download-does-not/#findComment-1524748 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.