If you are writing a plug-in that requires database connectivity, it’s probably best not to assume anything and connect to the db server afresh. A better alternative could be to check if a connection has already been made and if it still exists. With this information you could then determine whether to make a connection or not. Is there a php function that can check for an existing connection, or should I create a function for this purpose? Something similar to :
Code: Select all
<?php
function checkServerConnection() {
$pingServer = @mysql_ping();
if (!$pingServer) {
//connect to the database
}
}?>
Connect via the __construct()?
Code: Select all
<?php
class Pressume {
function __construct() {
$pingServer = @mysql_ping();
if (!$pingServer) {
//connect to the database
}
}
}?>
Connecting in the method.
Code: Select all
<?php
class Pressume {
function checkForArbValue() {
$this->connectToDb();
//continue with rest of the function.
}
}?>Code: Select all
<?php
$something = new Pressume;
$something->connectToDb();
// etc.
?>