将数组内容插入数据库-PHP [英] Insert array content into database - php

查看:178
本文介绍了将数组内容插入数据库-PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在数据库的每一行中插入变量$ num,它是一个数组。我想在数据库的每一行中插入每个数组的位置。

I'm trying to insert in each row of database the variable $num which is an Array. I want to insert each array position in each row of database.

这就是我的代码,此刻正在插入单词 Array:

That's the code I have, and in the moment it is inserting the word "Array":

foreach($html2->find('small[class=menu-item__label__count]') as $aholder) {

$holderdes=$aholder->outertext;
}
preg_match_all('!\d+!', $holderdes, $num);
//preg_match_all('/(\([0-9]+)\)/',  $holderdes,$num);

echo print_r($num); 
echo("<script>console.log('PHP: ".$num."');</script>");
$insert="INSERT into spec_insert(designers) VALUES ('".$num."');";

$sql=mysql_query($insert);

if ($sql === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " ;
}
}


推荐答案

如果您想将每个数组值插入数据库表 spec_insert 的特定列,然后可以使用 foreach()来检索数组的索引值。例子

If you want to insert each array value to a specific column of DB table spec_insert then can use foreach() to retrieve the index value of array. Example

foreach($num as $val):
    if(is_array($val)):
        foreach($val as $v):
            $insert="INSERT into spec_insert(designers) VALUES ('".$v."');";
            $sql=mysql_query($insert);
        endforeach;
    else:
        $insert="INSERT into spec_insert(designers) VALUES ('".$val."');";
        $sql=mysql_query($insert);
    endif;
endforeach;

这篇关于将数组内容插入数据库-PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆