Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO - What do $dbh and $sth stand for?

In PDO examples I often find the acronyms $dbh and $sth used. I suppose $dbh stands for "database handle" - correct? What about $sth? "Statement handle"?

Are there good reasons to use the above instead of $db_connection and $query (or other as appropriate)?

like image 587
JDelage Avatar asked Jun 16 '11 23:06

JDelage


People also ask

What is $dbh in PHP?

$DBH = null; You can get more information on database-specific options and/or connection strings for other databases from PHP.net.

What does PHP PDO stand for?

PDO is an acronym for PHP Data Objects. PDO is a lean, consistent way to access databases. This means developers can write portable code much easier. PDO is not an abstraction layer like PearDB. PDO is a more like a data access layer which uses a unified API (Application Programming Interface).

What does PDO stand for MySQL?

Introduction ¶ PDO_MYSQL is a driver that implements the PHP Data Objects (PDO) interface to enable access from PHP to MySQL databases.

What is PDO :: Param_str in PHP?

PDO::PARAM_STR (int) Represents the SQL CHAR, VARCHAR, or other string data type. PDO::PARAM_STR_NATL (int) Flag to denote a string uses the national character set.


2 Answers

$dbh = "Database Handle"

$sth = "Statement Handle"

I prefer the longer forms, as they are more descriptive. It is often helpful to future maintainers if you are explicit, even when using common acronyms and abbreviations.

In the past, when hard drive capacity, memory and bandwidth were scarcer, the abbreviations may have made sense. Today there is (arguably) greater value in producing readable, maintainable code.

like image 81
George Cummins Avatar answered Sep 29 '22 09:09

George Cummins


Your intuition was correct as to what those variable names stand for, but that isn't to say that every use of PDO uses those variable names, just the particular code you're looking at.

Variable names are never significant to the correct execution of a function that you pass them to. I've seen people use obscenities for variable names in their entire project... it just doesn't matter to the functions. For your own sake, and the sake of any future developers who work with you, variable names should be concise and clear. Abbreviations should be avoided when you can help it, since it may not be clear to someone else that $hndlr is how you abbreviate "handler" - as this very question demonstrates, abbreviations are seldom intuitive.

It is more important that you develop or choose a coding standard and stick with it. If your variables are all lower_case_with_underscores, stick to that regardless of what you see people doing in other code outside of your project.

like image 30
Chris Baker Avatar answered Sep 29 '22 08:09

Chris Baker