0

Firstly i created a custom table into the wpdb :

  global $wpdb;
  $table_name = $wpdb->prefix . 'countdown_table';

  $sql = '
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `styleName` varchar(255) NOT NULL,
  `css` blob NOT NULL,
  `expiration` blob NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
  ';

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

Then i created a form to send values into the custom table :

<p>Style Name</p>   

<form action="" method="POST">

<input type="text" name="style_name">
<input type="submit" name="submit">

</form>

if (isset($_POST['style_name'])) {

$style_name = $_POST['style_name'];

 global $wpdb;

    $wpdb->insert( 
        'countdown_table', 
        array( 
            'styleName' => $style_name 
        ), 
        array( 

            '%s' 
        ) 
    );

}

And here is the problem to get results from the table :

 global $wpdb;

    $output = '';

    $table_name = $wpdb->prefix . 'countdown_table';

    $name = 'styleName';

    $results = $wpdb->get_results( $wpdb->prepare('SELECT * FROM '.$table_name ) );

    if ( $results ) {
       foreach ( $results as $result ){
       echo $result[0];
    }

The problem is that i cannot display the saved data or do i did something wrong in the code for table creation or inserting data

3
  • Check your Database setup as by now it's pretty much guessing. Please update your question then. Thanks. Also please add some context to your code. It's unclear where and when this code executes which could be a problem of its own.
    – kaiser
    Nov 17, 2014 at 20:13
  • Seems really unlikely you should need a custom table for this. Especially one using blobs.
    – vancoder
    Nov 17, 2014 at 20:17
  • Since you have set all field to NOT NULL wont you have to add the other fields in your insert query? Nov 17, 2014 at 21:47

1 Answer 1

1

It seems you issue is with the below code piece:

if ( $results ) {
    foreach ( $results as $result ){
       echo $result[0];
    }
}

echo $result[0]; should just be echo $result;.

$results is your array so to use $results[0] (note the s) you would need a for loop and not a foreach loop.

if ( $results ) {

    $arrLen = count($results);

    for ($i = 0; $i < $arrLen; $i++ ){
       echo $result[$i];
    }
}

is what you want if you want to use $results[n] targeting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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