PHP SIMPLE DOM PDO select

169 views Asked by At

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

2

There are 2 answers

3
ivanivan On

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);

    }
0
Tudor On

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.