Store HTML DOM results into MySQL database
P粉549986089
P粉549986089 2023-09-15 23:41:06
0
1
400

I'm trying to store the scraped data into my database. It just outputs the results, so the crawling is normal. But there is definitely an error in defining the variable and passing it to mysql-insert. I get the message "New record created successfully". The result of the variable is empty and the date exists.

loadHTML($html); libxml_clear_errors(); $scriptDOMXPath = new DOMXPath($scriptDocument); $scriptRow = $scriptDOMXPath->query('//th[starts-with(text(), "ISSUES:")]//following::td[6]');if($scriptRow->length > 0){foreach($scriptRow as $row){echo $row->nodeValue;}} // echo result works $scriptRow = $scriptDOMXPath->query('//th[starts-with(text(), "ISSUES:")]//following::td[6]');if($scriptRow->length > 0){foreach($scriptRow as $row){$row->nodeValue = $nasdaq_dec;}} // defining variable does not work }; $host_name = ''; $database = ''; $user_name = ''; $password = ''; try { $conn = new PDO("mysql:host=$host_name; dbname=$database;", $user_name, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO `morgenroutine` (date,nasdaq_dec) VALUES (NOW(), '$nasdaq_dec')"; $conn->exec($sql); echo "成功创建新记录"; } catch(PDOException $e) { echo $sql . "
" . $e->getMessage(); } $conn = null; ?>
P粉549986089
P粉549986089

reply all (1)
P粉269530053

now I understand! I changed the format to "text" in mysql and changed the row to:$scriptRow = $scriptDOMXPath->query('//th[starts-with(text(), "ISSUES:")]/following::td[6]');if($scriptRow->length > 0){foreach($scriptRow as $row){$nasdaq_dec = $row->nodeValue;}}

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!