phpでdb処理を書こうとしています。以下はその一部です。
いちいちprepare〜bind〜executeで記述してそのたびにifで判定して$this->errorにセットしてreturnで帰るやり方をしていますが
正直、もっと別のやり方はないものだろうかとも思います。
同様の処理を短く、かつ可読性の高い処理方法はないものでしょうか?
php
1 private function searchAndCreateTable($tableName){ 2 require(dirname(__FILE__)."/db_config.php"); 3 4 //テーブルそのものの確認 5 $string="show tables like :tableName"; 6 $stmt=$this->PDO->prepare($string); 7 if( $stmt===false ){ 8 $this->errorMessage="seaechAndCreateTable showTables prepare error"; 9 $this->$tableName=$string; 10 $this->PDO=null; 11 return false; 12 } 13 $stmt->bindvalue(":tableName",$tableName); 14 $result=$stmt->execute(); 15 if( $result===false ){ 16 $this->errorMessage="seaechAndCreateTable showTables execute error"; 17 $this->$tableName=$string; 18 $this->PDO=null; 19 return false; 20 }else if( $result===0 ){ 21 //結果が0ならテーブル作成 22 $string="create table :tableName ()" 23 24 25 } 26 27 //指定カラムの確認 28 $string="show columns from ".$tableName." where field in (". 29 implode(',',array_fill(0,count($$tableName),'?')). 30 ")"; 31 $stmt=$this->PDO->prepare($string); 32 if( $stmt===false ){ 33 $this->errorMessage="searchAndCreateTable search colum prepare error"; 34 $this->$tableName=$string; 35 $this->PDO=null; 36 return false; 37 } 38 $result=$stmt->execute( array_keys($$tableName) ); 39 if( $result===false ){ 40 $this->errorMessage="searchAndCreateTable search colum execute error"; 41 $this->PDO=null; 42 $this->$tableName=$string; 43 return false; 44 } 45 return $result; 46 } 47
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/31 07:36