如何将用逗号分隔的数据添加到数据库

时间:2018-09-03 07:34:57

标签: php mysql implode

我想将我的数据插入数据库。我的数据用逗号分隔。

我尝试这样,但看起来有点问题。

$rand_post = ["3001182708", "3001182713", "3001183215"]; 
$id_post = '123456';

$prep = array();
foreach($rand_post as $v ) {
    $prep[] = "($id_post, $v)";
}
print_r($prep);
$sth = $db->prepare("INSERT INTO tes (`id_post`,`datas`) VALUES " . implode(', ', $prep));
$res = $sth->execute($prep);

我想这样插入我的数据

id_post            rand_post
============       ================
123456             3001182708
123456             3001182713
123456             3001183215

2 个答案:

答案 0 :(得分:1)

您很亲密,但没有使用前置语句的功能来粘贴字符串。您准备查询,然后在循环中使用不同的参数执行查询。也可以命名。

$rand_post = ["3001182708", "3001182713", "3001183215"]; 
$id_post = '123456';

$prep = array();
$sth = $db->prepare("INSERT INTO tes (`id_post`,`datas`) VALUES (?, ?)");

foreach($rand_post as $v ) {
    $sth->execute(array($id_post, $v));
}

您可以找到更多信息here

答案 1 :(得分:0)

这应该有效

 // 00001010 - binary
 Console.WriteLine(Convert.ToString(result, 2).PadLeft(8, '0')); 
 // 10       - decimal
 Console.WriteLine(result);  
 // 0A       - hexadecimal
 Console.WriteLine(result.ToString("X2"));  
相关问题