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.

Member Avatar for diafol

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.

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();

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.