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

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

  • 写回答

2条回答 默认 最新

  • douxuzui4590 2014-06-20 18:17
    关注

    Seems like you should set single quotes around your data values. Also adding a mysqli_error check for your mysqli_query line so you can actually see what is happening:

    $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) or die(mysqli_error());
    }
    

    Or better yet, use mysqli_stmt_bind_param like this. Allow MySQLi to deal with the whole query data structuring instead of having to worry about single quote placement. Also, added a check for mysqli_connect_error on your mysqli_connect line:

    // Connecting, selecting database
    $mysqli = mysqli_connect('localhost', 'root', '', '') or die(mysqli_connect_error());
    
    $allData = $dataSource->getAllData();
    foreach ($allData as $key => $value) {
        // Set the query.
        $query = "INSERT INTO `table`(`data_id`, `name`) VALUES (?, ?)";
    
        // Bind the params.
        // mysqli_stmt_bind_param($query, 'ss', $value['data_id'], $value['name']);
        mysqli_stmt_bind_param($query, 'is', $value['data_id'], $value['name']);
    
        // Run the query.
        $result = mysqli_query($mysqli, $query) or die(mysqli_error());
    }
    

    Note that I have a commented line for the mysqli_stmt_bind_param since it’s not clear to me if your $value['data_id'] is a number or a string. The mysqli_stmt_bind_param($query, 'is',… means the first value is an integer (i) and the next value is a string (s). Feel free to adjust to best fit your actual data types.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里