php - Extracted data/text from a webpage is not inserted into mysql db -


i trying insert text extracted webpage not inserted db. using xpath expression extract data , data on webpage within multiple html para or list item tags.

here code

<?php set_time_limit(0); $dbhost = "localhost"; $dbuser = "root"; $dbpass = ""; $dbname = "olx"; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("error connecting database"); mysql_select_db($dbname, $conn); $res1 = mysql_query("select * `item_url` id=10"); while($r1 = mysql_fetch_array($res1)) {     $url = $r1['url'];      $html = file_get_contents($url);            $doc = new domdocument();      @$doc->loadhtml($html);          $xpath = new domxpath($doc);      $details = $xpath->evaluate("//div[@id='description-text']/child::div");     foreach ($details $detail) {         $nodes = $detail->childnodes;         foreach ($nodes $node) {         $string = $node->nodevalue;         $string = preg_replace('/[^a-za-z0-9@.\-]/', ' ', $string); //allow required character         $string = strip_tags($string); //remove html tags          echo $string . '<br>';         }     }    mysql_query("insert `test` (`detail`) values ('$string')") or die(mysql_error());  }  ?> 

it displays data in way

performs skilled technical work in maintenance, repair, replacement, , installation of air conditioning systems.  installs, troubleshoots , repairs air conditioning units.  replaces expansion valves, compressors, motors, coil units , other component parts.  technicians work in residential homes, schools, hospitals, office buildings, or factories. 

can't insert data db.is issue of xpath nodes.each line within

tag on webpage.

below html of webpage

<div id="description-text">     <h2 class="title-desc">     <span>ad details</span>     </h2>     <ul class="item-optionals">     <li style="background-color: rgb(251, 251, 251);">     </ul>   <div style="padding-right: 30px; width: 388px;">       <p> performs skilled technical work in maintenance, repair, replacement, , installation of air conditioning systems.</p>       <p>installs, troubleshoots , repairs air conditioning units.</p>        <p>replaces expansion valves, compressors, motors, coil units , other component parts.</p>        <p>technicians work in residential homes, schools, hospitals, office buildings, or factories.</p>     </div> </div> 

your code fine, problem called mysql_query @ end of loop processes single nodes of html page, solve it's enough bring mysql_query call in internal foreach loop.

<?php set_time_limit(0); $dbhost = "localhost"; $dbuser = "root"; $dbpass = ""; $dbname = "olx"; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("error connecting database"); mysql_select_db($dbname, $conn); $res1 = mysql_query("select * `item_url` id=10"); while($r1 = mysql_fetch_array($res1)) {     $url = $r1['url'];      $html = file_get_contents($url);            $doc = new domdocument();      @$doc->loadhtml($html);          $xpath = new domxpath($doc);      $details = $xpath->evaluate("//div[@id='description-text']/child::div");     foreach ($details $detail) {         $nodes = $detail->childnodes;         foreach ($nodes $node) {         $string = $node->nodevalue;         $string = preg_replace('/[^a-za-z0-9@.\-]/', ' ', $string); //allow required character         $string = strip_tags($string); //remove html tags          echo $string . '<br>';         mysql_query("insert 'test' ('detail') values ('$string')") or die(mysql_error());         }     }   } ?> 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -