Conflict between Simple_html_dom and Nonobject-Oriented functions

Asked

Viewed 282 times

6

I’m developing an app that has to access a list of websites stored in a database, upload all their links. It’s a test application but I’ve found 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 me is a series of links from within the main page of this site.

It turns out that when I put the function mysql_connect, to be able to collect information from the bank at the time of running the following error message:

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 actually) of the 1113 line in simple_html_dom.php is as follows:

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

I’ve tried several alternatives and I don’t know what else to do.

If you want to download Simple_html_dom for testing follow the link.: http://sourceforge.net/projects/simplehtmldom/files/

  • Post the code from line 1113 of the simple_html_dom.php file, please.

  • Basically the error says that the object was not created correctly so it has no method.

  • Actually the method exists, as I showed in the update I asked in the question. And it works if I don’t put the function mysql_connect() before...

  • brasofilo, to try to explain better, if I put instead an array with the page a routine to connect to the database and take the links from there, the system does not work...

  • You can put the code that has the snippet with mysql_connect, I tested I got this error passing a blank value to simple_html_dom

  • Opa if you look I updated the question and put the additional data left the command that generates the commented error. abs.

  • If I’m not mistaken, that function find() gets a array and you try to read it as an object within the loop.

  • Opa thanks for the reply @luxu, really makes sense, however how to explain that the function works correctly if I do not insert the connection routine to the database?

  • Fatal error: Call to a member function find() on a non-object this error occurs when you try to execute an incorrect query, check if the fields you are searching for in the database actually exist

  • @luxu No. According to official documentation, the find() returns an "object array", see: "Find all Anchors, Returns an array of element Objects".

  • Thanks for the @Guilhermeoderdenge clarification. As I had said, the routine works correctly as long as I don’t call the database.

  • 1

    Opa Rodrigo thank you for the collaboration, the fields exist yes, the query has been revised, but I do not even call the query, as I said, just insert the function to connect with the database that the error happens.

  • Perform a var_dump() in $html and post the result.

  • Running var_dump($html) displayed this result: object(simple_html_dom)#1 (19) { ["nodes"]=> array(34488) { [0]=> object(simple_html_dom_node)#2 (9) { ["nodetype"]=> int(5) ["tag"]=> string(4) "root" ["attr"]=> array(0) { } ["children"]=> NULL ["nodes"]=> NULL ["parent"]=> NULL ["_"]=> array(2) { [0]=> int(-1) [1]=> int(34488) } ["tag_start"]=> int(0) ["dom":"simple_html_dom_node":private]=> NULL } [1]=> ... @lost to me it seems the object is working properly.

  • From what you’re saying, it seems to be the case mark an answer as accepted. Here we don’t put "solved" in the title. If you have an answer that really helped you, mark as accepted. If you came to the solution on your own, put in the solution as an answer. So content is more organized and easy to find for other people with similar problems in the future.

  • Thanks buddy, I’ll edit here. Thank you

Show 11 more comments

2 answers

3


Move the connection up the include_once() not to disrespect the file code - this is probably affecting the workflow class simple_html_dom. Upshot:

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

  include_once './simple_html_dom.php';

  // [...]
}

Remarks

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

According to, value by nomenclature. Variables tend to be nouns, and $conecta is a Verb conjugate in personal third of singular. Suggestion: $conecta$conexao.

  • Look, they’re good recommendations, but in my humble point of view, they’re not enough to generate an answer. These are good practices, but they don’t make any difference to the result of the problem. The system has been summarized and tested to eliminate unnecessary code. I agree to have to look for good practices, but I think your answer is more like a comment. But who am I right? Just a rookie around here. Hug and sorry if it seemed rude. It wasn’t my intention.

  • Actually I didn’t make it clear where the solution is. I made an issue and now it’s in evidence - you tested, by the way?

  • Thank you, @Guilherme-oderdenge, although I have already put this connection routine above, below, in another function, in an object and it has presented the same error(kkk), I will review everything again from your suggestions and see if it presents a different behavior, I just won’t do it right now because I’m on a client. I’m also starting to consider the possibility that the server gave some problem with apache, which was updated yesterday...

  • I’m actually going to do some more research on alternatives to mysql_connect().

  • I followed your comments and it really worked :D Thank you.

  • @pdonatilio Only for information beyond PDO may also be used mysqli

  • Yes, but I was interested in PDO as an ATM machine and native to PHP.

  • @pdonatilio PDO, in my opinion, is the best thing to do. And ah, if I solved your problem, check! : D Thanks guy!

  • Sorry for the ignorance, but where does @Guilhermeoderdenge tag ?

  • In my post, have two arrows: one up and one down. Below these arrows, has a gray marker - here’s who you’re looking for.

  • After a few millennia I am back on my account here and I realized that I committed this injustice with @Guilhermeoderdenge and I did not mark your answer as a viable solution.

Show 6 more comments

1

Resolution

I basically took the advice of @Guilhermeoderdenge and researched alternatives to the deceased mysql_connect() and used the PDO, and what was not my surprise to work only with objects the system worked perfect harmony and no bugs.

Then replace the command:

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

For the connection with the PDO:

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

And it all worked out right. Detail that whenever I try to put a function outside of an object to work directly with the simple_html_dom I’ll take the risk of having that problem.

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

Thank you to everyone who helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.