前提
PHPでMySQLを使用いてデータベースを結合できずに困っています。
調べた通りに行ったつもりですができなかったので、お力を貸していただけませんか?
実現したいこと
MstUserのUserIDとTrnSalesのUserIDが同じ値のデータ(UserName)を取り出したい。
該当のソースコード
PHP
1 <!-- レコードがある数繰り返す 変数にレコードの結果を入れる --> 2 <?php while ($row = $stmt->fetch()) : ?> 3 <!-- SalesIsCancelに1が入っている場合 --> 4 <?php if ($row['SalesIsCancel'] === "1") : ?> 5 <div class='l-wrap table__wrap'><table class='table__table is_cancel'> 6 <?php else : ?> 7 <div class='l-wrap table__wrap'><table class='table__table'> 8 <?php endif ; ?> 9 <!-- 売上ID --> 10 <tr class='table__tr'> 11 <th class='table__th'>No.</td> 12 <td class='table__td'><?php echo $row['SalesID']; ?></td> 13 </tr> 14 <!-- 顧客名 --> 15 <?php 16 // 変数にSQL文を入れる MstUserとTrnSalesのテーブルを結合 17 $strSQLFunction = "SELECT 18 TrnSales.UserID, 19 MstUser.UserID, 20 UserName 21 FROM TrnSales JOIN MstUser ON TrnSales.UserID = MstUser.UserID;"; 22 // SQL実行 23 $dbcFunction = connect(); 24 $stmtFunction = $dbcFunction->prepare($strSQLFunction); 25 $stmtFunction->execute(); 26 $rowFunction = $stmtFunction->fetch(); 27 ?> 28 <tr class='table__tr'> 29 <th class='table__th'>顧客名</td> 30 <td class='table__td'><?php echo $rowFunction['UserName']; ?></td> 31 </tr> 32 <!-- 売上金額 --> 33 <tr class='table__tr'> 34 <th class='table__th'>金額</td> 35 <td class='table__td'><?php echo h($row['UserID']); ?>円</td> 36 </tr> 37<?php endwhile ; ?>
補足情報
回答1件
あなたの回答
tips
プレビュー