OOP PHP with MySQLi



Hey, i'm looking for the best possible way for connecting to my database
without too much of an effort.

The example works fine and fast. What i need to know is if it's safe, stable
and the way to go. If anyone has a better way for connecting side-wide to
something i'd like you to show me. I've had it working before using 'global'
but people told me 'global' is bad programming.

One of the possibilities would be to make the link and pass it on to every
function and object but this is NOT an option because it makes my script
less transparant and easy to read.


<?php

class My {
private static $connection;

function SQL() {
if(empty(self::$connection)) {
self::$connection = new mysqli("localhost", "user", "password", "db");
}
return self::$connection;
}
}

class User {
function __construct() {
echo "user init<p>";
}

public function show() {
$query = My::SQL()->query("SELECT * FROM user");
while($row = $query->fetch_object()) {
echo $row->login."<p>";
}
}
}

$user = new User();
$user->show();

//Works here too
$query = My::SQL()->query("SELECT * FROM user");
while($row = $query->fetch_object()) {
echo $row->login."<p>";
}


?>


.



Relevant Pages

  • Re: Connect to Access Data from various regional locations
    ... All users will be connecting via broadband. ... When using MSDE ... > Dim rf As DAO.Recordset ... > There is a wizard to split your database into a FE and a BE; ...
    (microsoft.public.access.externaldata)
  • Re: Connect to Access Data from various regional locations
    ... All users will be connecting via broadband. ... When using MSDE ... > Dim rf As DAO.Recordset ... > There is a wizard to split your database into a FE and a BE; ...
    (microsoft.public.access.dataaccess.pages)
  • RE: Problem with connecting to an Oracle database using Perl
    ... Problem with connecting to an Oracle database using Perl ... *IS* described in the DBD::Oracle perldocs - as the perldocs are included ... I do not have a problem connecting to the remote database using the Oracle ...
    (perl.dbi.users)
  • Re: Running SQL script through VB
    ... I am connecting through a default username and my ... The error I am getting while running a script (generated by SQL server ... "A use database statement is not allowed in a procedure or trigger" ...
    (microsoft.public.dotnet.languages.vb)
  • RE: Compact Framework 2.0 to Sql Server 2000 -- cant Open() connectio
    ... I tried connecting to a different data source on another machine: ... datasource, again using a \IERP to get to the correct database, and it failed. ... > I have XP SP 2 on the machine w/ the sql server, ...
    (microsoft.public.dotnet.framework.compactframework)