Jared Aqua Reece
Jared Aqua Reece

Reputation: 55

MySQL to PDO (specific instance)

I've been translating an entire website page by page over from MySQL to PDO. For the most part, the resources, examples, and questions here have proven to be a fantastic resource for learning the differences and conversion equivalents. However, there is a statement I've been struggling with for a bit, and I feel I may need to ask the community on this one. It combines a Query + Num_Rows statement, and I'm not sure if there might be a simpler way to rewrite this. I've copied the relevant portions of the page below, but if you require additional information please let me know. The line in question is $total_pages=mysql_num_rows(mysql_query('select author from '.$sql_bookshelfquery));

Portion of PHP document that line above resides in (including line itself)

$sql_bookshelfquery="trgbookshelf13 where trgbookshelf13.remove!=1 ";
    $sql_bookshelfquery.=$querylevel;
    $sql_bookshelfquery.=$isnew;



// How many adjacent pages should be shown on each side?
    $adjacents = 3;

    /* 
       First get total number of rows in data table. 
       If you have a WHERE clause in your query, make sure you mirror it here.
    */

    $total_pages=mysql_num_rows(mysql_query('select author from '.$sql_bookshelfquery));



    /* Setup vars for query. */
    $limit = 50;                                //how many items to show per page
    if($page) 
        $start = ($page - 1) * $limit;          //first item to display on this page
    else
        $start = 0;                             //if no page var is given, set start to 0

    /* Get data. */
        $sql_bookshelfquery.=$orderby;
    $sql_bookshelfquery.=" LIMIT $start, $limit";
    $sql_bookshelfquery="SELECT * FROM ".$sql_bookshelfquery;


    //$result = mysql_query($sql_bookshelfquery);
    $result = $dbh->query($sql_bookshelfquery);

Upvotes: 1

Views: 51

Answers (1)

versalle88
versalle88

Reputation: 1137

I don't know specifically your PDO variables, but something like this should work:

$sql = "SELECT COUNT(`author`) FROM " . $sql_bookshelfquery; // Write SQL
$sth = $dbh->prepare($sql);  // Prepare query
$sth->execute(); // Execute query
$total_pages = $sth->fetchColumn(); // Fetch query result

Cheers!

Upvotes: 1

Related Questions