0

I wanna pass a variable, but always get the error message

Invalid argument supplied for foreach()...

Here is my code:

function second($taxonomies)
{
    global $wpdb;
    $result = $wpdb->get_results( "SELECT * FROM tblTest WHERE manufacturer = $d", $taxonomies);
    foreach ($result as $pointer) {
    $id = $pointer->id;
    $car = $pointer->car;
    $price = $pointer->price;
    echo $id." ".$car." ".$price."<br />";
    }  
}

And how can I pass the result to another drop down? Thanks in advance!

4

As you can see in the Codex pages the get_results doesn't provide the functionality to add a value parameter in the method:

$wpdb->get_results('query', output_type);

output_type has the following description:

One of four pre-defined constants. Defaults to OBJECT. See SELECT a Row and its examples for more information.

  • OBJECT - result will be output as a numerically indexed array of row objects.
  • OBJECT_K - result will be output as an associative array of row objects, using first column's values as keys (duplicates will be discarded).
  • ARRAY_A - result will be output as an numerically indexed array of associative arrays, using column names as keys.
  • ARRAY_N - result will be output as a numerically indexed array of numerically indexed arrays.

You could use the following:

$results = $wpdb->get_results(
  $wpdb->prepare(
    "
    SELECT *
     FROM tblTast
     WHERE manufacturer = %d
    ",
    $taxonomies
  )
);

if ( $results ) {
  foreach ( $results as $pointer ) {
    /* your code here */
  }
}

We'll add the 'prepare' method to make sure we're not dealing with an illegal operation or any illegal characters

See the Codex for more information.

|improve this answer|||||
0
$result = $wpdb->get_results(
    $wpdb->prepare(
        "SELECT * FROM tblTest WHERE manufacturer = %d",
        $taxonomies
    )
);

See the codex on wpdb::prepare()

|improve this answer|||||
  • Thanks, but it looks like my variable is empty, cause I don'T get a result. – user35227 Jul 12 '13 at 9:22
  • Here is my code to call the function. So why is my variable empty? <form action="<?php bloginfo('url'); ?>" method="get"> <?php $taxonomies = 'Mustang'; $select = second($taxonomies); echo $select; ?> <noscript><div><input type="submit" value="Go" /></div></noscript> </form> What I also will do, but don't know how: To read the taxonomy from my site and compare it with my database query, the results will be shown. – user35227 Jul 12 '13 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.