Page 1 of 2

Array problems in PHP 4...

Posted: Sun Aug 16, 2009 8:09 pm
by Wolf_22
The following code works fine in my dev station running PHP 5, but I just uploaded it to a server with PHP 4 and it broke. I believe it has something to do with the object in use:

Code: Select all

function array_combine($arr1, $arr2) {
    $out = array();
    $arr1 = array_values($arr1);
    $arr2 = array_values($arr2);
    foreach($arr1 as $key1 => $value1) {
        $out[(string)$value1] = $arr2[$key1];
    }  
    return $out;
}
 
function get_vtl_user_roles(){
    global $vtl_roles,$vtldb;
    $this_role = "'[[:<:]]administrator[[:>:]]'";
    $query = "SELECT DISTINCT id, user_login FROM $vtldb->users WHERE ID = ANY (SELECT DISTINCT user_id FROM $vtldb->usermeta WHERE meta_value RLIKE 'administrator') ORDER BY ID ASC LIMIT 10000";
    $result = $vtldb->get_results($query);
    foreach ($result as $id=>$user) {
        $partA[] = $user->user_login;
        $partB[] = $user->id;
    } 
    $users_of_this_role = array_unique(array_combine($partA,$partB));
    return $users_of_this_role;
}

I think where this thing bombs out is where $partA[] = $user->user_login... Is there an easy way around this?

Re: Array problems in PHP 4...

Posted: Sun Aug 16, 2009 8:16 pm
by aceconcepts
Do you get an error? If so what is it?

Re: Array problems in PHP 4...

Posted: Sun Aug 16, 2009 8:18 pm
by Wolf_22
Warning: array_values() [function.array-values]: The argument should be an array in /home/tutlib/public_html/vtl_content/plugins/to-do-list/to-do-list.php on line 31

Warning: array_values() [function.array-values]: The argument should be an array in /home/tutlib/public_html/vtl_content/plugins/to-do-list/to-do-list.php on line 32

Warning: Invalid argument supplied for foreach() in /home/tutlib/public_html/vtl_content/plugins/to-do-list/to-do-list.php on line 33
(Line 31 is "$arr1 = array_values($arr1);")

Re: Array problems in PHP 4...

Posted: Sun Aug 16, 2009 8:20 pm
by AlanG
Could you post up the code for the get_results() function in the $vtldb object? Or better yet, the entire file?

Re: Array problems in PHP 4...

Posted: Sun Aug 16, 2009 8:25 pm
by aceconcepts
Why do you use array_values()?

Re: Array problems in PHP 4...

Posted: Sun Aug 16, 2009 8:31 pm
by Wolf_22
aceconcepts wrote:Why do you use array_values()?
The function that resides in was something I got from the PHP manual from one of the comments that discussed issues with array_combine in PHP 4. The poster of that function mentioned that it fixed that and when I tried it on the server, it seemed to work as the array_combine issue was the first problem I ran into prior to what I'm running into now. Should I use something else?

Re: Array problems in PHP 4...

Posted: Sun Aug 16, 2009 8:42 pm
by aceconcepts
If you use arr_combine() then I guess it depends on how you've implemented it.

Re: Array problems in PHP 4...

Posted: Sun Aug 16, 2009 8:55 pm
by redmonkey
Have you checked and confirmed that '$vtldb->get_results($query);' actually does return a result array? Does the database on the server support sub queries?

Re: Array problems in PHP 4...

Posted: Sun Aug 16, 2009 8:57 pm
by aceconcepts
I ask about array_cmbine() because it was introduced for PHP 5.

You can use it in PHP 4 but with a work-around.

Re: Array problems in PHP 4...

Posted: Sun Aug 16, 2009 9:02 pm
by Wolf_22
I found this function and so far, it fixed the problem:

Code: Select all

function arr_combine($arr1,$arr2){
                if(!is_array($arr1))
                        $arr1 = array();
                if(!is_array($arr2))
                        $arr2 = array();
                $keys1 = array_keys($arr1);
                $keys2 = array_keys($arr2);
                $keys  = array_merge($keys1,$keys2);
                $vals1 = array_values($arr1);
                $vals2 = array_values($arr2);
                $vals  = array_merge($vals1,$vals2);
                $ret    = array();
 
                foreach($keys as $key)
                {
                        list($unused,$val) = each($vals);
                        $ret[$key] = $val;
                }
 
            return $ret;
        }
Credit goes to someone who posted over on the array_merge() manual comments section.

Re: Array problems in PHP 4...

Posted: Sun Aug 16, 2009 9:05 pm
by aceconcepts
The foreach is the main reason for this work around.

Nice one :D

Re: Array problems in PHP 4...

Posted: Sun Aug 16, 2009 9:38 pm
by Wolf_22
redmonkey wrote:Have you checked and confirmed that '$vtldb->get_results($query);' actually does return a result array? Does the database on the server support sub queries?
It's funny you mention this because after applying the workaround, I noticed that I'm having problems getting things to and from the database. Is this going to require a consult with the systems admin or is there yet again another workaround for this too?

Re: Array problems in PHP 4...

Posted: Sun Aug 16, 2009 10:58 pm
by Wolf_22
I changed the query to the following:

Code: Select all

SELECT id, user_login FROM $vtldb->users INNER JOIN $vtldb->usermeta ON $vtldb->users.id = $vtldb->usermeta.user_id WHERE $vtldb->usermeta.meta_value = 'administrator' ORDER BY id ASC LIMIT 10000
And I'm still not getting anything from the MySQL database. Have I done something wrong here?

Re: Array problems in PHP 4...

Posted: Sun Aug 16, 2009 11:18 pm
by aceconcepts
Have you looked at mysql_error()?

Re: Array problems in PHP 4...

Posted: Mon Aug 17, 2009 8:02 am
by Wolf_22
mysql_error() doesn't appear to be throwing any errors and when I use the MySQL Console to type out that query (to test it), it comes back saying that it's returned an empty set. This leads me to believe that this new query is not picking up the tables / columns that I am trying to get it to retrieve (unless there's some sort of compatibility issue at hand).

AlanG: Here is the function definition of get_results() -

Code: Select all

 
    function get_results($query = null, $output = OBJECT) {
        $this->func_call = "\$db->get_results(\"$query\", $output)";
 
        if ( $query )
            $this->query($query);
        else
            return null;
 
        if ( $output == OBJECT ) {
            // Return an integer-keyed array of row objects
            return $this->last_result;
        } elseif ( $output == OBJECT_K ) {
            // Return an array of row objects with keys from column 1
            // (Duplicates are discarded)
            foreach ( $this->last_result as $row ) {
                $key = array_shift( get_object_vars( $row ) );
                if ( !isset( $new_array[ $key ] ) )
                    $new_array[ $key ] = $row;
            }
            return $new_array;
        } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
            // Return an integer-keyed array of...
            if ( $this->last_result ) {
                $i = 0;
                foreach( (array) $this->last_result as $row ) {
                    if ( $output == ARRAY_N ) {
                        // ...integer-keyed row arrays
                        $new_array[$i] = array_values( get_object_vars( $row ) );
                    } else {
                        // ...column name-keyed row arrays
                        $new_array[$i] = get_object_vars( $row );
                    }
                    ++$i;
                }
                return $new_array;
            }
        }
    }
Here is the code to the query function, too:

Code: Select all

    function query($query) {
        if ( ! $this->ready )
            return false;
 
        // filter the query, if filters are available
        // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
        if ( function_exists('apply_filters') )
            $query = apply_filters('query', $query);
 
        // initialise return
        $return_val = 0;
        $this->flush();
 
        // Log how the function was called
        $this->func_call = "\$db->query(\"$query\")";
 
        // Keep track of the last query for debug..
        $this->last_query = $query;
 
        // Perform the query via std mysql_query function..
        if ( defined('SAVEQUERIES') && SAVEQUERIES )
            $this->timer_start();
 
        $this->result = @mysql_query($query, $this->dbh);
        ++$this->num_queries;
 
        if ( defined('SAVEQUERIES') && SAVEQUERIES )
            $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
 
        // If there is an error then take note of it..
        if ( $this->last_error = mysql_error($this->dbh) ) {
            $this->print_error();
            return false;
        }
 
        if ( preg_match("/^\\s*(insert|delete|update|replace|alter) /i",$query) ) {
            $this->rows_affected = mysql_affected_rows($this->dbh);
            // Take note of the insert_id
            if ( preg_match("/^\\s*(insert|replace) /i",$query) ) {
                $this->insert_id = mysql_insert_id($this->dbh);
            }
            // Return number of rows affected
            $return_val = $this->rows_affected;
        } else {
            $i = 0;
            while ($i < @mysql_num_fields($this->result)) {
                $this->col_info[$i] = @mysql_fetch_field($this->result);
                $i++;
            }
            $num_rows = 0;
            while ( $row = @mysql_fetch_object($this->result) ) {
                $this->last_result[$num_rows] = $row;
                $num_rows++;
            }
 
            @mysql_free_result($this->result);
 
            // Log number of rows the query returned
            $this->num_rows = $num_rows;
 
            // Return number of rows selected
            $return_val = $this->num_rows;
        }
 
        return $return_val;
    }