0

I am developing in WordPress plugins and I would like to know.if someone could guide
Me with this issue. I would like to know what's the easier method.of declaring a global variable that I can use. As of now I am not using classes, mostly because i'm not sure how to just yet. I just have a bunch of functions. I am finding myself writing out the same SQL query in multiple functions. How can I store my database results one time in a variable that I can call like this :

Global $results;

Similar to how I call global $wpdb;

When calling global $wpdb, am I actually calling a class or a variable? I'm just a little confused in all of this. Thanks.

3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by drumichael87
0

I'd pass them to other functions via parameter as opposed to using global vars:

function x($res){
    ...
}

function y(){
    ....
    $result = mysql_query(...);
    $p = x($result);
    ...
}

Slightly different for class methods.
May not be suitable for all cases though.

0

Hi,

You can do like this :

$query = "select name, phone from clients where id = 1";
$result = mysql_fetch_array(mysql_query($query));

function show_name(){
    global $result;
    echo " client name : " . $result['name'];
} 
function show_phone(){
    global $result;
    echo " client phone : " . $result['phone'];
} 

show_name();
show_phone();
0

Thanks so much. I can believe its as easy as just Declaring the variable outside of the other functions. :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.