我想将以下数据保存到MySQL数据库中。
以下代码向我显示了两个警告
1-非法字符串偏移。
2-只能通过引用传递变量。
请帮忙。
$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();
}
答案 0 :(得分:2)
我认为这会奏效,但在发布之前我个人会想要更强大的东西。
$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();
答案 1 :(得分:0)
首先从数组值中删除尾随点(。)
$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();
`