Conflict between Simple_HTML_Dom and non-object-oriented functions

6

I'm developing an app that has to access a list of sites saved in a database, loading all its links. It is a test application but I have encountered a difficulty. The routine is this one:

  function crawler() {
      include_once './simple_html_dom.php';

      //Coloquei este registro em um vetor para dar um exemplo
      $sites = array("http://www.folhavitoria.com.br/");

      //se eu descomentar a linha abaixo o erro acontece:
      // $conecta = mysql_connect("localhost", "root", "");

      foreach ($sites as $url){
        $html = new simple_html_dom($url);
        echo "<br>".$url."</br>";

        foreach($html->find("a") as $link){
            echo $link->href."<br/>";
        }
        unset($html);
      }
  }

Basically what this routine returns to me is a series of links from inside the main page of this site.

It happens that when I put the function mysql_connect , in order to be able to collect the information of the bank at the time of running the following error message appears:

  

Fatal error: Call to a member function find () on a non-object in /var/www/html/crawler/simple_html_dom.php on line 1113

updating responding to requests the function that is (next in fact) of line 1113 in simple_html_dom.php is as follows:

function find($selector, $idx=null, $lowercase=false)
{
    return $this->root->find($selector, $idx, $lowercase);
}

I have tried several alternatives and I do not know what else to do.

If you want to download Simple_html_dom for testing follow the link: link

    
asked by anonymous 08.05.2014 / 11:38

2 answers

1

Resolution

Basically I followed the advice of @GuilhermeOderdenge and I researched alternatives to the late mysql_connect() and I used the PDO , and what was not my surprise when working with objects only the system worked perfect harmony and no bugs.

Then replace the command:

$conecta = mysql_connect("localhost", "root", "");

By connecting to the PDO :

$pdo = new PDO('mysql:host=localhost;dbname=meuBancoDeDados', "root", ""); 
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

And everything worked right. Detail that whenever I try to put a function out of an object to work directly with simple_html_dom I'll run the risk of having that problem.

So from now on 100% PDO E 100% Object Orientation : D

Thanks to all who helped.

    
09.05.2014 / 12:27
3

Move the connection up to include_once() so that we do not have disrespect for the file code - this is probably affecting the workflow of class simple_html_dom . Result:

function crawler() {
  $conecta = mysql_connect("localhost", "root", "");

  include_once './simple_html_dom.php';

  // [...]
}

Observations

First , the mysql_connect () function is deprecated. This means that it is no longer updated, will be removed soon and you should avoid using it.

Second , value by nomenclature . Variables tend to be nouns , and $conecta is a conjugated verb in third person singular . Tip: $conecta$conexao .

    
08.05.2014 / 14:37