I use below code create and insert record in my table in wordpress database

global $jal_db_version;
$jal_db_version = "1.0";

function jal_install() {
   global $wpdb;
   global $jal_db_version;

   $table_name = $wpdb->prefix . "anncurtis";

   $sql = "CREATE TABLE " . $table_name . " (
      id mediumint(9) NOT NULL AUTO_INCREMENT,
      Ability VARCHAR(25) NOT NULL,
      Session VARCHAR(60) NOT NULL,
      Time VARCHAR(50) NOT NULL,
      Itemname VARCHAR(90) NOT NULL,
          Itemid int(8) NOT NULL,
          Childname VARCHAR(50) NOT NULL,
      UNIQUE KEY id (id)
    );";

   require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
   dbDelta($sql);

   add_option("jal_db_version", $jal_db_version);
}


function jal_install_data($ablty,$sesion,$tiime,$itm_name,$itm_id,$chld_name) {
   global $wpdb;
   $table_name = $wpdb->prefix . "anncurtis";

   $rows_affected = $wpdb->insert( $table_name, array( 'Ability' => $ablty, 'Session' => $sesion, 'Time' => $tiime,'Itemname' => $itm_name,'Itemid' => $itm_id,'Childname' => $chld_name ) );
}

Now i used below code for to fetch the records,

$table_name = $wpdb->prefix . "anncurtis";
$fivesdrafts = $wpdb->get_results(
    "SELECT * FROM $wpdb->$table_name"
);

foreach ( $fivesdrafts as $fivesdraft )
{
    echo $fivesdraft->Ability;
}

But i can't fetch the records. what is the problem?.

How can i fetch the records from my own table which is created in wordpress database.?

link|improve this question

43% accept rate
Update the post with what you get for print_r( $fivesdraft ), also, are you sure your records are making it into your database? Use a tool like phpMyAdmin to verify the data is there and that the table exists, etc. – m0r7if3r Jan 31 at 12:45
feedback

closed as too localized by Chip Bennett, kaiser, EAMann Jan 31 at 18:05

This question is unlikely to ever help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. See the FAQ for guidance on how to improve it.

2 Answers

Try escaping the variable in your select statement - different versions of PHP treat it differently but generally with objects it's better to do either:

"SELECT * FROM {$wpdb->$table_name}"

or

"SELECT * FROM " . $wpdb->$table_name

would be more robust ways of writing that query.

Also double check that the table name is what you think it should be.

link|improve this answer
i try both the codes. i got error like " WordPress 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 * FROM " – Ravichandran Jan 31 at 13:00
feedback
up vote 0 down vote accepted

I got an answer. i used below one its working.

global $wpdb;
echo $table_name = $wpdb->prefix."anncurtis";
$fivesdrafts = $wpdb->get_results(
"SELECT * FROM ".$table_name
);

foreach ( $fivesdrafts as $fivesdraft )
{

    print_r($fivesdraft);
}
link|improve this answer
feedback

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