Passing variables trouble.

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

This is weird and I can't explain it in any way how this is happening :scratchhead:

In my site's database access wrapper class, I have the following two functions...
PHP Code: [ Select ]
<?php
function build_key_query($sql)
{
    // Checking if $sql is an array
    if(!is_array($sql))
    {
        trigger_error('The variable <strong>$sql</strong> is not an array ', E_USER_ERROR);
    }
   
    // Checking if $sql is a valid array
    if(!$this->rkey_exists($sql, $this->sql_queries))
    {
        trigger_error('The variable <strong>$sql</strong> is not a valid array ', E_USER_ERROR);
    }
   
    // Getting the allowed SQL functions
    $haystack = $this->gen_allowed();
    // var_dump($sql);
    // Retrieving the first key in the array
    $key = $this->retaval($sql, 0);
 
    // Checking if the SQL function used is allowed
    if(!in_array($key, $haystack)) // if($all_funcs[$key] == 0)  <- Backup way
    {
        trigger_error($key . ' is not allowed to be used in SQL', E_USER_ERROR);
    }
   
    // Initiating the result variable
    $bquery = null;
   
    // Looping through the SQL array and creating the query
    foreach($sql as $type => $query)
    {
        $key = $this->key_as_value($query);
        if(is_array($query) && ($type == 'WHERE' || $type == 'where'))
        {
            $bquery .= $this->build_where($query);
        }
        else
        {
            $bquery .= ' ' . strtoupper($type) . ' ' . strtolower($query);
        }
    }
   
    // Checking if the built query is of correct format
    if($this->valid_query($bquery))
    {
        // Returning the build query
        return trim($bquery);
    }
    else
    {
        return false;
    }
}
function retaval($array, $akey, $array = false,  $value = false, $numeric = false)
{
    // Setting some variables
    $i = 0;
    $num = count($array);
    // var_dump($array);
    // Looping though the array and choosing the key/value that was asked for
    foreach($array as $key => $val)
    {
        // Setting the return value to the current key/value
        if(!is_false($value) && is_false($array))
        {
            $return = ((is_false($value)) ? $key : $val);
        }
       
        if(!is_false($array))
        {
            $key = ((is_false($numeric)) ? $key : $i);
            $return[$key] = $val;
        }
       
        // Making sure we stop when we have the correct key/value
        if($i == $akey)
        {
            break;
        }
       
        // Incrementing the counter
        ++$i;
    }
   
    //  Returning the return value if there is any
    return (($i > $num) ? false : $return);
}
?>
  1. <?php
  2. function build_key_query($sql)
  3. {
  4.     // Checking if $sql is an array
  5.     if(!is_array($sql))
  6.     {
  7.         trigger_error('The variable <strong>$sql</strong> is not an array ', E_USER_ERROR);
  8.     }
  9.    
  10.     // Checking if $sql is a valid array
  11.     if(!$this->rkey_exists($sql, $this->sql_queries))
  12.     {
  13.         trigger_error('The variable <strong>$sql</strong> is not a valid array ', E_USER_ERROR);
  14.     }
  15.    
  16.     // Getting the allowed SQL functions
  17.     $haystack = $this->gen_allowed();
  18.     // var_dump($sql);
  19.     // Retrieving the first key in the array
  20.     $key = $this->retaval($sql, 0);
  21.  
  22.     // Checking if the SQL function used is allowed
  23.     if(!in_array($key, $haystack)) // if($all_funcs[$key] == 0)  <- Backup way
  24.     {
  25.         trigger_error($key . ' is not allowed to be used in SQL', E_USER_ERROR);
  26.     }
  27.    
  28.     // Initiating the result variable
  29.     $bquery = null;
  30.    
  31.     // Looping through the SQL array and creating the query
  32.     foreach($sql as $type => $query)
  33.     {
  34.         $key = $this->key_as_value($query);
  35.         if(is_array($query) && ($type == 'WHERE' || $type == 'where'))
  36.         {
  37.             $bquery .= $this->build_where($query);
  38.         }
  39.         else
  40.         {
  41.             $bquery .= ' ' . strtoupper($type) . ' ' . strtolower($query);
  42.         }
  43.     }
  44.    
  45.     // Checking if the built query is of correct format
  46.     if($this->valid_query($bquery))
  47.     {
  48.         // Returning the build query
  49.         return trim($bquery);
  50.     }
  51.     else
  52.     {
  53.         return false;
  54.     }
  55. }
  56. function retaval($array, $akey, $array = false,  $value = false, $numeric = false)
  57. {
  58.     // Setting some variables
  59.     $i = 0;
  60.     $num = count($array);
  61.     // var_dump($array);
  62.     // Looping though the array and choosing the key/value that was asked for
  63.     foreach($array as $key => $val)
  64.     {
  65.         // Setting the return value to the current key/value
  66.         if(!is_false($value) && is_false($array))
  67.         {
  68.             $return = ((is_false($value)) ? $key : $val);
  69.         }
  70.        
  71.         if(!is_false($array))
  72.         {
  73.             $key = ((is_false($numeric)) ? $key : $i);
  74.             $return[$key] = $val;
  75.         }
  76.        
  77.         // Making sure we stop when we have the correct key/value
  78.         if($i == $akey)
  79.         {
  80.             break;
  81.         }
  82.        
  83.         // Incrementing the counter
  84.         ++$i;
  85.     }
  86.    
  87.     //  Returning the return value if there is any
  88.     return (($i > $num) ? false : $return);
  89. }
  90. ?>

Now, it works so good until it get's to the first appearance of retaval in build_key_query. The variable $sql is obviously an associative array... and when I pass it to retaval... retaval receives it as a boolean 'false'... what the hell?

I did var_dump($sql) in build_key_query() and it showed as an array filled up correctly... when I did var_dump($array) in retaval I got bool(false) as the result... I don't get it. It used to work perfectly until now... This is confusing and annoying and irritating...

I also tried doing the following:
Code: [ Select ]
$this->retaval(array('SELECT' => 'style', 'FROM' => 'table1'), 0);
And it's still giving me the bool(false) thing :banghead:

How do I stop this magic from happening?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

lol omg... wow... I'm stupid... don't be with me... :oops:

Oh sh*t! wow... I declared $array twice in the function and the last one was set to false... jeeze... sh*t!

Nevermind... this problem is fixed... I'm now tackling another one though :( Hopefully it's not as stupid as this one :oops:

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 56 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.