problem with function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yshaf13
Forum Commoner
Posts: 72
Joined: Mon Apr 03, 2006 7:59 pm

problem with function

Post by yshaf13 »

Hi, I wrote a function to handle mysql queries, thing is, some times it works and sometimes not (it doesn't return anything, even though the sql is legal), and I can't figure out why!
here it is:

Code: Select all

function query(){//returns $queryresult, $error and $query* for each sql statement.
    
    global $queryresult, $error;
    
    
    $queries=func_get_args();
    foreach ($queries as $key => $value) {
        $qnum=$key +1;
        
        global ${"query{$qnum}"};
        
        
        if (!${"query{$qnum}"} = mysql_query($value)){ 
        $error++;
        echo '<br><font color="red"><strong>There was a problem with sql'.$qnum.':</strong></font><br>
        '.$value.'<br><font color="red"><strong>The error given was:</strong></font><br>'.mysql_error().'<br>';
    }else{
        $queryresult .="<br>Query$qnum ---$value--- OK<br>";}
        
        
    }
 
    if ($error==0){
        return true;
    
    }else { return false;}
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: problem with function

Post by John Cartwright »

That code is an absolute nightmare (no offense). Ignoring all the malpractice and global usage, what is the error what is given when the query fails? It would be helpful to see the queries that fail. Perhaps you should be logging (or displaying echo'ing if this is a test server) all the queries that do not work as expected.
yshaf13
Forum Commoner
Posts: 72
Joined: Mon Apr 03, 2006 7:59 pm

Re: problem with function

Post by yshaf13 »

gosh, is it that bad? in my defense i'm kinda new to the whole php thing, i just play around with it in my own time...
the idea is to be able to pass as many sql statements to the function and the function would return (make global) a variable called $query1 for the first $query2 for the second etc. which would hold the resultset for that query. when a query does go wrong the function should return false and echo the error information.... this is handled from line 13-21.... any ideas on how i would fix it up?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: problem with function

Post by shiznatix »

Problems with your code:
-Instead of using func_get_args(); you should pass and array to the function with your queries.
-Don't do this: global ${"query{$qnum}"};. Just, don't
-global = bad (99.99% of the time)

Do something like this instead:

Code: Select all

 
function query($queries = array())
{
    if (count($queries))
    {
        $return = array();
 
        foreach ($queries as $query)
        {
            $checker = mysql_query($query);
 
            if (!$checker)
            {
                $return = mysql_error();
                break;
            }
 
            $return[] = $checker;
        }
    }
    else
    {
        $return = 'No queries passed. Fail.';
    }
 
    return $return;
}
 
remember that all code i write is 100% untested and I am drunk so take it as you get it. Anyway, my code is super simpler than your code and will return either an array of all the result sets or a string with the error message in it.
yshaf13
Forum Commoner
Posts: 72
Joined: Mon Apr 03, 2006 7:59 pm

Re: problem with function

Post by yshaf13 »

i hope you get over the hangover alright!
the reason i did it the way i did is because that function used to be written completely different - very un-abstract (is there a word for that?) and i have implemented it in many of my scripts, and those scripts pass variables and expect to receive resultsets like $query1... so to change every implementation would be a real pain... any way to save it?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: problem with function

Post by Jonah Bron »

Just make the whole thing a class, and instead of filling in a global variable, make it fill in a class state. (like so)

Code: Select all

 
class Query {
    public $returnQuery = false;
    public function query($queries = array()){
        if (count($queries)){
            $return = array();
            foreach ($queries as $query){
                $checker = mysql_query($query);
                if (!$checker){
                    $return = mysql_error();
                    break;
                }
                $return[] = $checker;
            }
        }else{
            $return = 'No queries passed. Fail.';
        }
        return $return;
    }
}
$query = new Query();
$query->query('some query');
$query1 = $query->returnQuery;
 
I suggest reading the PHP manual section on Classes and Objects
And learning about Object Oriented Programming in PHP
And chaining in classes

Have fun
Post Reply