drwghu6386
2014-06-20 18:13
浏览 298
已采纳

php使用mysqli从数组插入mysql

hi i want to insert some data from a parsed json to a table, but when i do it, it doesn't work, it returns 0 rows, what am i missing? i'm new yet with this "mysqli".. i have more than 25000 rows to insert to the table. thx

$mysqli = mysqli_connect('localhost', 'root', '', '');

$allData = $dataSource->getAllData();
foreach ($allData as $key => $value) {
    $query = 'INSERT INTO `table`(`data_id`, `name`) VALUES (' . $value['data_id'] . ', ' . $value['name'] . ')';
    $result = mysqli_query($mysqli, $query);
}

it works now, it inserted all the object data, the code that i made thx to replies is something like this:

$mysqli = mysqli_connect('localhost', 'root', '', '') or die(mysqli_connect_error());
if (!$mysqli) {
    die('Could not connect: ' . mysqli_error());
}

$allData = $dataSource->getAllData();

foreach ($allData as $key => $value) {
    $query = mysqli_prepare($mysqli, "INSERT INTO `table`(`data_id`, `name`) VALUES (?, ?)");
    mysqli_stmt_bind_param($query, 'is', $value['data_id'], $value['name']);
    mysqli_stmt_execute($query);
    mysqli_stmt_close($query);
}

hope everything is ok here, i'm new in this mysqli and i need a lot of practice with programming

图片转代码服务由CSDN问答提供 功能建议

嗨我想将一些数据从解析后的json插入到表中,但是当我这样做时,它并没有 工作,它返回0行,我错过了什么? 我是新的还有这个“mysqli”..我有超过25000行插入到表中。 thx

  $ mysqli = mysqli_connect('localhost','root','',''); 
 
 $ allData = $ dataSource-> getAllData()  ; 
foreach($ allData as $ key => $ value){
 $ query ='INSERT INTO`table`(`data_id`,`name`)VALUES('。$ value ['data_id']。',  '。$ value ['name']。')'; 
 $ result = mysqli_query($ mysqli,$ query); 
} 
   
 
 

它现在有效,它插入了所有的对象数据,我对回复的代码是这样的:

  $ mysqli = mysqli_connect('localhost  ','root','','')或死(mysqli_connect_error()); 
if(!$ mysqli){
 die('无法连接:'。mysqli_error()); 
} 
 <  / code>  
 
 

$ allData = $ dataSource-&gt; getAllData();

  foreach($ allData as $ key =&gt;  $ value){
 $ query = mysqli_prepare($ mysqli,“INSERT INTO`table`(`data_id`,`name`)VALUES(?,?)”); 
 mysqli_stmt_bind_param($ query,'is',$  value ['data_id'],$ value ['name']); 
 mysqli_stmt_execute($ query); 
 \ mysqli_stmt_close($ query); 
  } 
   
 
 

希望这里一切正常,我是这个mysqli的新手,我需要大量的编程练习

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题