2012-01-02 3 views
0

私は、以下の「コース」クラスがあります:私はクラスを宣言し、機能「getCourseInfoByID」を実行しようとするとPHP OOP:奇妙なアレイ戻る

class Course { 
    // The constructor just sets the database object 
    public function __construct($mysqli) { 
     $this->mysqli = $mysqli; 
    } 
    public function getCourseInfoByID($id) { 
     $result = $this->mysqli->query("SELECT * FROM courses WHERE id='$id'"); 
     $course_info = $result->fetch_array(); 

     // If found, return the student object 
     if($course_info) { 
      return $course_info; 
     } 
     return FALSE; 
    } 
} 

、私は奇妙な結果を得る(下記参照)

$cid = process_get_request('cid'); 
$course = new Course($mysqli); 

if(! $course_info = $course->getCourseInfoByID($cid)) { 
    $error[] = "Invalid Course ID"; 
    setError(); 
    redirectTo("instructors.php"); 
} 
print_r($course_info); 

私はこの取得:

Array ([0] => 2 [id] => 2 [1] => 1 [course_type_id] => 1 [2] => 1 [instructor_id] => 1 [3] => Tooele [dz_name] => Tooele [4] => 4 Airport Road [dz_address] => 4 Airport Road [5] => Tooele [dz_city] => Tooele [6] => Utah [dz_state] => Utah [7] => 84020 [dz_zip] => 84020 [8] => [dz_email] => [9] => 2011-12-30 17:25:12 [created] => 2011-12-30 17:25:12 [10] => 2012-01-02 16:24:08 [start_date] => 2012-01-02 16:24:08 [11] => 2012-01-08 16:24:17 [end_date] => 2012-01-08 16:24:17 [12] => 10 [student_slots] => 10 [13] => Brett will also be assisting in teaching this course as Nathan's assistant. Brett paid Nathan quite well to be his assistant. [notes] => Brett will also be assisting in teaching this course as Nathan's assistant. Brett paid Nathan quite well to be his assistant. [14] => 0 [approved_by] => 0 [15] => 0000-00-00 00:00:00 [approved_on] => 0000-00-00 00:00:00 [16] => 0 [completed] => 0) 

なぜ各レコードが重複しているの?

答えて

3

これは、数値索引と結合索引の両方を戻しているために発生しています。 fetch_assoc()を使用するか、適切な定数MYSQLI_ASSOCまたはMYSQLI_NUMを渡して、それらのキーだけを返す必要があります。


mysqli_result::fetch_array()のドキュメントを参照してください。

脇には、あなたが間違って無効な引数を渡すことができないようにmysqliクラスを渡すようにコンストラクタをヒントします。

0

fetch_array()メソッドの場合はthe docsを読んでください。 2番目のパラメータのデフォルトはMYSQLI_BOTHです。