PHP Code:
// To access a member variable from within a member function,
// you must use the special variable (also called a pseudo-variable) $this.
// It refers to the current instance of the object itself.
// You combine it with the dereferencing operator (->) to access the member variables.
class mysql
{
private $databaseHost, $databaseUser, $databasePass, $databaseName, $link_id;
function __construct( $databaseHost, $databaseUser, $databasePass, $databaseName )
{
// these variables are equal to the __construct function arguments
$this->databaseHost = $databaseHost;
$this->databaseUser = $databaseUser;
$this->databasePass = $databasePass;
$this->databaseName = $databaseName;
$this->connect();
}
public function connect()
{
$this->link_id = mysql_connect( $this->databaseHost, $this->databaseUser, $this->
databasePass );
if ( ! $this->link_id )
{
$this->oops( "Could not connect to server" );
}
if ( ! mysql_select_db($this->databaseName, $this->link_id) )
{
$this->oops( "Cannot open database" );
}
}
public function oops( $msg = '' )
{
if ( $this->link_id > 0 )
{
$this->error = mysql_error( $this->link_id );
$this->errno = mysql_errno( $this->link_id );
}
$this->error = mysql_error();
$this->errno = mysql_errno();
echo $msg;
if ( strlen($this->error) > 0 )
echo $this->error;
}
}
// instantiate
$mysql = new mysql( "localhost", "root", "password", "database" );
Bookmarks