• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PDO in Codeigniter

#11
[eluser]Ickes[/eluser]
Also I did add...

Code:
$stmt->execute();
$data = $stmt->fetch(PDO::FETCH_ASSOC);

Still when I print_r($data), I get nothing!! There is data in the table and the sql query on its own works. I am sure I am missing something. Anyone?

#12
[eluser]Chad Fulton[/eluser]
I think that there is some confusion in this thread about the way PDO and CI are being integrated. In kgill's DB.php hack, he essentially just makes it so that $this->db is a PDO object.

So, in his approach, $this->db is identical to the following:
Code:
$pdo = new PDO( ... )

In other words, $this->db is no longer connected to CodeIgniter in any way. Maybe this is where your confusion is coming from. Since you are making $this->db a PDO object, you can use it exactly as you normally would (I think you mentioned that you have some familiarity with PDO?).

Edit:
Whoops, posted this too late, and you'd already gotten to the $stmt->fetch(); part.

As far as I can see, there is some bug in your code somewhere, unfortunately since I can't see it and your database I don't know what it is.

Try this, for debugging:

Code:
//dummy variable
$pass_thru = 5;

$stmt = $this->db->prepare("SELECT * FROM tbl_blog WHERE blog_id <= :test");
$stmt->bindParam(':test', $pass_thru, PDO::PARAM_INT);

// Notice that execute() returns TRUE on success, FALSE on failure
if($stmt->execute()) {
    echo count($stmt->fetchAll(PDO::FETCH_ASSOC));
}
else {
    print_r($stmt->errorInfo());
}
die;

#13
[eluser]Ickes[/eluser]
[quote author="Chad Fulton" date="1246923557"]
As far as I can see, there is some bug in your code somewhere, unfortunately since I can't see it and your database I don't know what it is.

Try this, for debugging:

Code:
//dummy variable
$pass_thru = 5;

$stmt = $this->db->prepare("SELECT * FROM tbl_blog WHERE blog_id <= :test");
$stmt->bindParam(':test', $pass_thru, PDO::PARAM_INT);

// Notice that execute() returns TRUE on success, FALSE on failure
if($stmt->execute()) {
    echo count($stmt->fetchAll(PDO::FETCH_ASSOC));
}
else {
    print_r($stmt->errorInfo());
}
die;
[/quote]

Stupid me! There was an issue with my db connection. Thanks everyone for the help on this. Even though it was very painful, and probably painful for all involved, I learned a lot.

By using PDO like this, it does remove it from CodeIgniter's syntax. While the idea with CI is to reduce code by using Active Record, I think I will keep my db stuff in PDO. If someone is against this approach, I would love to hear why. I am definitely open minded for change.

Thanks again everyone!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.