gpt4 book ai didi

php - 使用php在mysql数据库中插入数组

转载 作者:行者123 更新时间:2023-11-29 19:05:45 25 4
gpt4 key购买 nike

目前,我有一个通过 print_r() 输出时如下所示的数组;

Array ( [0] => Array ( [0] =>
Sri Chandrasekharendra Saraswathi Viswa Mahavidyalaya

| Kanchipuram, Chennai
[1] =>
Karpaga Vinayaga College of Engineering and Technology

| Kanchipuram, Chennai
[2] =>
SRM University, Chennai - Kattankulathur Campus

| Kattankulathur, Chennai
[3] =>
Saveetha University, Thandalam

| Thandalam, Chennai
[4] =>
B.S. Abdur Rahman Crescent University

| Vandalur, Chennai
[5] =>
P.B. College of Engineering

| Sriperumbudur, Chennai
[6] =>
Saveetha Engineering College

| Thandalam, Chennai
[7] =>
Hindustan University

| Kelambakkam, Chennai

我想将此数据插入到一个表中,其中每个元素值属于其各自的字段。

实际上我从网站上抓取数据。我使用了一些正则表达式。获取的所有数组每个都包含 30 个值。所有数组都包含所需的结果,但我无法弄清楚如何将它们插入数据库。即使你的方法是也不为我工作使用的 for 循环是:

for ($i=0;$i<30;$i++)
{ $temp1 = $names[1][$i];
$temp2 = $location[2][$i];
$temp3 = $facilities[1][$i];
$temp4 = $reviews[1][$i];





$sql = 'INSERT INTO collegedb (Name,Location,Facilities,Reviews) VALUES ("$temp1","$temp2","$temp3","$temp4")';
$run=mysqli_query($dbconnect,$sql);}

有人可以帮助我了解我应该如何处理这种情况吗?这是完整代码的链接 http://paste.ubuntu.com/24422534/

最佳答案

@Clearshot66 的答案很适合你的问题,所以这是额外的:如果你曾经考虑转向比 mysqli 更好的 PDO,这可能会帮助你:

    $req = $conn->prepare('INSERT INTO document (nom,date_creation,prix,auteur) VALUES (:nom, :date_creation, :prix
, :auteur)');
$req->execute(array('nom' => $document->getNom(), 'date_creation' => $document->getDateCreation()
, 'prix' => $document->getPrix(), 'auteur' => $document->getAuteur()));

在 protected 数据库中插入 Document 类对象的简单示例。祝你好运。

关于php - 使用php在mysql数据库中插入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43528594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com