0

I write code to extract peice from website with curl. this should be refresh page to get price.

I want store data in the wordpress database with foreach.

when I refreshing page

but give this error.

Call to a member function insert() on a non-object in C:\wamp\www\wordpress\wp-content\plugins\price\fras-coin.php on line 73

database

foreach($table_rows as $tr) { // foreach row
     $row = $tr->childNodes;
    if($row->item(0)->tagName != 'tblhead') { // avoid headers
        $data[] = array(
             $trip ['Name' ]= trim($row->item(0)->nodeValue),
             $trip['LivePrice'] = trim($row->item(2)->nodeValue),
             $trip ['Changing']=  trim($row->item(4)->nodeValue),
             $trip ['Lowest']=  trim($row->item(6)->nodeValue),
             $trip['Topest']=  trim($row->item(8)->nodeValue),
             $trip['Time']=  trim($row->item(10)->nodeValue),

          );
       }
       }
    global $wpdb;
    foreach($table_rows as $tr) { // foreach row
    $row = $tr->childNodes;
    if($row->item(0)->tagName != 'tblhead') { // avoid headers
        $wpdb->insert( $wpdb->farsc, 
        array( 
        'title' => " . trim($row->item(0)->nodeValue) . ",
        'liveprice' => " . trim($row->item(2)->nodeValue) . ",
        'changing' => " . trim($row->item(4)->nodeValue) . ",
        'lowest' => " . trim($row->item(6)->nodeValue) . ",
        'topest' => " . trim($row->item(8)->nodeValue) . ",
        'time' => " . trim($row->item(10)->nodeValue) . " ), 
        array( 
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s'
     ) );
        ///$sql = "INSERT INTO `fars_coin`(title,liveprice,changing,lowest,topest,time) VALUES ('" . trim($row->item(0)->nodeValue) . "','" . trim($row->item(2)->nodeValue) . "','" . trim($row->item(4)->nodeValue) . "','" . trim($row->item(6)->nodeValue) . "','" . trim($row->item(8)->nodeValue) . "','" . trim($row->item(10)->nodeValue) . "')";
        /// 
        ///mysql_query($sql);

    }
}
  • Where exactly is this code being called? It's likely that $wpdb hasn't been created yet, thus the error message. If the code is not called by a hook, I'd recommed implementing that (init is a good one to use - codex.wordpress.org/Plugin_API/Action_Reference/init). – David Gard Jan 30 '15 at 10:40
  • As you are inserting data using multiple queries, can I also suggest that you check out the TRANSACTION START and COMMIT/ROLLBACK MySQL commands. This functionality will allow you to check the $wpdb object for errors after every inset, and if necessary ROLLBACK the queries. If there is no error, you can COMMIT them all at the end of your function - dev.mysql.com/doc/refman/5.0/en/commit.html. – David Gard Jan 30 '15 at 10:45

Your Answer

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

Browse other questions tagged or ask your own question.