私はPHPを使ってMySQLiでコーディングするのがとても新しいです。私は現在、以下に示すようにいくつかのテスト値を入力したデータベースに2つのテーブルを持つ単純なアポイントユーザアプリケーションをコーディングしようとしています。'User'テーブルのuserIDと一致する 'Appointment'テーブルのすべての値を選択して表示するためのベストプラクティスは何ですか?
私は本当に最善を尽くしていますが、私はまだMySQLiについて何も知らないし、学びたいと思っています。私はすでにこのサイトの周りを見回して昨日すべてを過ごしましたが、私の問題に答えた以前の投稿を見逃すことはありません。私のような初心者をお手伝いいただきありがとうございます。
- を、私は別のセッションでのユーザーのログインセッションを保存した:私は、これまで取り組んできた何
。それは正常に動作し、ユーザー名のログインに基づいて正しいユーザー行を取得します。
- マッチングユーザID
と$_SESSION['appointment']
ディスプレイ特定のユーザのアポイントメントアレイを問題
$_SESSION['appointment']
のみ一致する特定のユーザのためtheappointment配列の最初の結果を表示ユーザーID。それはuserID = 3
に一致する同じ場合であっても私の場合は
$_SESSION['appointment']['date']
= 2016-12-20
を表示し、すべての彼の他の予定2016-12-31
と2016-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();}
}
私はすでにそれを試して、それは私にエラーを与えた:致命的なエラー:未定義のメソッドmysqli_result :: fetch_all()を呼び出す... – 5120bee
UPDA TE:インターネットを掘り起こした後、私はこれを見つけました: "変数$ stmtはmysqli_stmt型であり、mysqli_result型ではありません。mysqli_stmtクラスにはメソッドfetch_assoc()が定義されていません。 – 5120bee
fetch_all()メソッドと同じか代替のものがありますか? – 5120bee