[英]How to insert an array into a MySql database
I have a foreach to elaborate some values from my web site and I'd like to insert them into a mysql database. 我有一个foreach来阐述我网站上的一些值,我想将它们插入到mysql数据库中。
My problem is where I have an array, because I don't know how to treat them in this case. 我的问题是我有一个数组,因为在这种情况下我不知道如何处理它们。
This is my code: 这是我的代码:
...
$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();
When I run this, I don't get that 3 "$odds" 当我运行它时,我不会得到那3个“ $ odds”
Thank you for your attention 感谢您的关注
EDIT So, I saved almost all data with my code but I have a problem with $odds (You can see my function print_odd). 编辑因此,我用代码保存了几乎所有数据,但是$ odds出现了问题(您可以看到我的函数print_odd)。 How could to save them? 如何保存它们?
Try the concept... Thanks 试试这个概念...谢谢
$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)";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.