2

I am wondering what PDO function that is equal to the db::getOne? I have this i need to change to PDO:

$count = db::getOne($query,$id,$showU['id']);
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Karem
  • 17,615
  • 72
  • 178
  • 278

3 Answers3

2
$st = $pdodb->prepare($query);
$st->bindValue('id', $id);
$count = $st->fetchColumn();

This assumes you use named parameters which are always better than positional parameters IMO.

rustyx
  • 80,671
  • 25
  • 200
  • 267
  • Thank for the answer, I made this question of out this answer, please see: http://stackoverflow.com/questions/4359642/php-making-cleaner-shorter-code/4360286#4360286 , because I do not know what getOne and I needed it in PDO. But will this do it? – Karem Dec 07 '10 at 17:30
1

You really need to provide more info but im going to assume that id and $showU['id'] are params to be bound to $query and that getOne retruns a single record. If these assumptions are correct you would do something like:

$query = "SELECT count(id) as nb_records FROM your_table_name WHERE id = :id AND uID = :uid";
$stmt = $pdo->prepare($query);
$stmt->execute(array(':id' => $id, ':uid' => $showU['id']);
$count = $stmt->fetchColumn();

You could of course chain these.

prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • Hi. Thank for the answer, I made this question of out this answer, please see: http://stackoverflow.com/questions/4359642/php-making-cleaner-shorter-code/4360286#4360286 , because I do not know what getOne and I needed it in PDO. But will this do it? – Karem Dec 07 '10 at 17:08
  • 1
    execute returns boolean, not a statement object. Correct code would be `if ($stmt->execute(array(':id' => $id, ':uid' => $showU['id'])) { $count = $stmt->fetchColumn(); } ` – Tim Sep 24 '13 at 02:39
  • @Tim corrected... surprised no on including myself caught that. – prodigitalson Sep 24 '13 at 18:43
-2

It's a user-defined function. One can use not only ready-made functions, but also create new ones. So, getOne is of this kind. It has nothing to do with PDO or other library. It's rather of your own library. But of course, it can be made by using PDO as well.

You can adopt this function to make a similar one which returns scalar value instead of array. But it seems you do not quite understand the meaning and the benefits (no offense - it's just matter of experience), so, nevermind. This function is not obligatory, it's just to make your code shorter and cleaner. Programming, in general, stands for eliminating repetitions. Most of local folks aren't programmers but rather copy/pasters. Most of them just don't care of code size or readability. Not quite bad, as long as their code works. So, you can go this way too.

The only purpose of this function is to do less work for you and make your code more readable. It doesn't affect any results. So, it's not obligatory.

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