-2

I have in the database a list of links from which I want to take some data.

All the script is working, except the part when I'm taking the link from the DB and paste it in Simple DOM function. " include ('utile/db.php'); include_once('utile/simple_html_dom.php');

$dbh = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8;", $username, $password);
$sth = $dbh->query("SELECT link FROM pilots where year = '2007' and Contry ='CZ' and zboruri <> '101' limit 3 ");

foreach ($sth as $url) {
     functie ($url['link']);
}

function functie($lin){
     $linkul=file_get_html("$lin");

// pages number
     $paging = $linkul->find('div[class*=paging]',0);
     echo $paging;
     $numar=-4;
     foreach($paging->find('a') as $element=>$item){$numar++;}
     echo $numar;
}

" I receive the following error:

Fatal error: Call to a member function find() on null in C:\xampp\htdocs\para\teste.php on line 269

If I change the link manually it will work.

I think it is something related how I extract the link from DB and insert it the function.

Thank you

Tudor
  • 980
  • 8
  • 10
  • Maybe there are some extra characters in the database. Use `var_dump($lin)` to see what it contains. Make sure the string length matches what you expect. There might be spaces or newlines in it. – Barmar Oct 20 '16 at 00:05
  • 1
    You shouldn't put the `include_once` call inside the function. These functions should be defined globally, not local to a function. – Barmar Oct 20 '16 at 00:06
  • thank you @Barmar . But, i still receive the same error. – Tudor Oct 20 '16 at 07:39
  • I never intended that that would fix the problem. That's why it was a comment, not an answer. Like I said, you need to check that the database really contains the correct URL. It might have extra space or newline characters in it. The use of a variable doesn't depend on how you set it, only what it contains. – Barmar Oct 20 '16 at 15:28
  • I tryed with var_dump($lin) and I receive the following line: string(58) "http://www.xcontest.org/2007/world/en/pilots/detail:Luca69" – Tudor Oct 20 '16 at 18:33
  • You're missing the `http://` at the beginning of the URL. So it looks like a local filename, not a remote URL. – Barmar Oct 20 '16 at 18:37
  • That's also only 47 characters long, so the length 58 is suspicious. Although if I add `http://www.` to the beginning, it's 58 characters. Did that get filtered out by the SO comment parser? Put backticks around string in comments to make them literal. – Barmar Oct 20 '16 at 18:38
  • thank you for your help @Barmar. It worked after I chaged this line: foreach($sth->fetchAll() as $url){ – Tudor Oct 20 '16 at 19:04

2 Answers2

0

The issue with fetchALL in foreach. The line changed:

foreach($sth->fetchAll() as $url){

The final code that is working:

include ('utile/db.php');
include_once('utile/simple_html_dom.php');

$dbh = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8;", $username, $password);
$sth = $dbh->query("SELECT link FROM pilots where zboruri > '101' limit 3");

foreach($sth->fetchAll() as $url){
     functie ($url['link']);
}

function functie($lin){
  var_dump($lin);
     $linkul=file_get_html("$lin");

     $paging = $linkul->find('div[class*=paging]',0);// pages number
     echo $paging;
     $numar=-4;
     foreach($paging->find('a') as $element=>$item){$numar++;}
     echo $numar;
}

Thank you for advices.

Tudor
  • 980
  • 8
  • 10
-1

When I use PDO I use prepared statements, so the syntax on getting the result of the query is a little different... but I think that you need to fetch a row from your $sth since it would be a record set. Here's a snippit of what I do

    $dbconn = new PDO('mysql:host='.$hostname.';port='.$dbPort.';dbname='.$dbName.';charset=utf8', $dbuser, $dbpass,array(PDO::MYSQL_ATTR_FOUND_ROWS => true));
    $dbconn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $result=$dbconn->prepare($query);
    $result->execute($arr);
    if(!$result){
        // do your error handling, result is either false 
        // for error or contains a recordset

        $errorMessage=$dbconn->errorInfo();

    }

    $result->setFetchMode(PDO::FETCH_ASSOC);
    while($row=$result->fetch()){

    // do stuff here, $row is an associative array w/ the 
    //keys being the column titles in your db table

        print_r($row);

    }
ivanivan
  • 2,155
  • 2
  • 10
  • 11
  • Basicly, I have my URL list for each I need to run the simple DOM script saved in a database. – Tudor Oct 20 '16 at 07:41
  • The issue is when I try to send the link from the database in to DOM function. It is not reading it as beeing a valid data. You have any similar script or anoather advice? Thank you again. – Tudor Oct 20 '16 at 07:43
  • I see you fixed it by fetching a row (well,all of them) from your result :) – ivanivan Oct 20 '16 at 21:29