Jump to content

Why manual array works with extract but auto download does not?


Go to solution Solved by Ch0cu3r,

Recommended Posts

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/0JZMLQQV71u
and 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:
 

 

<?php

require("../../../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 by tommytx

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 by benanamen
  • Like 1

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 auto
for example here is the result..

for the manual array.....
my_array[option_id] = 1
my_array[option_name] = siteurl
my_array[option_value] = http://foreclosure-city.info

for 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 by tommytx

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 by tommytx

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

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

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 by benanamen
  • Solution

@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.

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 by tommytx
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.