-2

I need to convert the following from old mysql_ style connections to new pdo style connections.

function getRecords(){
    $this->res = mysql_query("select * from cusbuilder_sites");
    if(mysql_num_rows($this->res)){
        while($this->row = mysql_fetch_assoc($this->res)){
            $record = array_map('stripslashes', $this->row);
            $this->records[] = $record; 
        }
        return $this->records;
    }
   //else echo "No records found";
}

I am still learning pdo but was trying to replace as follows, but i am completely lost:

$stmt=$db->prepare('SELECT *, COUNT(*) AS cnt FROM cusbuilder_sites ORDER BY id');
$stmt->execute();
$row = $stmt->fetchAll();
if ($row['cnt'] > $row[id]) {
    ...

At this point I really have no idea what to change. Looking for some assistance or perhaps a better and more correct way to achieve this.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Bruce
  • 1,039
  • 1
  • 9
  • 31
  • 1
    why not go with `mysqli_*` it works much in the same way as `mysql_*`. `mysqli_*` supports procedural and OOP while `PDO` only supports OOP. `mysqli_*` should also be faster than `PDO` – SuperDJ Sep 06 '16 at 16:55
  • because mysqli_ just like mysql will be phased out in newer versions of php and in php7 requires a module as it is not part of the core php7, in favor of pdo. – Bruce Sep 06 '16 at 16:57
  • @SuperDJ "mysqli_* should also be faster than PDO" Why should it? – PeeHaa Sep 06 '16 at 17:01
  • 1
    for the same reason mysql_ is faster than mysqli_ actually. But the benchmarks are SO SMALL its not a significant reason for not using pdo. – Bruce Sep 06 '16 at 17:02
  • @PeeHaa I haven't tested/ used `PDO` in a project and because of that I can't say it is. I hafe only read about it. `mysqli_*` is specific and therfor tweaked for `mysql` where `PDO` can be used for many database types – SuperDJ Sep 06 '16 at 17:02
  • 1
    @Bruce Could you show up what makes you think that php's `mysqli` extension "will be phased out"? – arkascha Sep 06 '16 at 17:03
  • 2
    Let me help you in that case. Any difference in performance is likely to be soo small it doesn't warrant a mention in 99% of the cases. And the fact that PDO's API is much saner that would trump it all @SuperDJ – PeeHaa Sep 06 '16 at 17:03
  • @arkascha http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Bruce Sep 06 '16 at 17:06
  • How is someone going to mark this as duplicate when I am specifically stating I do not want to use mysqli_ I want pdo – Bruce Sep 06 '16 at 17:09
  • Sorry Bruce. Copied the wrong dupe target http://stackoverflow.com/questions/18803501/convert-mysql-to-pdo – Machavity Sep 06 '16 at 17:13
  • @Bruce Can't spot a hint on that `mysqli` is planned to be removed in that question you referenced, sorry. – arkascha Sep 06 '16 at 17:14
  • @Machavity most of that question specifically relates to converting the connection itself, how to assign paramenters, how to execute... as can be seen in my code, I understand that part of things, my question is not so broad, it specifically relates to THIS function. I have converted most of the rest of my code already. – Bruce Sep 06 '16 at 17:17
  • @Bruce I still think it's a dupe but have at it – Machavity Sep 06 '16 at 17:19
  • 1
    Also, don't confuse `mysql_` with `mysqli_`. The latter is here to stay, while the former is original to PHP and had numerous deficiencies – Machavity Sep 06 '16 at 17:21

1 Answers1

1

the code is pretty much the same. especially if you take out all the useless and harmful stuff.

function getRecords(){
    $stmt = $this->db->query('SELECT * FROM cusbuilder_sites ORDER BY id');
    return $stmt->fetchAll();
}

just make sure you assigned a PDO instance to a class variable db

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345