使用php在MySQL中保存json数据

时间:2022-10-22 20:46:33

I want to save my following data into MySQL Database.

我想将以下数据保存到MySQL数据库中。

following code is showing me two warnings

以下代码向我展示了两个警告

1- Illegal string offset.

1-非法字符串偏移。

2- Only variables can be passed by reference in.

2-只有变量可以通过引用传递。

Please help.

$db = new PDO('mysql:host=localhost;dbname=whois;charset=utf8mb4', 'root', '');
$items = array(
        'Domain Name:' => 'domain.name',
        'Domain ID:' => 'domain.handle',
        'Sponsoring Registrar:' => 'domain.sponsor',
        'Registrar ID:' => 'domain.sponsor',
        'Domain Status:' => 'domain.status.',
        'Status:' => 'domain.status.',
        'Name Server:' => 'domain.nserver.',
        'Nameservers:' => 'domain.nserver.',
        'Maintainer:' => 'domain.referer',
);

$stmt = $db->prepare("INSERT INTO temdata VALUES(?,?,?,?,?,?,?,?,?)");

foreach ($items as $row) {

    $stmt->bindParam(1, $row['domainname']);
    $stmt->bindParam(2, $row['domainid']);
    $stmt->bindParam(3, $row['registrar']);
    $stmt->bindParam(4, $row['registrarid']);
    $stmt->bindParam(5, $row['domainstatus']);
    $stmt->bindParam(6, $row['status']);
    $stmt->bindParam(7, $row['server']);
    $stmt->bindParam(8, $row['nameservers']);
    $stmt->bindParam(9, $row['maintainer']);
    $stmt->execute();
}

2 个解决方案

#1


2  

I think this will work, but I personally would want something more robust before I released it.

我认为这会有效,但在我发布它之前,我个人会想要更强大的东西。

$items = array(
        'Domain Name:' => 'domain.name',
        'Domain ID:' => 'domain.handle',
        'Sponsoring Registrar:' => 'domain.sponsor',
        'Registrar ID:' => 'domain.sponsor',
        'Domain Status:' => 'domain.status.',
        'Status:' => 'domain.status.',
        'Name Server:' => 'domain.nserver.',
        'Nameservers:' => 'domain.nserver.',
        'Maintainer:' => 'domain.referer',
);

$stmt = $db->prepare("INSERT INTO temdata VALUES(?,?,?,?,?,?,?,?,?)");
$col=1;
foreach ($items as $item) {
    $stmt->bindParam($col, $item);
    $col++;
}
$stmt->execute();

#2


0  

First of all remove trailing dot (.) from array values

首先从数组值中删除尾随点(。)

$items = array(
    'Domain Name:' => 'domain.name',
    'Domain ID:' => 'domain.handle',
    'Sponsoring Registrar:' => 'domain.sponsor',
    'Registrar ID:' => 'domain.sponsor',
    'Domain Status:' => 'domain.status',
    'Status:' => 'domain.status',
    'Name Server:' => 'domain.nserver',
    'Nameservers:' => 'domain.nserver',
    'Maintainer:' => 'domain.referer',
);
$stmt = $db->prepare("INSERT INTO temdata VALUES(?,?,?,?,?,?,?,?,?)");

$i = 1;
foreach ($items as $row) {
    $stmt->bindParam($i, $row);
    $i++;
}

$stmt->execute();
`

#1


2  

I think this will work, but I personally would want something more robust before I released it.

我认为这会有效,但在我发布它之前,我个人会想要更强大的东西。

$items = array(
        'Domain Name:' => 'domain.name',
        'Domain ID:' => 'domain.handle',
        'Sponsoring Registrar:' => 'domain.sponsor',
        'Registrar ID:' => 'domain.sponsor',
        'Domain Status:' => 'domain.status.',
        'Status:' => 'domain.status.',
        'Name Server:' => 'domain.nserver.',
        'Nameservers:' => 'domain.nserver.',
        'Maintainer:' => 'domain.referer',
);

$stmt = $db->prepare("INSERT INTO temdata VALUES(?,?,?,?,?,?,?,?,?)");
$col=1;
foreach ($items as $item) {
    $stmt->bindParam($col, $item);
    $col++;
}
$stmt->execute();

#2


0  

First of all remove trailing dot (.) from array values

首先从数组值中删除尾随点(。)

$items = array(
    'Domain Name:' => 'domain.name',
    'Domain ID:' => 'domain.handle',
    'Sponsoring Registrar:' => 'domain.sponsor',
    'Registrar ID:' => 'domain.sponsor',
    'Domain Status:' => 'domain.status',
    'Status:' => 'domain.status',
    'Name Server:' => 'domain.nserver',
    'Nameservers:' => 'domain.nserver',
    'Maintainer:' => 'domain.referer',
);
$stmt = $db->prepare("INSERT INTO temdata VALUES(?,?,?,?,?,?,?,?,?)");

$i = 1;
foreach ($items as $row) {
    $stmt->bindParam($i, $row);
    $i++;
}

$stmt->execute();
`