php - 将数组值插入 MySQL

标签 php mysql

我试图找到另一个问题的答案,但我没有运气。我的问题基本上是……这行得通吗?

$insert_tweets = "INSERT INTO tweets (
  'id',
  'created_at',
  'from_user_id',
  'profile_image',
  'from_user',
  'from_user_name',
  'text' 
  ) VALUES (
  {$user_data[$i]["id"]},
  {$user_data[$i]["created_at"]},
  {$user_data[$i]["from_user_id"]},
  {$user_data[$i]["profile_image"]},
  {$user_data[$i]["from_user"]},
  {$user_data[$i]["from_user_name"]},
  {$user_data[$i]["text"]}
)"

        for($i=0;$i<count($user_data);$i++){
            mysqli_query($mysqli,$insert_tweets);
        }

$user_data是一个多维数组,第一层是数值型,后面是关联型。

此外,在插入之前“准备数据库”/清理关联数组变量的最佳方法是什么?我预计不会有任何恶意数据,但它始终是可能的。

最佳答案

        for($i=0;$i<count($user_data);$i++){
            $insert_tweets = "INSERT INTO tweets ('id','created_at','from_user_id','profile_image','from_user','from_user_name','text') VALUES ({$user_data[$i]["id"]},{$user_data[$i]["created_at"]},{$user_data[$i]["from_user_id"]},{$user_data[$i]["profile_image"]},{$user_data[$i]["from_user"]},{$user_data[$i]["from_user_name"]},{$user_data[$i]["text"]})";
            mysqli_query($mysqli,$insert_tweets);
        }

这应该可行

关于php - 将数组值插入 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11680774/

相关文章:

php - 如何在 symfony 布局中填充侧边栏内容?

php - WebSocket 消息前面的 [] 字符?

php - 如何在 PHP/MySQL 中将 ","读取为 "<br/>"?

php - MySQL查询货币格式

sql - 合并具有相同名称的 2 列的结果,并且不覆盖其中一列

php - Laravel 中的过滤关系

php - 将图像数据 URI 转换为图像 PHP

php - 使用 php 从 sql 数据库下载后,我得到一个空的 excel 文件

MySQL 在单次插入中插入 20K 行

php - 将日期保存到 MySQL