0

I've been trying to convert a mysql_connect connection into a PDO connection with no success, here is what I have:

$host = 'localhost';
        $user = 'root';
        $pwd = '';
        $db = 'jdlferreira';
        $connection = mysql_connect($host, $user, $pwd) or die("Could not connect");
        mysql_select_db($db) or die("Could not select database");

        $query = "SELECT COUNT(*) FROM blog";
        $result = mysql_query($query) or die(mysql_error());
        $num_rows = mysql_fetch_row($result);

        $pages = new Paginator;
        $pages->items_total = $num_rows[0];
        $pages->mid_range = 9; // Number of pages to display. Must be odd and > 3
        $pages->paginate();

        $query = "SELECT id, title, resume, date
            FROM blog
            ORDER BY date DESC $pages->limit";
        $result = mysql_query($query) or die(mysql_error());


        while ($row = mysql_fetch_row($result)) {
//do stuff
}

And what I tried to do with PDO:

include_once 'inc/db.inc.php';
        $db = new PDO(DB_INFO, DB_USER, DB_PASS);
        mysql_select_db("jdlferreira") or die("Could not select database");

        $query = "SELECT COUNT(*) FROM blog";
        $result = mysql_query($query) or die(mysql_error());
        $num_rows = mysql_fetch_row($result);

        $pages = new Paginator;
        $pages->items_total = $num_rows[0];
        $pages->mid_range = 9; // Number of pages to display. Must be odd and > 3
        $pages->paginate();

        $query = "SELECT id, title, resume, date
            FROM blog
            ORDER BY date DESC $pages->limit";
        $result = mysql_query($query) or die(mysql_error());


        while ($row = mysql_fetch_row($result)) {
//do stuff
}

I'm getting a "Could not select database" error, I don't really care for the 'or die' cases, I would just like to make this connection functional on PDO, any help would be great.

Jorg Ancrath
  • 1,447
  • 10
  • 34
  • 61

1 Answers1

3

You cant use PDO and then exepect to use mysql_* functions they arent related.

Theres no need to select a db like that with pdo because its included in the DSN which is the contructors first argument:

$db = new PDO('mysql:host=localhost;dbname=jdlferreira', DB_USER, DB_PASS);

Then you need to use the PDO interface to interact with the DB, not the mysql ones:

    $stmt = $db->prepare("SELECT COUNT(*) FROM blog");
    $stmt->execute();
    $num_rows = $stmt->fetchColumn();
    $stmt->closeCursor();

    $pages = new Paginator;
    $pages->items_total = $num_rows;
    $pages->mid_range = 9; // Number of pages to display. Must be odd and > 3
    $pages->paginate();

    $query = "SELECT id, title, resume, date
        FROM blog
        ORDER BY date DESC $pages->limit";
    $stmt = $db->prepare($query);
    $stmt->execute();


    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
      // do stuff
    }
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • What are my chances or working out an equivalent solution in PDO though? I've been looking at mysql_* functions on the php website and I'm not getting much luck on finding out equivalencies. – Jorg Ancrath Sep 17 '12 at 21:17
  • if you want equivalences, the `mysqli` library is a closer match to the old `mysql` library than PDO is. But read the example given in tis answer -- he's given you a pretty solid example of how to do what you're asking for in PDO: all the calls you'll need are in there. – Spudley Sep 17 '12 at 21:25
  • Nice solution, thanks for your help, I was getting this error though: Fatal error: Call to undefined method PDOStatement::close() , I removed that close(); line and everything seems to be functioning fine, am I missing something here? What are the problems of me not closing $stmt? – Jorg Ancrath Sep 17 '12 at 21:35
  • OK, seems like there is no close() method for PDO, thanks again for your help prodigitalson. – Jorg Ancrath Sep 17 '12 at 21:50
  • Yeah sorry should be `PDOStatement::closeCursor` answer updated. – prodigitalson Sep 17 '12 at 22:25
  • @Joao: There arent direct equiv. because the theory of operation is a bit different and there is no functional interface only an OOP one. IMO this is a good thing :-) – prodigitalson Sep 17 '12 at 22:31