私のスクリプトをこれまで見直してみたところ、try/catch
内の複数の呼び出しまたはそれぞれを呼び出す複数のtry/catch
の複数の呼び出しを使用してデータを取得/Php/pdo/mysql複数のDBがベストプラクティスを呼び出す
いずれの方法を使用しても、結果は正常に表示されますが、テストデータが実生活ではないため、実稼働環境で使用されるベストプラクティスに修正したいと考えています。私は例えばtry/catch
の複数の呼び出しで正しい例外を取得しない可能性があるかどうか疑問に思っています。 30デシベルはtry/catch
を使用して6 open/close db
で30デシベルの呼び出しのような私が持っている他の人に、try/catch
を使用して30 open/close db
で呼び出すように私が持っているいくつかのスクリプトで
。
ありがとうございました。
例方法1
try {
$connexion = new PDO("mysql:host=$serveur;dbname=$db;charset=$charset", $login, $pwd);
$connexion -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
select table1/execute/process
join table1 & table2/execute/process
update table3/execute/process
...
$connexion = null; // close
} catch (PDOException $e) {
echo "Connexion failed:".$e -> getMessage();
}
例方法2
try {
$connexion = new PDO("mysql:host=$serveur;dbname=$db;charset=$charset", $login, $pwd);
$connexion -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
select table1/execute/process
$connexion = null; // close
} catch (PDOException $e) {
echo "Connexion failed:".$e -> getMessage();
}
try {
$connexion = new PDO("mysql:host=$serveur;dbname=$db;charset=$charset", $login, $pwd);
$connexion -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
join table1 & table2/execute/process
$connexion = null; // close
} catch (PDOException $e) {
echo "Connexion failed:".$e -> getMessage();
}
try {
$connexion = new PDO("mysql:host=$serveur;dbname=$db;charset=$charset", $login, $pwd);
$connexion -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
update table3/execute/process
$connexion = null; // close
} catch (PDOException $e) {
echo "Connexion failed:".$e -> getMessage();
}
更新
私は(私は自分でPHPを学んでいる)何か間違ったことを知っていました。一方、私はShudhansh Shekharとe4c5によって作られたソリューションの価値を理解することができます。
私はDBaccessクラスを作成しました。私はすべての利点を見ることができます。スクリプトの一番上に新しいインスタンスが含まれていれば、ほぼ完了です。私は1つのスクリプトでテストし、それは魅力的なように動作しています。
すべてのスクリプトをリエンジニアリングする前に、自分のコードを検証していただけますか? $this->connexion = null;
がdbを閉じる正しい場所にあるかどうかはわかりません。
もう一度お手数をおかけします。 試みを使用してデシベル近い/ 30オープンで30デシベルの呼び出しのような私が持っているいくつかのスクリプトで
DBaccess.class.php
<?php
class DBaccess{
private $serveur = "localhost";
private $db_name = "qh61test";
private $charset = "utf8";
private $login = "root";
private $pwd = "";
private $connexion; // dbh name
private $erreur;
private $requete; // sql stmt
public function __construct(){
$this->connexion = null; // <----------- NOT SURE ?
try{
$this->connexion = new PDO("mysql:host=" . $this->serveur . ";dbname=" . $this->db_name. ";charset=". $this->charset, $this->login, $this->pwd);
$this->connexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
echo "Connexion foirée :".$e -> getMessage();
// $this->erreur = $e->getMessage();
}
}
public function sql($sql){
$this->requete = $this->connexion->prepare($sql);
}
public function execute(){
return $this->requete->execute();
}
public function resultAll(){
$this->execute();
return $this->requete->fetchAll(PDO::FETCH_ASSOC);
}
}
?>
script.php
<?php
session_start();
include 'DBaccess.class.php';
$db = new DBaccess();
...
if (isset($_POST['periode'])) {
// sales within a period
if (!empty($_POST['datebeg']) && !empty($_POST['dateend'])) {
// datepicker dates
$datebeg = preg_replace("([^0-9/])", "", $_POST['datebeg']);
$dateend = preg_replace("([^0-9/])", "", $_POST['dateend']);
$search_row = "SELECT COUNT(cmdID) as nbcmd FROM Commande2 WHERE date_cmd BETWEEN '$datebeg' AND '$dateend'"
);
$db->sql("$search_row");
$db->execute();
$resultat = $db->resultAll();
foreach($resultat as $row)
{
if ($row['nbcmd'] == 0) {
$no_rows = "No sales for that period";
$all = '<div>
<button class="btn btn-mini btn-primary bold" type="submit" name="all">All saless</button></div>';
}
}
}
}
// retrieve all sales or sales within period
if (empty($errors) && empty($no_rows) || isset($_POST['all'])) {
$search_tot = array(
"select" => "SELECT COUNT(cmdID) as nbcmd, SUM(`articles_cmd`) as total_articles, SUM(`total_cmd_nofp`) as somme, AVG(`total_cmd_nofp`) as moyenne, MIN(`total_cmd_nofp`) as mini, MAX(`total_cmd_nofp`) as maxi, MIN(`date_cmd`) as date_mini, MAX(`date_cmd`) as date_maxi FROM Commande2",
"where" => " WHERE ",
"periode" => "date_cmd BETWEEN '$datedeb' AND '$datefin'"
);
if (empty($datebeg) && empty($dateend)) {
unset($search_tot["where"],$search_tot["periode"]);
}
}
$search_tot = implode(' ', $search_tot);
// fetch all matching rows
$db->sql("$search_tot");
$db->execute();
$resultat = $db->resultAll();
foreach($resultat as $row)
{
$date1 = date('d-m-Y', strtotime($row['date_mini']));
$date1 = str_replace('-', '/', $date1);
$date2 = date('d-m-Y', strtotime($row['date_maxi']));
$date2 = str_replace('-', '/', $date2);
$orders= '<div style="text-align: center; font-size: 16px; font-weight: bold;">Sales for '.$date1.' to '.$date2.'/div>';
...
}
...
なぜDBハンドラクラスを作成するのが好きではないのですか?...あなたは$ this-> dbという1つの行コードだけで静かなコードを作成します。 –
これらのクエリが成長し続けるなら、私はあなたのデータベースを焼くことを心配しています –
mysqlの接続プロトコルは特に重いものではありませんが、毎回接続/実行/切断のリソースはまだ無駄です。それはまた、それらがすべてあなたが切断するたびに破壊/クリーンアップされるので、マルチクエリートランザクション、サーバサイド変数の保持などを行うことを不可能にします。 –