2013-06-03 37 views
-1

我有這個OOP選擇我正在使用,我想添加JOIN的能力。我的新代碼雖然沒有生成數組。誰能幫忙?OOP選擇包括一個JOIN

這是我最初的簡單選擇這就像一個夢

public function select($table, $rows = '*', $where = null, $order = null) 
{ 
    $q = 'SELECT '.$rows.' FROM '.$table; 
    if($where != null) 
     $q .= ' WHERE '.$where; 
    if($order != null) 
     $q .= ' ORDER BY '.$order; 

    $query = @mysql_query($q); 
    if($query) 
    { 
     $this->numResults = mysql_num_rows($query); 
     for($i = 0; $i < $this->numResults; $i++) 
     { 
      $r = mysql_fetch_array($query); 
      $key = array_keys($r); 
      for($x = 0; $x < count($key); $x++) 
      { 
       // Sanitizes keys so only alphavalues are allowed 
       if(!is_int($key[$x])) 
       { 
        if(mysql_num_rows($query) > 1) 
         $this->result[$i][$key[$x]] = $r[$key[$x]]; 
        else if(mysql_num_rows($query) < 1) 
         $this->result = null; 
        else 
         $this->result[$key[$x]] = $r[$key[$x]]; 
       } 
      } 
     } 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

這是我在使用加入添加方式的嘗試,但它沒有返回數組。

public function select($table, $rows = '*', $join = null, $where = null, $order = null){ 
    // Create query from the variables passed to the function 
    $q = 'SELECT '.$rows.' FROM '.$table; 
    if($join != null){ 
     $q .= ' JOIN '.$join; 
    } 
    if($where != null){ 
     $q .= ' WHERE '.$where; 
    } 
    if($order != null){ 
     $q .= ' ORDER BY '.$order; 
    } 
    // Check to see if the table exists 
    if($this->tableExists($table)){ 
     // The table exists, run the query 
     $query = @mysql_query($q); 
     if($query){ 
      // If the query returns >= 1 assign the number of rows to numResults 
      $this->numResults = mysql_num_rows($query); 
      // Loop through the query results by the number of rows returned 
      for($i = 0; $i < $this->numResults; $i++){ 
       $r = mysql_fetch_array($query); 
       $key = array_keys($r); 
       for($x = 0; $x < count($key); $x++){ 
        // Sanitizes keys so only alphavalues are allowed 
        if(!is_int($key[$x])){ 
         if(mysql_num_rows($query) > 1){ 
          $this->result[$i][$key[$x]] = $r[$key[$x]]; 
         }else if(mysql_num_rows($query) < 1){ 
          $this->result = null; 
         }else{ 
          $this->result[$key[$x]] = $r[$key[$x]]; 
         } 
        } 
       } 
      } 
      return true; // Query was successful 
     }else{ 
      array_push($this->result,mysql_error()); 
      return false; // No rows where returned 
     } 
    }else{ 
     return false; // Table does not exist 
    } 
} 
+5

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 –

+1

這是什麼OOP? –

+0

你有沒有檢查過你的查詢??試試echo $ query;並執行以檢查查詢是否正確 –

回答

4

如果要調用此功能,您應該聲明它是這樣的:

public function select($table, $rows, $where, $order) 
{ 
     // your statements 
} 

然後調用它像這樣:

select('your_table','rows_you_want_select','where_conditions','column_you_want_to_sort'); 

在另一方面,如果你想加入聲明,聲明如下:

public function select($table, $rows, $join, $where, $order) 
    { 
      // your statements 
    } 

然後調用它像這樣:

select('your_table','rows_you_want_select','join_conditions','where_conditions','column_you_want_to_sort'); 

例子:

select('table1','id',' table2 on table1.id = table2.id','id = 1','id'); 

你仍然有很多東西需要學習FUNCTIONAL PROGRAMMING並請避免使用mysql_。請使用mysqli_PDO。如果你確實是在OOP之後,那麼我強烈建議PDO