Database class in PHP; has only one function - the question of practice and is this a good idea

So, I have this database class in PHP, and I have only one function (except __constructand __destruct). Let me explain further ...

I originally wrote this when I connected to the database, I just call my function connect_to_db(), which will return a mysqli object. Then I used the function objects ( ->query(), ->prepare(), ->bind_param()etc.) to do anything. This is cluttered (and still such that I haven't switched my code to my new class yet) with a lot of functions that just do certain things, for example:

function get_country_info($db, $usrid) {
  $statement = "select `name`, `population`, `money`, `food` from `countries` where `usr_id` = ?";
  $qry = $db->prepare($statement) or
    trigger_error("get_country_info():statement failed...");
  $qry->bind_param("i", $usrid);
  $qry->execute();
  $qry->bind_result($name, $population, $money, $food);
  $qry->fetch();
  $res = array("name" => $name, 
    "pop" => $population, 
    "money" => $money, 
    "food" => $food);

  return $res;
}

, , , , , - , mysqli ( ) . $db->close();, script , .

; query(). . eval(). , , , , , eval() ; ?

, , , ( ), , . , " " - , , , , - ( ) PHP,

, , . , - ?

: http://pastebin.org/353721, ~ 60 , . ( , , , -), , , , , .

.

+3
5

. , , .

, , , : SQL-, . , - , SQL: ( ...).

SQL . , . query() , "" Db . - , , , SQL. .

, , ORM ( ), .

eval: . , "goto", , , . , , ... .

+4
  • eval call_user_func() call_user_func_array().
  • execute().
  • , , .
  • trim() ltrim(rtrim()).

get_country_info() db. - , . , . , , , .

+2

Techpriester: . :

  • if-else
  • sql ( )

, sql ( , , where...) . . DAO .

: ... ( , ). PDO, Doctrine, Propel , ZendFramework ( ...), .

, , - OO Systems. , , , ( 2.x:-)).

+1

-: eval() , . $qry->...?

-, - . , .

-: SQL-, , . , - SQL. .

-: mysqli. PDO.

, , SELECT . , , , .

SQL , , - . , . SQL. SQL, , Doctrine ORM. SQL ...

eval(): :

  • .
  • IDE
  • , eval()
  • eval(), , .
0

- - , , " ", eval() , ..

http://dibiphp.com/cs/ PHP.

Java, -. , iBatis, PHP

- lib DB, :

$aasAreas = Array();
$sSQL = "SELECT id, x, y, x2 AS r, popis FROM ".$oFW->GetOption('tables.areas')
         ." WHERE id_ad=".asq((int)$_GET['id']);
$oRes = $oFW->GetDB()->Select($sSQL);
if(!$oRes || !$oRes->IsOk()){ $sError = $oRes->GetError(); break; }
else while( $a = $oRes->FetchRow() ){
   $aasAreas[] = $a;
}

[OT] ORM PHP :

// Load by ID
echo "<h3>Load by ID</h3>";
$iID = 1;
echo "<pre>\$oObject = \$oOP->LoadObjectById(".$oClass->GetName().", $iID);</pre>";
$oUser = $oOP->LoadObjectById($oClass, $iID);
echo "<pre>oUser: [".gettype($oUser)."]".AdjustedPrintR($oUser)."</pre>";
echo "<div>GetPoolCount(): ".$oOP->GetPoolCount()."</div>";

// Save
echo "<h3>Save</h3>";
$oUser = new cObjectPersistenceTestClass_User();
$oUser->SetId(1);
$oUser->SetProperty('user', 'as'.rand());
$oUser->SetProperty('pass', 'as');
$oUser->SetProperty('fname', 'Astar');
$oUser->SetProperty('lname', 'Seran');
echo "<pre>oUser: [".gettype($oUser)."]".AdjustedPrintR($oUser)."</pre>";
$bSucc = $oOP->SaveObject($oUser);
echo "<div>".($bSucc ? 'saved' : 'error')."</div>";

// Load by value - load object created above -> Object Pool hit
$xVal = $oUser->GetProperty('user');
$aoUsers = $oOP->LoadObjectsByValue($oClass, 'user', $xVal);
echo "<pre>\$aoUsers: [".gettype($aoUsers)."]".AdjustedPrintR($aoUsers)."</pre>";

.

0

Source: https://habr.com/ru/post/1751326/


All Articles