gpt4 book ai didi

php - 如何将数组插入MySql数据库

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

我有一个 foreach 来详细说明我网站上的一些值,我想将它们插入到 mysql 数据库中。

我的问题是我有一个数组,因为我不知道在这种情况下如何处理它们。

这是我的代码:

   ...

$titles = $html->find("a[class=in-match]"); // 1 per match
$result = $html->find("td[class=h-text-center]/a"); // 1
$best_bets = $html->find("td[class=table-matches__odds colored]/span/span/span"); // 1
$odds = $html->find("td[class=table-matches__odds]"); // 2

function print_odd($odd) {
if (array_key_exists('data-odd', $odd->attr)) {
return $odd->attr['data-odd'];
}

return $odd->children(0)->children(0)->children(0)->attr['data-odd'];
}

$c=0; $b=0; $o=0; $z=0; // two counters
foreach ($titles as $match) {
list($num1, $num2) = explode(':', $result[$c++]->innertext); // <- explode
$num1 = intval($num1);
$num2 = intval($num2);
$num3 = ($num1 + $num2);
if ($num3 > 1) {
$over15 = "OK";
} else {
$over15 = "NO";
}

echo "<tr><td class='rtitle'>".
"<td class='first-cell'>".$match->innertext."</td> ".
"<td>".$match->innertext."</td><td> ".$num1.'</td><td> : </td><td>'.$num2 . " / " . // <- example use
"<td class='first-cell'>".$num3 ."</td> " .
"<td class='first-cell'>".$over15 ."</td> " .
"<td class='odds'>".print_odd($odds[$b++]) . ";" .
"".print_odd($odds[$b++]) . ";" .
"".print_odd($odds[$b++]) . "</td>" .
"</td></tr><br/>";


$servername = "xx.xxx.xxx.xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxxxxxxx";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO risultati (titles, scorehome, scoreaway, best_bets)
VALUES ('$match', '$num1', '$num2', '$odds');";

if ($conn->multi_query($sql) === TRUE) {
echo "New records created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();

当我运行这个时,我没有得到 3“$odds”

感谢您的关注

编辑 因此,我用代码保存了几乎所有数据,但 $odds 有问题(您可以看到我的函数 print_odd)。怎么才能拯救他们呢?

最佳答案

尝试这个概念...谢谢

$columns = implode(", ",array_keys($insData));
$escaped_values = array_map('mysql_real_escape_string', array_values($insData));
$values = implode(", ", $escaped_values);
$sql = "INSERT INTO `fbdata`($columns) VALUES ($values)";

关于php - 如何将数组插入MySql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41715785/

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