FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » database access
Show: Today's Messages :: Unread Messages :: Show Polls :: Message Navigator
| Subscribe to topic | Bookmark topic 
Switch to threaded view of this topic Create a new topic Submit Reply
database access [message #174189] Wed, 25 May 2011 13:17 Go to next message
Jivanmukta is currently offline  Jivanmukta
Messages: 20
Registered: January 2011
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
I new relatively new to PHP. I need your opinion if the following set
of database functions make sense:

function openDatabase() {
global $db;
try {
$db = new PDO('mysql:host=' . MYSQL_SERVER . ';dbname=' .
MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD);
$db->exec('SET NAMES latin2');
$db->exec('SET CHARACTER SET latin2 COLLATE latin2_general_ci');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
register_shutdown_function('closeDatabase');
} catch (PDOException $e) {
return false;
}
return true;
}

function closeDatabase() {
global $db, $inTransaction;
if ($inTransaction) {
$db->rollBack();
$inTransaction = false;
}
if (!is_null($db)) {
$db = null;
}
}

function queryDatabase($sql) {
global $db, $inTransaction;
$result = array();
try {
$qry = $db->query($sql);
if (!empty($qry)) {
$result = $qry->fetchAll();
}
} catch (PDOException $e) {
if ($inTransaction) {
$db->rollBack();
}
error(ERRMSG_DATABASE_OPERATION_FAILED);
}
return $result;
}

function execDatabase($sql) {
global $db, $inTransaction;
$result = array();
try {
$result = $db->exec($sql);
} catch (PDOException $e) {
if ($inTransaction) {
$db->rollBack();
}
error(ERRMSG_DATABASE_OPERATION_FAILED);
}
return $result;
}

function startTransaction() {
global $db, $inTransaction;
$db->beginTransaction();
$inTransaction = true;
}

function commitTransaction() {
global $db, $inTransaction;
$inTransaction = false;
if ($db->commit()) {
return;
}
$db->rollBack();
error(ERRMSG_DATABASE_OPERATION_FAILED);
}

function rollbackTransaction() {
global $db, $inTransaction;
$inTransaction = false;
if ($db->rollBack()) {
return;
}
error(ERRMSG_DATABASE_OPERATION_FAILED);
}

function lockTables($tables) {
return execDatabase('LOCK TABLES ' . join(' WRITE, ', $tables) . '
WRITE');
}

function unlockTables() {
return execDatabase('UNLOCK TABLES');
}
Re: database access [message #174190 is a reply to message #174189] Wed, 25 May 2011 17:37 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 5/25/2011 1:17 PM, Jivanmukta wrote:
> I new relatively new to PHP. I need your opinion if the following set
> of database functions make sense:
>
> function openDatabase() {
> global $db;
> try {
> $db = new PDO('mysql:host=' . MYSQL_SERVER . ';dbname=' .
> MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD);
> $db->exec('SET NAMES latin2');
> $db->exec('SET CHARACTER SET latin2 COLLATE latin2_general_ci');
> $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
> register_shutdown_function('closeDatabase');
> } catch (PDOException $e) {
> return false;
> }
> return true;
> }
>
> function closeDatabase() {
> global $db, $inTransaction;
> if ($inTransaction) {
> $db->rollBack();
> $inTransaction = false;
> }
> if (!is_null($db)) {
> $db = null;
> }
> }
>
> function queryDatabase($sql) {
> global $db, $inTransaction;
> $result = array();
> try {
> $qry = $db->query($sql);
> if (!empty($qry)) {
> $result = $qry->fetchAll();
> }
> } catch (PDOException $e) {
> if ($inTransaction) {
> $db->rollBack();
> }
> error(ERRMSG_DATABASE_OPERATION_FAILED);
> }
> return $result;
> }
>
> function execDatabase($sql) {
> global $db, $inTransaction;
> $result = array();
> try {
> $result = $db->exec($sql);
> } catch (PDOException $e) {
> if ($inTransaction) {
> $db->rollBack();
> }
> error(ERRMSG_DATABASE_OPERATION_FAILED);
> }
> return $result;
> }
>
> function startTransaction() {
> global $db, $inTransaction;
> $db->beginTransaction();
> $inTransaction = true;
> }
>
> function commitTransaction() {
> global $db, $inTransaction;
> $inTransaction = false;
> if ($db->commit()) {
> return;
> }
> $db->rollBack();
> error(ERRMSG_DATABASE_OPERATION_FAILED);
> }
>
> function rollbackTransaction() {
> global $db, $inTransaction;
> $inTransaction = false;
> if ($db->rollBack()) {
> return;
> }
> error(ERRMSG_DATABASE_OPERATION_FAILED);
> }
>
> function lockTables($tables) {
> return execDatabase('LOCK TABLES ' . join(' WRITE, ', $tables) . '
> WRITE');
> }
>
> function unlockTables() {
> return execDatabase('UNLOCK TABLES');
> }

Kind of, and maybe a bit overkill. But personally I'd put it all in a
Database class. The code would be more flexible and less prone to errors.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: database access [message #174194 is a reply to message #174190] Thu, 26 May 2011 08:09 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 5/25/2011 5:37 PM, Jerry Stuckle wrote:
> On 5/25/2011 1:17 PM, Jivanmukta wrote:
>> I new relatively new to PHP. I need your opinion if the following set
>> of database functions make sense:
>>
>> function openDatabase() {
>> global $db;
>> try {
>> $db = new PDO('mysql:host=' . MYSQL_SERVER . ';dbname=' .
>> MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD);
>> $db->exec('SET NAMES latin2');
>> $db->exec('SET CHARACTER SET latin2 COLLATE latin2_general_ci');
>> $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
>> register_shutdown_function('closeDatabase');
>> } catch (PDOException $e) {
>> return false;
>> }
>> return true;
>> }
>>
>> function closeDatabase() {
>> global $db, $inTransaction;
>> if ($inTransaction) {
>> $db->rollBack();
>> $inTransaction = false;
>> }
>> if (!is_null($db)) {
>> $db = null;
>> }
>> }
>>
>> function queryDatabase($sql) {
>> global $db, $inTransaction;
>> $result = array();
>> try {
>> $qry = $db->query($sql);
>> if (!empty($qry)) {
>> $result = $qry->fetchAll();
>> }
>> } catch (PDOException $e) {
>> if ($inTransaction) {
>> $db->rollBack();
>> }
>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>> }
>> return $result;
>> }
>>
>> function execDatabase($sql) {
>> global $db, $inTransaction;
>> $result = array();
>> try {
>> $result = $db->exec($sql);
>> } catch (PDOException $e) {
>> if ($inTransaction) {
>> $db->rollBack();
>> }
>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>> }
>> return $result;
>> }
>>
>> function startTransaction() {
>> global $db, $inTransaction;
>> $db->beginTransaction();
>> $inTransaction = true;
>> }
>>
>> function commitTransaction() {
>> global $db, $inTransaction;
>> $inTransaction = false;
>> if ($db->commit()) {
>> return;
>> }
>> $db->rollBack();
>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>> }
>>
>> function rollbackTransaction() {
>> global $db, $inTransaction;
>> $inTransaction = false;
>> if ($db->rollBack()) {
>> return;
>> }
>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>> }
>>
>> function lockTables($tables) {
>> return execDatabase('LOCK TABLES ' . join(' WRITE, ', $tables) . '
>> WRITE');
>> }
>>
>> function unlockTables() {
>> return execDatabase('UNLOCK TABLES');
>> }
>
> Kind of, and maybe a bit overkill. But personally I'd put it all in a
> Database class. The code would be more flexible and less prone to errors.
>

Further, I would not have globals. I would pass $db in via the
constructor. Likewise, I would have $inTransaction be a class variable
which gets set by the method startTransaction().

--
Shelly
Re: database access [message #174196 is a reply to message #174194] Thu, 26 May 2011 09:00 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 5/26/2011 8:09 AM, sheldonlg wrote:
> On 5/25/2011 5:37 PM, Jerry Stuckle wrote:
>> On 5/25/2011 1:17 PM, Jivanmukta wrote:
>>> I new relatively new to PHP. I need your opinion if the following set
>>> of database functions make sense:
>>>
>>> function openDatabase() {
>>> global $db;
>>> try {
>>> $db = new PDO('mysql:host=' . MYSQL_SERVER . ';dbname=' .
>>> MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD);
>>> $db->exec('SET NAMES latin2');
>>> $db->exec('SET CHARACTER SET latin2 COLLATE latin2_general_ci');
>>> $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
>>> register_shutdown_function('closeDatabase');
>>> } catch (PDOException $e) {
>>> return false;
>>> }
>>> return true;
>>> }
>>>
>>> function closeDatabase() {
>>> global $db, $inTransaction;
>>> if ($inTransaction) {
>>> $db->rollBack();
>>> $inTransaction = false;
>>> }
>>> if (!is_null($db)) {
>>> $db = null;
>>> }
>>> }
>>>
>>> function queryDatabase($sql) {
>>> global $db, $inTransaction;
>>> $result = array();
>>> try {
>>> $qry = $db->query($sql);
>>> if (!empty($qry)) {
>>> $result = $qry->fetchAll();
>>> }
>>> } catch (PDOException $e) {
>>> if ($inTransaction) {
>>> $db->rollBack();
>>> }
>>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>> }
>>> return $result;
>>> }
>>>
>>> function execDatabase($sql) {
>>> global $db, $inTransaction;
>>> $result = array();
>>> try {
>>> $result = $db->exec($sql);
>>> } catch (PDOException $e) {
>>> if ($inTransaction) {
>>> $db->rollBack();
>>> }
>>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>> }
>>> return $result;
>>> }
>>>
>>> function startTransaction() {
>>> global $db, $inTransaction;
>>> $db->beginTransaction();
>>> $inTransaction = true;
>>> }
>>>
>>> function commitTransaction() {
>>> global $db, $inTransaction;
>>> $inTransaction = false;
>>> if ($db->commit()) {
>>> return;
>>> }
>>> $db->rollBack();
>>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>> }
>>>
>>> function rollbackTransaction() {
>>> global $db, $inTransaction;
>>> $inTransaction = false;
>>> if ($db->rollBack()) {
>>> return;
>>> }
>>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>> }
>>>
>>> function lockTables($tables) {
>>> return execDatabase('LOCK TABLES ' . join(' WRITE, ', $tables) . '
>>> WRITE');
>>> }
>>>
>>> function unlockTables() {
>>> return execDatabase('UNLOCK TABLES');
>>> }
>>
>> Kind of, and maybe a bit overkill. But personally I'd put it all in a
>> Database class. The code would be more flexible and less prone to errors.
>>
>
> Further, I would not have globals. I would pass $db in via the
> constructor. Likewise, I would have $inTransaction be a class variable
> which gets set by the method startTransaction().
>

That was part of the class, Sheldon. In fact, I wouldn't even pass $db
into the constructor; it (along with $inTransaction, as you note) would
be class variables. That way the rest of the program is completely
independent of the database (and you can have more than one database
active concurrently if you want).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: database access [message #174197 is a reply to message #174189] Thu, 26 May 2011 09:05 Go to previous messageGo to next message
JohnT is currently offline  JohnT
Messages: 16
Registered: April 2011
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
On Wed, 25 May 2011 10:17:48 -0700, Jivanmukta wrote:

> I new relatively new to PHP. I need your opinion if the following set of
> database functions make sense:
>
> function openDatabase() {
> global $db;
> try {
> $db = new PDO('mysql:host=' . MYSQL_SERVER . ';dbname=' .
> MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD);
> $db->exec('SET NAMES latin2');
> $db->exec('SET CHARACTER SET latin2 COLLATE
latin2_general_ci');
> $db->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
> register_shutdown_function('closeDatabase');
> } catch (PDOException $e) {
> return false;
> }
> return true;
> }

Looks like real overkill to me.
Do you really need to use PDO ?
Whats wrong with the regular PHP mysql_XXXX() functions ?
Re: database access [message #174198 is a reply to message #174197] Thu, 26 May 2011 09:37 Go to previous messageGo to next message
Jivanmukta is currently offline  Jivanmukta
Messages: 20
Registered: January 2011
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
> Do you really need to use PDO ?
> Whats wrong with the regular PHP mysql_XXXX() functions ?

I could not solve some problem with mysqli and this problem did not
occur using PDO. Nobody could help me with this problem on newsgroups.
If I remember correctly fetching some nonempty query returned false in
mysqli, in PDO the same query returned proper set.
Re: database access [message #174199 is a reply to message #174196] Thu, 26 May 2011 10:22 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 5/26/2011 9:00 AM, Jerry Stuckle wrote:
> On 5/26/2011 8:09 AM, sheldonlg wrote:
>> On 5/25/2011 5:37 PM, Jerry Stuckle wrote:
>>> On 5/25/2011 1:17 PM, Jivanmukta wrote:
>>>> I new relatively new to PHP. I need your opinion if the following set
>>>> of database functions make sense:
>>>>
>>>> function openDatabase() {
>>>> global $db;
>>>> try {
>>>> $db = new PDO('mysql:host=' . MYSQL_SERVER . ';dbname=' .
>>>> MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD);
>>>> $db->exec('SET NAMES latin2');
>>>> $db->exec('SET CHARACTER SET latin2 COLLATE latin2_general_ci');
>>>> $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
>>>> register_shutdown_function('closeDatabase');
>>>> } catch (PDOException $e) {
>>>> return false;
>>>> }
>>>> return true;
>>>> }
>>>>
>>>> function closeDatabase() {
>>>> global $db, $inTransaction;
>>>> if ($inTransaction) {
>>>> $db->rollBack();
>>>> $inTransaction = false;
>>>> }
>>>> if (!is_null($db)) {
>>>> $db = null;
>>>> }
>>>> }
>>>>
>>>> function queryDatabase($sql) {
>>>> global $db, $inTransaction;
>>>> $result = array();
>>>> try {
>>>> $qry = $db->query($sql);
>>>> if (!empty($qry)) {
>>>> $result = $qry->fetchAll();
>>>> }
>>>> } catch (PDOException $e) {
>>>> if ($inTransaction) {
>>>> $db->rollBack();
>>>> }
>>>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>>> }
>>>> return $result;
>>>> }
>>>>
>>>> function execDatabase($sql) {
>>>> global $db, $inTransaction;
>>>> $result = array();
>>>> try {
>>>> $result = $db->exec($sql);
>>>> } catch (PDOException $e) {
>>>> if ($inTransaction) {
>>>> $db->rollBack();
>>>> }
>>>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>>> }
>>>> return $result;
>>>> }
>>>>
>>>> function startTransaction() {
>>>> global $db, $inTransaction;
>>>> $db->beginTransaction();
>>>> $inTransaction = true;
>>>> }
>>>>
>>>> function commitTransaction() {
>>>> global $db, $inTransaction;
>>>> $inTransaction = false;
>>>> if ($db->commit()) {
>>>> return;
>>>> }
>>>> $db->rollBack();
>>>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>>> }
>>>>
>>>> function rollbackTransaction() {
>>>> global $db, $inTransaction;
>>>> $inTransaction = false;
>>>> if ($db->rollBack()) {
>>>> return;
>>>> }
>>>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>>> }
>>>>
>>>> function lockTables($tables) {
>>>> return execDatabase('LOCK TABLES ' . join(' WRITE, ', $tables) . '
>>>> WRITE');
>>>> }
>>>>
>>>> function unlockTables() {
>>>> return execDatabase('UNLOCK TABLES');
>>>> }
>>>
>>> Kind of, and maybe a bit overkill. But personally I'd put it all in a
>>> Database class. The code would be more flexible and less prone to
>>> errors.
>>>
>>
>> Further, I would not have globals. I would pass $db in via the
>> constructor. Likewise, I would have $inTransaction be a class variable
>> which gets set by the method startTransaction().
>>
>
> That was part of the class, Sheldon. In fact, I wouldn't even pass $db
> into the constructor; it (along with $inTransaction, as you note) would
> be class variables. That way the rest of the program is completely
> independent of the database (and you can have more than one database
> active concurrently if you want).

If you don't pass it in with the constructor, then you need something
like a setDb($parameter) to set the class variable. Passing it along
with the constructor does not limit you to only one database
concurrently. After all, you could readily do

$dbObj1 = new DatabaseClass($db1);
$dbObj2 = new DatabaseClass($db2);

What passing it along inside the constructor does is (a) it saves a line
of code each time in the invocation process(es) and (b) uniquely
identifies a particular instance of the class. Since the DatabaseClass
_MUST_ have the db defined, there is no problem with passing it in with
the constructor and no reason not to do so.

--
Shelly
Re: database access [message #174200 is a reply to message #174198] Thu, 26 May 2011 10:30 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 5/26/2011 9:37 AM, Jivanmukta wrote:
>> Do you really need to use PDO ?
>> Whats wrong with the regular PHP mysql_XXXX() functions ?
>
> I could not solve some problem with mysqli and this problem did not
> occur using PDO. Nobody could help me with this problem on newsgroups.
> If I remember correctly fetching some nonempty query returned false in
> mysqli, in PDO the same query returned proper set.

If the query is non-empty, you should have no problem in fetching the
results with the standard mysql functions. Why were you even bothering
with mysqli? Look for mysql_query, mysql_fetch_assoc, and
mysql_fetch_array in www.php.net. They are all you need.

What I regularly use most is:

$theArray = array();
$result = mysql_query("The query text");
if (FALSE !== $result) {
while ($row = mysql_fetch_assoc($result) {
$theArray[] = $row;
}
}

if (count($theArray)) {
.....
}


You could even put the query into your database class as a
getArray($query) method.

--
Shelly
Re: database access [message #174201 is a reply to message #174200] Thu, 26 May 2011 10:33 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 5/26/2011 10:30 AM, sheldonlg wrote:
> On 5/26/2011 9:37 AM, Jivanmukta wrote:
>>> Do you really need to use PDO ?
>>> Whats wrong with the regular PHP mysql_XXXX() functions ?
>>
>> I could not solve some problem with mysqli and this problem did not
>> occur using PDO. Nobody could help me with this problem on newsgroups.
>> If I remember correctly fetching some nonempty query returned false in
>> mysqli, in PDO the same query returned proper set.
>
> If the query is non-empty, you should have no problem in fetching the
> results with the standard mysql functions. Why were you even bothering
> with mysqli? Look for mysql_query, mysql_fetch_assoc, and
> mysql_fetch_array in www.php.net. They are all you need.
>
> What I regularly use most is:
>
> $theArray = array();
> $result = mysql_query("The query text");
> if (FALSE !== $result) {
> while ($row = mysql_fetch_assoc($result) {
> $theArray[] = $row;
> }
> }
>
> if (count($theArray)) {
> .....
> }
>
>
> You could even put the query into your database class as a
> getArray($query) method.
>

Ooops. Make that
if (count($theArray) > 0) {

--
Shelly
Re: database access [message #174202 is a reply to message #174199] Thu, 26 May 2011 11:41 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 5/26/2011 10:22 AM, sheldonlg wrote:
> On 5/26/2011 9:00 AM, Jerry Stuckle wrote:
>> On 5/26/2011 8:09 AM, sheldonlg wrote:
>>> On 5/25/2011 5:37 PM, Jerry Stuckle wrote:
>>>> On 5/25/2011 1:17 PM, Jivanmukta wrote:
>>>> > I new relatively new to PHP. I need your opinion if the following set
>>>> > of database functions make sense:
>>>> >
>>>> > function openDatabase() {
>>>> > global $db;
>>>> > try {
>>>> > $db = new PDO('mysql:host=' . MYSQL_SERVER . ';dbname=' .
>>>> > MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD);
>>>> > $db->exec('SET NAMES latin2');
>>>> > $db->exec('SET CHARACTER SET latin2 COLLATE latin2_general_ci');
>>>> > $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
>>>> > register_shutdown_function('closeDatabase');
>>>> > } catch (PDOException $e) {
>>>> > return false;
>>>> > }
>>>> > return true;
>>>> > }
>>>> >
>>>> > function closeDatabase() {
>>>> > global $db, $inTransaction;
>>>> > if ($inTransaction) {
>>>> > $db->rollBack();
>>>> > $inTransaction = false;
>>>> > }
>>>> > if (!is_null($db)) {
>>>> > $db = null;
>>>> > }
>>>> > }
>>>> >
>>>> > function queryDatabase($sql) {
>>>> > global $db, $inTransaction;
>>>> > $result = array();
>>>> > try {
>>>> > $qry = $db->query($sql);
>>>> > if (!empty($qry)) {
>>>> > $result = $qry->fetchAll();
>>>> > }
>>>> > } catch (PDOException $e) {
>>>> > if ($inTransaction) {
>>>> > $db->rollBack();
>>>> > }
>>>> > error(ERRMSG_DATABASE_OPERATION_FAILED);
>>>> > }
>>>> > return $result;
>>>> > }
>>>> >
>>>> > function execDatabase($sql) {
>>>> > global $db, $inTransaction;
>>>> > $result = array();
>>>> > try {
>>>> > $result = $db->exec($sql);
>>>> > } catch (PDOException $e) {
>>>> > if ($inTransaction) {
>>>> > $db->rollBack();
>>>> > }
>>>> > error(ERRMSG_DATABASE_OPERATION_FAILED);
>>>> > }
>>>> > return $result;
>>>> > }
>>>> >
>>>> > function startTransaction() {
>>>> > global $db, $inTransaction;
>>>> > $db->beginTransaction();
>>>> > $inTransaction = true;
>>>> > }
>>>> >
>>>> > function commitTransaction() {
>>>> > global $db, $inTransaction;
>>>> > $inTransaction = false;
>>>> > if ($db->commit()) {
>>>> > return;
>>>> > }
>>>> > $db->rollBack();
>>>> > error(ERRMSG_DATABASE_OPERATION_FAILED);
>>>> > }
>>>> >
>>>> > function rollbackTransaction() {
>>>> > global $db, $inTransaction;
>>>> > $inTransaction = false;
>>>> > if ($db->rollBack()) {
>>>> > return;
>>>> > }
>>>> > error(ERRMSG_DATABASE_OPERATION_FAILED);
>>>> > }
>>>> >
>>>> > function lockTables($tables) {
>>>> > return execDatabase('LOCK TABLES ' . join(' WRITE, ', $tables) . '
>>>> > WRITE');
>>>> > }
>>>> >
>>>> > function unlockTables() {
>>>> > return execDatabase('UNLOCK TABLES');
>>>> > }
>>>>
>>>> Kind of, and maybe a bit overkill. But personally I'd put it all in a
>>>> Database class. The code would be more flexible and less prone to
>>>> errors.
>>>>
>>>
>>> Further, I would not have globals. I would pass $db in via the
>>> constructor. Likewise, I would have $inTransaction be a class variable
>>> which gets set by the method startTransaction().
>>>
>>
>> That was part of the class, Sheldon. In fact, I wouldn't even pass $db
>> into the constructor; it (along with $inTransaction, as you note) would
>> be class variables. That way the rest of the program is completely
>> independent of the database (and you can have more than one database
>> active concurrently if you want).
>
> If you don't pass it in with the constructor, then you need something
> like a setDb($parameter) to set the class variable. Passing it along
> with the constructor does not limit you to only one database
> concurrently. After all, you could readily do
>
> $dbObj1 = new DatabaseClass($db1);
> $dbObj2 = new DatabaseClass($db2);
>
> What passing it along inside the constructor does is (a) it saves a line
> of code each time in the invocation process(es) and (b) uniquely
> identifies a particular instance of the class. Since the DatabaseClass
> _MUST_ have the db defined, there is no problem with passing it in with
> the constructor and no reason not to do so.
>

A better way would be to pass the basic information to the constructor -
i.e. the host, userid, password and database name. Let the constructor
build the database object. That way it is completely encapsulated and
the rest of the program is not dependent on a database object. If he
later finds he wants to change database objects (i.e. maybe PDO is to
slow and he wants to use mysqli), he can do so without changing anything
in the rest of the program. The class is now more fully encapsulated.

A very good reason for not passing it to the constructor.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: database access [message #174203 is a reply to message #174198] Thu, 26 May 2011 11:43 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 5/26/2011 9:37 AM, Jivanmukta wrote:
>> Do you really need to use PDO ?
>> Whats wrong with the regular PHP mysql_XXXX() functions ?
>
> I could not solve some problem with mysqli and this problem did not
> occur using PDO. Nobody could help me with this problem on newsgroups.
> If I remember correctly fetching some nonempty query returned false in
> mysqli, in PDO the same query returned proper set.

If the mysql query returned false, there was a problem with the query.
We asked for more information on the query and gave you some debugging
tips, but you never responded.

So yes, we did try to help you here.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: database access [message #174204 is a reply to message #174202] Thu, 26 May 2011 12:07 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 5/26/2011 11:41 AM, Jerry Stuckle wrote:
> On 5/26/2011 10:22 AM, sheldonlg wrote:
>> On 5/26/2011 9:00 AM, Jerry Stuckle wrote:
>>> On 5/26/2011 8:09 AM, sheldonlg wrote:
>>>> On 5/25/2011 5:37 PM, Jerry Stuckle wrote:
>>>> > On 5/25/2011 1:17 PM, Jivanmukta wrote:
>>>> >> I new relatively new to PHP. I need your opinion if the following set
>>>> >> of database functions make sense:
>>>> >>
>>>> >> function openDatabase() {
>>>> >> global $db;
>>>> >> try {
>>>> >> $db = new PDO('mysql:host=' . MYSQL_SERVER . ';dbname=' .
>>>> >> MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD);
>>>> >> $db->exec('SET NAMES latin2');
>>>> >> $db->exec('SET CHARACTER SET latin2 COLLATE latin2_general_ci');
>>>> >> $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
>>>> >> register_shutdown_function('closeDatabase');
>>>> >> } catch (PDOException $e) {
>>>> >> return false;
>>>> >> }
>>>> >> return true;
>>>> >> }
>>>> >>
>>>> >> function closeDatabase() {
>>>> >> global $db, $inTransaction;
>>>> >> if ($inTransaction) {
>>>> >> $db->rollBack();
>>>> >> $inTransaction = false;
>>>> >> }
>>>> >> if (!is_null($db)) {
>>>> >> $db = null;
>>>> >> }
>>>> >> }
>>>> >>
>>>> >> function queryDatabase($sql) {
>>>> >> global $db, $inTransaction;
>>>> >> $result = array();
>>>> >> try {
>>>> >> $qry = $db->query($sql);
>>>> >> if (!empty($qry)) {
>>>> >> $result = $qry->fetchAll();
>>>> >> }
>>>> >> } catch (PDOException $e) {
>>>> >> if ($inTransaction) {
>>>> >> $db->rollBack();
>>>> >> }
>>>> >> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>>> >> }
>>>> >> return $result;
>>>> >> }
>>>> >>
>>>> >> function execDatabase($sql) {
>>>> >> global $db, $inTransaction;
>>>> >> $result = array();
>>>> >> try {
>>>> >> $result = $db->exec($sql);
>>>> >> } catch (PDOException $e) {
>>>> >> if ($inTransaction) {
>>>> >> $db->rollBack();
>>>> >> }
>>>> >> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>>> >> }
>>>> >> return $result;
>>>> >> }
>>>> >>
>>>> >> function startTransaction() {
>>>> >> global $db, $inTransaction;
>>>> >> $db->beginTransaction();
>>>> >> $inTransaction = true;
>>>> >> }
>>>> >>
>>>> >> function commitTransaction() {
>>>> >> global $db, $inTransaction;
>>>> >> $inTransaction = false;
>>>> >> if ($db->commit()) {
>>>> >> return;
>>>> >> }
>>>> >> $db->rollBack();
>>>> >> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>>> >> }
>>>> >>
>>>> >> function rollbackTransaction() {
>>>> >> global $db, $inTransaction;
>>>> >> $inTransaction = false;
>>>> >> if ($db->rollBack()) {
>>>> >> return;
>>>> >> }
>>>> >> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>>> >> }
>>>> >>
>>>> >> function lockTables($tables) {
>>>> >> return execDatabase('LOCK TABLES ' . join(' WRITE, ', $tables) . '
>>>> >> WRITE');
>>>> >> }
>>>> >>
>>>> >> function unlockTables() {
>>>> >> return execDatabase('UNLOCK TABLES');
>>>> >> }
>>>> >
>>>> > Kind of, and maybe a bit overkill. But personally I'd put it all in a
>>>> > Database class. The code would be more flexible and less prone to
>>>> > errors.
>>>> >
>>>>
>>>> Further, I would not have globals. I would pass $db in via the
>>>> constructor. Likewise, I would have $inTransaction be a class variable
>>>> which gets set by the method startTransaction().
>>>>
>>>
>>> That was part of the class, Sheldon. In fact, I wouldn't even pass $db
>>> into the constructor; it (along with $inTransaction, as you note) would
>>> be class variables. That way the rest of the program is completely
>>> independent of the database (and you can have more than one database
>>> active concurrently if you want).
>>
>> If you don't pass it in with the constructor, then you need something
>> like a setDb($parameter) to set the class variable. Passing it along
>> with the constructor does not limit you to only one database
>> concurrently. After all, you could readily do
>>
>> $dbObj1 = new DatabaseClass($db1);
>> $dbObj2 = new DatabaseClass($db2);
>>
>> What passing it along inside the constructor does is (a) it saves a line
>> of code each time in the invocation process(es) and (b) uniquely
>> identifies a particular instance of the class. Since the DatabaseClass
>> _MUST_ have the db defined, there is no problem with passing it in with
>> the constructor and no reason not to do so.
>>
>
> A better way would be to pass the basic information to the constructor -
> i.e. the host, userid, password and database name. Let the constructor
> build the database object. That way it is completely encapsulated and
> the rest of the program is not dependent on a database object. If he
> later finds he wants to change database objects (i.e. maybe PDO is to
> slow and he wants to use mysqli), he can do so without changing anything
> in the rest of the program. The class is now more fully encapsulated.
>
> A very good reason for not passing it to the constructor.

Good point. It would be best to pass those three instead. Point was
that I was making is that it is best to pass what is _ALWAYS_ needed
through the constructor. Your suggestion is better.

--
Shelly
Re: database access [message #174205 is a reply to message #174204] Thu, 26 May 2011 12:14 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 5/26/2011 12:07 PM, sheldonlg wrote:
> On 5/26/2011 11:41 AM, Jerry Stuckle wrote:
>> On 5/26/2011 10:22 AM, sheldonlg wrote:
>>> On 5/26/2011 9:00 AM, Jerry Stuckle wrote:
>>>> On 5/26/2011 8:09 AM, sheldonlg wrote:
>>>> > On 5/25/2011 5:37 PM, Jerry Stuckle wrote:
>>>> >> On 5/25/2011 1:17 PM, Jivanmukta wrote:
>>>> >>> I new relatively new to PHP. I need your opinion if the following
>>>> >>> set
>>>> >>> of database functions make sense:
>>>> >>>
>>>> >>> function openDatabase() {
>>>> >>> global $db;
>>>> >>> try {
>>>> >>> $db = new PDO('mysql:host=' . MYSQL_SERVER . ';dbname=' .
>>>> >>> MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD);
>>>> >>> $db->exec('SET NAMES latin2');
>>>> >>> $db->exec('SET CHARACTER SET latin2 COLLATE latin2_general_ci');
>>>> >>> $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
>>>> >>> register_shutdown_function('closeDatabase');
>>>> >>> } catch (PDOException $e) {
>>>> >>> return false;
>>>> >>> }
>>>> >>> return true;
>>>> >>> }
>>>> >>>
>>>> >>> function closeDatabase() {
>>>> >>> global $db, $inTransaction;
>>>> >>> if ($inTransaction) {
>>>> >>> $db->rollBack();
>>>> >>> $inTransaction = false;
>>>> >>> }
>>>> >>> if (!is_null($db)) {
>>>> >>> $db = null;
>>>> >>> }
>>>> >>> }
>>>> >>>
>>>> >>> function queryDatabase($sql) {
>>>> >>> global $db, $inTransaction;
>>>> >>> $result = array();
>>>> >>> try {
>>>> >>> $qry = $db->query($sql);
>>>> >>> if (!empty($qry)) {
>>>> >>> $result = $qry->fetchAll();
>>>> >>> }
>>>> >>> } catch (PDOException $e) {
>>>> >>> if ($inTransaction) {
>>>> >>> $db->rollBack();
>>>> >>> }
>>>> >>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>>> >>> }
>>>> >>> return $result;
>>>> >>> }
>>>> >>>
>>>> >>> function execDatabase($sql) {
>>>> >>> global $db, $inTransaction;
>>>> >>> $result = array();
>>>> >>> try {
>>>> >>> $result = $db->exec($sql);
>>>> >>> } catch (PDOException $e) {
>>>> >>> if ($inTransaction) {
>>>> >>> $db->rollBack();
>>>> >>> }
>>>> >>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>>> >>> }
>>>> >>> return $result;
>>>> >>> }
>>>> >>>
>>>> >>> function startTransaction() {
>>>> >>> global $db, $inTransaction;
>>>> >>> $db->beginTransaction();
>>>> >>> $inTransaction = true;
>>>> >>> }
>>>> >>>
>>>> >>> function commitTransaction() {
>>>> >>> global $db, $inTransaction;
>>>> >>> $inTransaction = false;
>>>> >>> if ($db->commit()) {
>>>> >>> return;
>>>> >>> }
>>>> >>> $db->rollBack();
>>>> >>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>>> >>> }
>>>> >>>
>>>> >>> function rollbackTransaction() {
>>>> >>> global $db, $inTransaction;
>>>> >>> $inTransaction = false;
>>>> >>> if ($db->rollBack()) {
>>>> >>> return;
>>>> >>> }
>>>> >>> error(ERRMSG_DATABASE_OPERATION_FAILED);
>>>> >>> }
>>>> >>>
>>>> >>> function lockTables($tables) {
>>>> >>> return execDatabase('LOCK TABLES ' . join(' WRITE, ', $tables) . '
>>>> >>> WRITE');
>>>> >>> }
>>>> >>>
>>>> >>> function unlockTables() {
>>>> >>> return execDatabase('UNLOCK TABLES');
>>>> >>> }
>>>> >>
>>>> >> Kind of, and maybe a bit overkill. But personally I'd put it all in a
>>>> >> Database class. The code would be more flexible and less prone to
>>>> >> errors.
>>>> >>
>>>> >
>>>> > Further, I would not have globals. I would pass $db in via the
>>>> > constructor. Likewise, I would have $inTransaction be a class variable
>>>> > which gets set by the method startTransaction().
>>>> >
>>>>
>>>> That was part of the class, Sheldon. In fact, I wouldn't even pass $db
>>>> into the constructor; it (along with $inTransaction, as you note) would
>>>> be class variables. That way the rest of the program is completely
>>>> independent of the database (and you can have more than one database
>>>> active concurrently if you want).
>>>
>>> If you don't pass it in with the constructor, then you need something
>>> like a setDb($parameter) to set the class variable. Passing it along
>>> with the constructor does not limit you to only one database
>>> concurrently. After all, you could readily do
>>>
>>> $dbObj1 = new DatabaseClass($db1);
>>> $dbObj2 = new DatabaseClass($db2);
>>>
>>> What passing it along inside the constructor does is (a) it saves a line
>>> of code each time in the invocation process(es) and (b) uniquely
>>> identifies a particular instance of the class. Since the DatabaseClass
>>> _MUST_ have the db defined, there is no problem with passing it in with
>>> the constructor and no reason not to do so.
>>>
>>
>> A better way would be to pass the basic information to the constructor -
>> i.e. the host, userid, password and database name. Let the constructor
>> build the database object. That way it is completely encapsulated and
>> the rest of the program is not dependent on a database object. If he
>> later finds he wants to change database objects (i.e. maybe PDO is to
>> slow and he wants to use mysqli), he can do so without changing anything
>> in the rest of the program. The class is now more fully encapsulated.
>>
>> A very good reason for not passing it to the constructor.
>
> Good point. It would be best to pass those three instead. Point was that
> I was making is that it is best to pass what is _ALWAYS_ needed through
> the constructor. Your suggestion is better.
>
four, not three.

--
Shelly
Re: database access [message #174206 is a reply to message #174197] Thu, 26 May 2011 13:48 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
.oO(JohnT)

> Looks like real overkill to me.
> Do you really need to use PDO ?
> Whats wrong with the regular PHP mysql_XXXX() functions ?

The mysql_* functions are a bit outdated and mysqli_* isn't as elegant
as PDO. Nothing wrong with using it, especially if you want to use some
"advanced" features like prepared statements. It's a nice and clean
interface and easy to encapsulate and extend in your own DB class as
already mentioned.

Micha
Re: database access [message #174261 is a reply to message #174190] Sun, 29 May 2011 16:41 Go to previous messageGo to next message
sophal chao is currently offline  sophal chao
Messages: 1
Registered: May 2011
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
On May 25, 5:37 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 5/25/2011 1:17 PM, Jivanmukta wrote:
>
>
>
>> I new relatively new to PHP. I need your opinion if the following set
>> of database functions make sense:
>
>> function openDatabase() {
>>      global $db;
>>    try {
>>            $db = new PDO('mysql:host=' . MYSQL_SERVER . ';dbname=' .
>> MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD);
>>            $db->exec('SET NAMES latin2');
>>            $db->exec('SET CHARACTER SET latin2 COLLATE latin2_general_ci');
>>            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
>>            register_shutdown_function('closeDatabase');
>>    } catch (PDOException $e) {
>>            return false;
>>    }
>>      return true;
>> }
>
>> function closeDatabase() {
>>    global $db, $inTransaction;
>>    if ($inTransaction) {
>>            $db->rollBack();
>>            $inTransaction = false;
>>    }
>>            if (!is_null($db)) {
>>            $db = null;
>>    }
>> }
>
>> function queryDatabase($sql) {
>>    global $db, $inTransaction;
>>    $result = array();
>>    try {
>>            $qry = $db->query($sql);
>>            if (!empty($qry)) {
>>                    $result = $qry->fetchAll();
>>            }
>>    } catch (PDOException $e) {
>>            if ($inTransaction) {
>>                    $db->rollBack();
>>            }
>>            error(ERRMSG_DATABASE_OPERATION_FAILED);
>>    }
>>    return $result;
>> }
>
>> function execDatabase($sql) {
>>    global $db, $inTransaction;
>>    $result = array();
>>    try {
>>            $result = $db->exec($sql);
>>    } catch (PDOException $e) {
>>            if ($inTransaction) {
>>                    $db->rollBack();
>>            }
>>            error(ERRMSG_DATABASE_OPERATION_FAILED);
>>    }
>>    return $result;
>> }
>
>> function startTransaction() {
>>            global $db, $inTransaction;
>>            $db->beginTransaction();
>>    $inTransaction = true;
>> }
>
>> function commitTransaction() {
>>            global $db, $inTransaction;
>>    $inTransaction = false;
>>            if ($db->commit()) {
>>            return;
>>            }
>>            $db->rollBack();
>>            error(ERRMSG_DATABASE_OPERATION_FAILED);
>> }
>
>> function rollbackTransaction() {
>>            global $db, $inTransaction;
>>    $inTransaction = false;
>>            if ($db->rollBack()) {
>>            return;
>>            }
>>            error(ERRMSG_DATABASE_OPERATION_FAILED);
>> }
>
>> function lockTables($tables) {
>>    return execDatabase('LOCK TABLES ' . join(' WRITE, ', $tables) . '
>> WRITE');
>> }
>
>> function unlockTables() {
>>    return execDatabase('UNLOCK TABLES');
>> }
>
> Kind of, and maybe a bit overkill.  But personally I'd put it all in a
> Database class.  The code would be more flexible and less prone to errors.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

You can connect use singleton. I think this link is good for basic
tutorial.

http://www.w3cyberlearning.com/tutorial/php_pdomysql.php
Re: database access [message #174263 is a reply to message #174261] Sun, 29 May 2011 23:12 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 5/29/2011 4:41 PM, sophal chao wrote:

>
> You can connect use singleton. I think this link is good for basic
> tutorial.
>
> http://www.w3cyberlearning.com/tutorial/php_pdomysql.php

Singletons have their uses. But they are not appropriate for all
situations, and have nothing to do with the ops question.

But then spamming your site is also frowned on in this newsgroup,
especially when you don't say it is your site. And it's a pretty poor
"tutorial".

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: database access [message #174270 is a reply to message #174206] Mon, 30 May 2011 04:56 Go to previous message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 5/26/2011 7:48 PM, Michael Fesser wrote:
> .oO(JohnT)
>
>> Looks like real overkill to me.
>> Do you really need to use PDO ?
>> Whats wrong with the regular PHP mysql_XXXX() functions ?
>
> The mysql_* functions are a bit outdated and mysqli_* isn't as elegant
> as PDO. Nothing wrong with using it, especially if you want to use some
> "advanced" features like prepared statements. It's a nice and clean
> interface and easy to encapsulate and extend in your own DB class as
> already mentioned.
>
> Micha

I second that. I have been using PDO in my latest projects, and I like
it so far.
Only thing to take care of is dates. (I Insert them as a string and
Postgresql happily takes them correctly, but this could give problems on
other backends.)

Regards,
Erwin Moller

--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
Quick Reply
Formatting Tools:   
  Switch to threaded view of this topic Create a new topic
Previous Topic: Stats comp.lang.php (last 7 days)
Next Topic: a more elegant way?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Dec 14 23:36:14 EST 2017

Total time taken to generate the page: 0.01012 seconds