2016-11-22 3 views
0

私はPHPを使ってMySQLiでコーディングするのがとても新しいです。私は現在、以下に示すようにいくつかのテスト値を入力したデータベースに2つのテーブルを持つ単純なアポイントユーザアプリケーションをコーディングしようとしています。'User'テーブルのuserIDと一致する 'Appointment'テーブルのすべての値を選択して表示するためのベストプラクティスは何ですか?

私は本当に最善を尽くしていますが、私はまだMySQLiについて何も知らないし、学びたいと思っています。私はすでにこのサイトの周りを見回して昨日すべてを過ごしましたが、私の問題に答えた以前の投稿を見逃すことはありません。私のような初心者をお手伝いいただきありがとうございます。

  • を、私は別のセッションでのユーザーのログインセッションを保存した:私は、これまで取り組んできた何

    。それは正常に動作し、ユーザー名のログインに基づいて正しいユーザー行を取得します。

  • マッチングユーザID

$_SESSION['appointment']ディスプレイ特定のユーザのアポイントメントアレイを問題

  • $_SESSION['appointment']のみ一致する特定のユーザのためtheappointment配列の最初の結果を表示ユーザーID。それはuserID = 3

に一致する同じ場合であっても私の場合は

  • は、それだけで、私はすべてを表示するにはどうすればよいボブ・マーリーのため$_SESSION['appointment']['date'] = 2016-12-20を表示し、すべての彼の他の予定2016-12-312016-12-29を表示しません。 'appointment'テーブルに一致するuserIDがある場合、ユーザーの関連する予定の日付?

    私のデータベーステーブル:

    'users' table:

    userID (column 1) // username (column 2) // name (column 3)

    1 // user1 // Bob Dylan

    2 // user2 // John Lennon

    3 // user3 // Bob Marley

    -

    'appointment' table

    'appointmentID' (column 1) // userID (column 2) // date (column 3)

    1 (appointmentID) // 1 (userID) // 2016-12-07

    2 (appointmentID) // 2 (userID) // 2016-12-15

    3 (appointmentID) // 3 (userID) // 2016-12-20

    4 (appointmentID) // 3 (userID) // 2016-12-31

    5 (appointmentID) // 3 (userID) // 2016-12-29

    マイSESSIONS:

    //Displays the array of the user 
    $_SESSION['profile'] = displayProfile($_SESSION['user']); 
    
    //Displays the array of the appointment based on the matching userID 
    $_SESSION['appointment'] = displayAppointment($_SESSION['profile']['userID']); 
    

    マイmysqliの機能:

    function displayProfile($username) 
    { 
        global $ db_con; //my database connection 
    
        $query = 'SELECT * FROM users WHERE username=\''.mysqli_escape_string($db_con, $username).'\''; 
        $result = $db_con-> query($query); 
    
        //This fetches the associative array for the user row 
        //(i.e. $_SESSION['profile']['userID'] outputs the userID of the user logged into the system 
        if ($result->num_rows) { return $result->fetch_assoc();} 
    } 
    
        ////////////////////////////// 
    
        function displayAppointment($userID) 
    { 
        global $ db_con; //my database connection 
    
        //This passes the current logged on user's userID into the query. 
        //For example, if Bob Marley was currently logged on, the $userID that will be passed is userID = 3. 
        $query = 'SELECT * FROM appointment WHERE userID='.$userID; 
        $result = $db_con-> query($query); 
    
        //This fetches the associative array for the appointment row with the correct matching userID 
        //(i.e. $_SESSION['profile']['userID'] for Bob Marley is '3' 
        if ($result->num_rows) { return $result->fetch_assoc();} 
    } 
    
  • 答えて

    1

    ので、何を必要とすることは右、あなたが

    fetch_all()

    editenoteを使うべき機能からすべてを返すことです。彼はfetch_all

    function displayAppointment($userID) 
    { 
        global $ db_con; //my database connection 
    
        //This passes the current logged on user's userID into the query. 
        //For example, if Bob Marley was currently logged on, the $userID that will be passed is userID = 3. 
        $query = 'SELECT * FROM appointment WHERE userID='.$userID; 
        $result = $db_con-> query($query); 
    
        //This fetches the associative array for the appointment row with the correct matching userID 
        //(i.e. $_SESSION['profile']['userID'] for Bob Marley is '3' 
        $resultout=array(); 
        while ($row = mysqli_fetch_assoc($result)) { 
         array_push($resultout, $row); 
        } 
        return $resultout; 
    } 
    
    用のドライバを持っているdoesntのbecouse
    +0

    私はすでにそれを試して、それは私にエラーを与えた:致命的なエラー:未定義のメソッドmysqli_result :: fetch_all()を呼び出す... – 5120bee

    +0

    UPDA TE:インターネットを掘り起こした後、私はこれを見つけました: "変数$ stmtはmysqli_stmt型であり、mysqli_result型ではありません。mysqli_stmtクラスにはメソッドfetch_assoc()が定義されていません。 – 5120bee

    +0

    fetch_all()メソッドと同じか代替のものがありますか? – 5120bee

    関連する問題