Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter mirandms

    (@mirandms)

    3) To create your associative array of variables from the string passed to get_categories(), use a loop and strtok() to parse the string and determine the ‘name’=>’value’ array members, instead of calling parse_str().

    It might be informative to mention that we found the problem in the function get_categories() in wp-includes/category.php

    We encountered this error and isolated the problem.

    This is due to a bug in older versions of PHP.

    parse_str() advertises that it will store variables in an array if an array is passed as the optional second parameter, INSTEAD of creating the variables in the current scope.

    What we actually observed was that it created the array AS WELL AS creating the variables in the current scope.

    This behavior causes the extract() call (with EXTR_SKIP) to NOT create the orderby variable because, due to the bug in parse_str() it already exists, and because EXTR_SKIP is specified, extract WILL NOT overwrite the value in the orderby variable.

    Here is some code that exposes the problem:

    <pre>
    <?
    $args = "type=link&orderby=name&order=ASC&hierarchical=0";
    echo "Test string \$args = \"$args\"\n";
    echo "\$orderby before call to parse_str() = $orderby\n";
    echo "Calling parse_str(\"$args\", \$arr)\n";
    parse_str($args, $arr);
    echo "\$orderby after call to parse_str() = $orderby\n";
    echo "\$arr = "; print_r ($arr); echo "\n\nAdd prefix to a value in \$arr array\n";
    $arr['orderby'] = "cat_" . $arr['orderby'];
    echo "\$arr = "; print_r ($arr); echo "\n\nCall extract(\$arr, EXTR_SKIP)\n";
    extract($arr, EXTR_SKIP);
    echo "\$arr = "; print_r ($arr); echo "\n";
    echo "orderby after call to extract(\$arr, EXTR_SKIP) = $orderby\n";
    ?>
    </pre>

    NOTE: This bug has been fixed in later versions of PHP, so depending upon the version you are running, the code above may execute correctly (meaning parse_str() won’t create variables in scope)

    If you are seeing this error:

    [Unknown column ‘name’ in ‘order clause’]
    SELECT * FROM wp_categories WHERE cat_ID > 0 AND link_count > 0 ORDER BY name ASC

    then more than likely this is the problem. There are a few simple ways to work around this. We will post a fix later.

Viewing 3 replies - 1 through 3 (of 3 total)