我编写代码将数据保存在Wordpress数据库表中(表名为wp\\U fafa)
但无法在中保存数据
$qry = $wpdb->query( "INSERT INTO `wp_fafa` (titile,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) . "\')");
$wpdb->query($qry);
最合适的回答,由SO网友:amir rasabeh 整理而成
以下代码将在中正确存储数据wp_fafa
:
$wpdb->insert( $wpdb->prefix . \'fafa\',
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\'
)
);
SO网友:mikemanger
确保已声明$wpdb变量global $wpdb;
. 也在呼叫$wpdb->query()
将为您执行查询,因此无需再次执行。
我还建议使用$wpdb->insert()
由于这会为您转义数据:
global $wpdb;
$wpdb->insert(
$wpdb->prefix . \'fafa\',
array(
\'titile\' => 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 ),
)
);