在codeigniter中将json数据推送到数据库
Posted
技术标签:
【中文标题】在codeigniter中将json数据推送到数据库【英文标题】:push json data to database in codeigniter 【发布时间】:2013-08-29 15:56:33 【问题描述】:因此,我的网站解析 JSON 数据,并将其分解为来自 NY Times API 的单独报道。
我正在尝试将这些单独的故事加载到我的 mysql 数据库中。
下面的代码运行页面但出现错误:
A Database Error Occurred
You must use the "set" method to update an entry.
Filename: /Applications/MAMP/htdocs/topline/controllers/index.php
Line Number: 29
在下面取出我的 api 密钥。
型号
<?php
class Api_model extends CI_Model
public function __construct()
// End of construct
public $offset;
public function get_news()
$offset = 0;
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://api.nytimes.com/svc/mostpopular/v2/mostviewed/all-sections/1.json?offset=" . $offset . "&api-key=mykey");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$Obj = json_decode($output);
return $Obj;
// End of function
// End of class
?>
查看
<section>
<?php
$paginate = site_url('/pageup');
//loop through json response items
foreach ($articles->results as $article)
//error reporting turned off for this process because some objects have more than 1 photo
error_reporting(0);
echo "<div class='story'>";
echo "<div class='photo'>";
//pass image into an img tag
echo "<img src='" . $article->media[0]->"media-metadata"[0]->url . "' />";
echo "</div>";
echo "<div class='story-text'>";
//call the story headline
echo "<h3>" . $article->title . "</h3>";
//call the description
echo "<p>" . $article->abstract . "</p>";
//call the link to the full story
echo "<p class='btn btn-primary'><a href='" . $article->url . "' target='_blank'>Read More</a></p>";
echo "</div>";
echo "</div>";
//setup database array
$article_data = array(
'id' => 1,
'image' => $article->media[0]->"media-metadata"[0]->url,
'body' => $article->abstract,
'title' => $article->title,
'link' => $article->url
);
//var_dump($articles->results[0]);
?>
<p><a href="<?php echo $paginate; ?>" class="btn btn-block">Load More Stories</a></p>
</section>
控制器
<?php
class Index extends CI_Controller
public function __construct()
parent::__construct();
session_start();
public function index()
$data['title'] = "Topline Press";
// Load the model
$this->load->model('Api_model');
// Call the method of the model
$data['articles'] = $this->Api_model->get_news();
// Load the views
$this->load->view('header', $data);
$this->load->view('start', $data);
$this->load->view('footer', $data);
//push story data into database
$this->db->insert('stories', $article_data);
// End of class
?>
在这方面相当新,所以我很感激任何帮助。提前致谢。
【问题讨论】:
【参考方案1】:您必须在插入 db 之前设置值:
$this->db->set('column_in_stories_table', $value_to_place_in_table);
$this->db->insert('stories');
// Produces: INSERT INTO stories (column_in_stories_table) VALUES ('$value_to_place_in_table')
如果需要,您还可以将模型对象从控制器存储到数据库中:
/*
class Api_model
$title = 'My Title';
$content = 'My Content';
$date = 'My Date';
*/
$obj = new Api_model;
$this->db->set($obj);
$this->db->insert('stories');
【讨论】:
是的 - 现在似乎可以正常工作了。谢谢你的解释。 @user2673468 如果答案有帮助并解决了您的问题,您应该通过单击旁边的绿色复选标记将其标记为“已接受”,这让社区知道您的问题现已解决以上是关于在codeigniter中将json数据推送到数据库的主要内容,如果未能解决你的问题,请参考以下文章
如何将 JSON 数据发布到 PubSub,然后推送到 BigQuery?
如何在一个数据库命中中将多个插入或更新推送到一起 - 对于多数据库(SQL Server、Oracle、MySQL 和 PostgreSQL)应用程序