我有一个php文件(带有simple_HTML_Dom),它抓取了CSV文件的所有网址。
他提取了我需要的所有信息,一切正常,但现在我不想在MySQL桌面上添加所有这些结果。
我希望每个结果在MySQL表中添加一行。
这是我到目前为止的代码:
<?php
require 'libs/simple_html_dom/simple_html_dom.php';
set_time_limit(0);
function scrapUrl($url)
{
$html = new simple_html_dom();
$html->load_file($url);
$names = $html->find('h1');
$manufacturers = $html->find('h2');
foreach ($names as $name) {
echo $name->innertext;
echo '<br>';
}
foreach ($manufacturers as $manufacturer) {
echo $manufacturer->innertext;
echo '<br>';
echo '<hr><br>';
}
}
$rutaCSV = 'csv/urls1.csv'; // Ruta del csv.
$csv = array_map('str_getcsv', file($rutaCSV));
//print_r($csv); // Verás que es un array donde cada elemento es array con una de las url.
foreach ($csv as $linea) {
$url = $linea[0];
scrapUrl($url);
}
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
foreach ($csv as $linea) {
$sql = "INSERT INTO productos (nombre, nombreFabricante) VALUES($name, $manufacturer)";
print ("<p> $sql </p>");
if ($conn->query($sql) === TRUE) {
echo "Items added to the database!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>
编辑:我已更新代码。我还添加了价格变量。 输出中显示的错误是:
Notice: Undefined variable: name in C:\xampp\htdocs\bootstrap\csv2.php on line 69
Notice: Undefined variable: manufacturer in C:\xampp\htdocs\bootstrap\csv2.php on line 69
Error: INSERT INTO productos (nombre, nombreFabricante) VALUES(,)
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' 'https://url.com/es/product1.html', )' at line 1
如何解决通知的错误:未定义的变量:name&amp;价格?
我想将函数内部的$ name和$ manufacturer变量添加到我的MySQL表
编辑2:@adamlab,你删除了帖子:(
如果有人能帮助我,我会非常感激。
谢谢你,以及最好的问候
答案 0 :(得分:1)
我不确定您在哪里定义$ name,因此您需要检查它是否有效。
foreach ($csv as $linea) {
$sql = "INSERT INTO productos (name, manufacturer) VALUES('$name', '{$linea[0]}')";
print ("<p> $sql </p>");
if ($conn->query($sql) === TRUE) {
echo "Items added to the database!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>
答案 1 :(得分:0)
尝试使用此代码,希望您分别收集每个值,但是您需要将每组结果作为新行插入mysql表中,尝试下面但是根据您的需要进行粗略调整。
require 'libs/simple_html_dom/simple_html_dom.php';
set_time_limit(0);
function scrapUrl($url)
{
$ar_name =array();
$ar_man = array();
$html = new simple_html_dom();
$html->load_file($url);
$names = $html->find('h1');
$manufacturers = $html->find('h2');
foreach ($names as $name) {
$ar_name[] = $name->innertext;
}
foreach ($manufacturers as $manufacturer) {
$ar_man[] = $manufacturer->innertext;
}
for($i=o;$i<sizeof($ar_name);$i++)
{
$sql = "INSERT INTO table(name, manufacturer)
VALUES ($ar_name[$i],$ar_man[$i])";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
//echo $url;
}
$rutaCSV = 'csv/urls1.csv'; // Ruta del csv.
$csv = array_map('str_getcsv', file($rutaCSV));