views:

28

answers:

1

Hi. I'm trying to display all the values from a specific column created by my Wordpress plugin (specifically, the ID's). Here is the code I have managed to use to display the column names, but I cannot get it to just display all the ID's. Here is the code:

function test() {
    global $wpdb;
    global $table_name;
    $testing = $wpdb->get_col_info('name', 0);
    foreach ($testing as $test) {
      echo $test;
    }
}

And here you can see the output:

www.matthewruddy.com/premiumslider

Can anyone help me out?

A: 

It seems you want data instead of column information. So you need to another function.

$testing = $wpdb->get_results("SELECT id FROM mytable")
foreach ($testing as $test) {
    echo $test->id
}

EDIT:

OK, how about this one:

$wpdb->show_errors();
echo 'Listing from table: $table_name<br>';
$ids = $wpdb->get_col($wpdb->prepare("SELECT id FROM %s", $table_name));
if ($ids) {
  echo 'printing results:<br>';
  foreach($ids as $id) {
    echo $id;
  }
} else {
  echo 'no results or error<br>';
  echo 'error: ' . $wpdb->print_error();
}
$wpdb->hide_errors();

If this doesn't help you either to get your ID's or to figure out what the error is, I am lost.

littlegreen
This outputs nothing..
Matthew Ruddy
Did you replace 'mytable' with the contents of $table_name? And is your column indeed called 'id' in the database or something else.
littlegreen
I replaced my table with $table_name and above that; global $table_name. This works for all other instances. And yes it is called 'id'.
Matthew Ruddy
I posted a new one, hopefully this works. $wpdb->prepare is not strictly necessary but will help you against SQL injection attacks.
littlegreen
Here is what I get: Listing from table: $table_nameWordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '''' at line 1]SELECT id FROM ''
Matthew Ruddy
Nevermind. Managed to get it sorted. Didn't have $table_name defined properly. My bad. Sorry. Thanks a million anyway :)
Matthew Ruddy