PHP 中級 講座

<< $pdf->Cellで1文字ずれてしまいます 質問一覧 ini_set() の影響範囲と有効期限を教... >>
  • キーワードが設定されていません

  • 0P
Mysqlの実行コードが無限ループしてしまう。

正しい書き方のはずなんですが、無限ループしてしまいます。
  1. <?php
  2. class config{
  3.  
  4.             public function connection_to_mysql ($ret = true$port = '3306') {
  5.                 $connection = mysql_connect($this->database['IP'] . ':' . $port$this->database['HOST']$this->database['PASSWORD']) or die (mysql_error());
  6.                 mysql_select_db($this->database['NAME']$connection) or die (mysql_error());
  7.                 mysql_query('SET NAMES UTF8');
  8.                 if ($ret == true) {
  9.                     return $connection;
  10.                 }
  11.             }
  12.             public function mysql_attach_basic ($first$table$where$set = ''$objecting = false$limit = 1) {
  13.                 $self = '';
  14.                 $first = strtoupper($first);
  15.                 switch ($first) {
  16.                     case 'SELECT':
  17.                     case 'DELETE':
  18.                         $usewhere = '';
  19.                         $asterisk = '';
  20.                         if ( $where != '' ) {
  21.                             $usewhere = 'WHERE ';
  22.                         }
  23.                         if ( $first == 'SELECT' ) {
  24.                             $asterisk = ' * ';
  25.                         }
  26.                         $self = $first . $asterisk . ' FROM `' . $table . '' . $usewhere . $where;
  27.                     break;
  28.                     case 'INSERT':
  29.                         $self = $first . $asterisk . ' INTO `' . $table . '` (' . $where . ') VALUES (' . $set . ')';
  30.                     break;
  31.                     case 'UPDATE':
  32.                         $add = '';
  33.                         $usewhere = '';
  34.                         if ( $where != '' ) {
  35.                             $usewhere = 'WHERE ';
  36.                         }
  37.                         if ( $set != '' ) {
  38.                             $add = 'SET ' . $set;
  39.                         }
  40.                         $self = $first . ' ' . $add . '`' . $table . '' . $usewhere . $where;
  41.                     break;
  42.                 }
  43.                 $query = $this->mysql_attach_escape($self);
  44.                 $db = mysql_query($query$this->connection_to_mysql());
  45.                 if ( $objecting == true ) {
  46.                     $db = mysql_fetch_object($db);
  47.                 }
  48.                 $this->mysql_attach_action($con = null$query = null);
  49.                 return $db;
  50.             }
  51.  
  52.             private function mysql_attach_escape($query) {
  53.                 $query = preg_replace('/"(.*?)"/''"' . mysql_real_escape_string('$1'$this->connection_to_mysql()) . '"'$query);
  54.                 $query = preg_replace("/'(.*?)'/"'"' . mysql_real_escape_string('$1'$this->connection_to_mysql()) . '"'$query);
  55.                 return $query;
  56.             }
  57.  
  58.             private function mysql_attach_action($con = null$query = null) {
  59.                 if ($con != null) {
  60.                     if ($query != null) {
  61.                         @mysql_free_result($query);
  62.                     }
  63.                     @mysql_close($con);
  64.                 }
  65.             }
  66.  
  67. }
  68. ?>

というコードで、使用方法は、
  1. <?php
  2.  
  3.     $config = new config();
  4.     $config->init();
  5.     $a = $config->mysql_attach_basic('SELECT''accounts'''''true);
  6.     while ($b = $a) {
  7.  
  8.     }
  9. ?>
という風にやっています。

以前のプログラムもこんな感じに書いたのですが、それは実行できています。
相違点がまったくもって分かりません。
以前のプログラム
  1. <?php
  2. class server_system {
  3.    function attach_mysql() {
  4.      $connect = mysql_connect($this->ip$this->database_id$this->database_password);
  5.      mysql_select_db($this->database_name$connect);
  6.      mysql_query('SET NAMES UTF8');
  7.      return $connect;
  8.    }
  9.  
  10.    function attach_mysql_query_escape($query) {
  11.      $query = preg_replace('/"(.*?)"/''"' . mysql_real_escape_string('$1') . '"'$query);
  12.      $query = preg_replace("/'(.*?)'/"'"' . mysql_real_escape_string('$1') . '"'$query);
  13.      return $query;
  14.    }
  15.  
  16.    function attach_mysql_query($query$unreturn = false) {
  17.      $connect = $this->attach_mysql();
  18.      if ($unreturn == false) {
  19.         $set = mysql_query($this->attach_mysql_query_escape($query)$connect) or die($this->debug(mysql_error()1));
  20.      } else {
  21.         mysql_query($this->attach_mysql_query_escape($query)$connect) or die($this->debug(mysql_error()1));
  22.      }
  23.      $this->attach_mysql_action($connect);
  24.      if ($unreturn == false) {
  25.        return $set;
  26.      }
  27.    }
  28.  
  29.    function attach_mysql_action($con = null$query = null) {
  30.      if ($con != null) {
  31.        if ($query != null) {
  32.          @mysql_free_result($query);
  33.        }
  34.        @mysql_close($con);
  35.      }
  36.    }
  37. }
  38. ?>

この質問への意見の募集は締め切られ、ポイントは既に配分されました。
意見を投稿することはできますが、ポイントを受け取ることはできません。

QMysqlの実行コードが無限ループしてしまう。 memory  [02月06日 01時18分] 
┗参考になったRe:Mysqlの実行コードが無限ループしてしまう。 kaitau  [02月06日 13時01分] 
  ┗Re:Re:Mysqlの実行コードが無限ループしてしまう。 memory  [02月06日 15時54分] 
    ┗Re:Re:Re:Mysqlの実行コードが無限ループしてしまう。 memory  [02月06日 18時13分] 

コメント一覧

並び替え( ツリー順 / 投稿順[降順] / 投稿順[昇順]

Re:Mysqlの実行コードが無限ループしてしまう。

とりあえず、「無限ループ」ってありますけどループしてる箇所や、
再帰関数が返ってこないような類の場所はありません。
大量のデータのテーブルに対して複雑なクエリをかければ実行時間が
非常に遅くなることはままありますが、どうみてもそんな現場の
雰囲気でもありませんね。

おそらく、呼び出し側の
  1. $a = $config->mysql_attach_basic('SELECT', 'accounts', '', '', true);
  2. while ($b = $a) {
  3.  
  4. }
のループの中の処理の方が問題なのでしょう。
まずそちらを調べた方が良いかと。

---- 本題ここまで ----




ところで、率直に言ってクラスの設計がかなりよろしくないかと。
多分覚えたてなんだと思うけど、こういう「自分ルール」的な
パラメータの用法をすると拡張する時とかバグを直す時に苦しみます。
PEARのライブラリのソースをよく読んで勉強した方が良いと思います。
SQLの組み立ても、単一のメソッドで済むほど簡単な世界ではありません。

なにより、最低限エスケープを何のためにするか知ってください。
$query全体にかけても意味ないから。

Re:Re:Mysqlの実行コードが無限ループしてしまう。

返信ありがとうございます。

クラスについては、まだ作り途中のもので、一時的にデバッグを行った結果です。
まだ改善点があるようなので、肝に銘じておきます。

エスケープについてですが、特殊文字など、ソフトウェア側が解釈するためまたは、文字化けの引き金となる文字を処理するためだと独学で学びました。
海外のフォーラムで、文字列に対してmysql_real_escape_stringをするといいということが書いてありましたので、こういった処理をしました。

もう一度見直してみます。
返信ありがとうございました。

Re:Re:Re:Mysqlの実行コードが無限ループしてしまう。

自己解決しました。
一度変数に入れてから、実行すると、できました。

お手数おかけしました。
$pdf->Cellで1文字ずれてしまいます 質問一覧 ini_set() の影響範囲と有効期限を教...