前提・実現したいこと
予約機能を作っています。店側の予約確認画面を作っているのですが、HTML内のforeachでデータを取得できません。
原因は$valueがNULLになっているからだと思うのですが、なぜNULLになっているのかがわからず苦戦しています。
*データベース接続確認済み
*reserve_shop_id = 2
データベースに2のデータは入っています、
$value
var_dump($value);
→NULL
$reserve_result
var_dump($reserve_result);
→string(47) "SELECT * FROM reserve WHERE reserve_shop_id = 2"
発生している問題・エラーメッセージ
var_dump($value);実行時に Notice: Undefined variable: value in C:\xampp\htdocs\shop_home.php on line 107
該当のソースコード
<?php error_reporting(E_ALL); ini_set("display_errors",1); define('PAGE_LIMIT', 10); session_start(); try { $pdo = new PDO('mysql:host=localhost;dbname=reserve;charset=utf8','root','', array(PDO::ATTR_EMULATE_PREPARES => false)); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); if(!isset($_SESSION['user'])) { header("Location: shop_index.php"); } // ユーザーIDからユーザー名を取り出す $query = "SELECT * FROM shop WHERE shop_id=".$_SESSION['user'].""; $result = $pdo->query($query); if (!$result) { print('ユーザークエリーが失敗しました。' . $pdo->error); $pdo->close(); exit(); } //ユーザー情報の取り出し while ($row = $result->fetch(PDO::FETCH_ASSOC)) { $shop_name = $row['shop_name']; $shop_id = $row['shop_id']; } // shopIDからshop名を取り出す $reserve_query = "SELECT * FROM reserve WHERE reserve_shop_id = ".$shop_id; $reserve_result = $pdo->query($reserve_query); if (!$reserve_result) { print('予約クエリーが失敗しました。' . $pdo->error); $pdo->close(); exit(); } // ユーザー情報の取り出し $arr_shop = []; while ($rows = $reserve_result->fetch(PDO::FETCH_ASSOC)) { $arr_shop[] = [ 'reserve_shop_id' => $rows['reserve_shop_id'], 'count' => $rows['count'], 'time' => $rows['time'], 'reserve_comment' => $rows['reserve_comment'], 'reserve_time' => $rows['reserve_time'], ]; } // Total件数 $totalRow = $pdo->query("SELECT FOUND_ROWS() as total"); $all = $totalRow->fetch(PDO::FETCH_ASSOC); } catch (PDOException $e) { var_dump($reserve_query); die(); } ?> <!DOCTYPE HTML> <html lang="ja"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>店舗側マイページ</title> <link rel="stylesheet" href="style.css"> <!-- Bootstrap読み込み(スタイリングのため) --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css"> </head> </head> <body> <div class="col-xs-6 col-xs-offset-3" style="text-align:center;"> <div class="header-right"> <p><?php echo $shop_name; ?>ログイン中</p> <a href="shop_logout.php?logout">ログアウト</a> </div> <div class="main"> <div class="col-xs-12" > <p class="alert alert-success">予約数<?= number_format($all['total']) ?>件</p> <table class="table"> <thead> <tr> <th>人数</th> <th>時間</th> <th>コメント</th> <th>予約時間</th> </tr> </thead> <tbody> <?php foreach($reserve_result as $key => $value) : ?> ←この$value <tr> <td><?= $value['count'] ?></td> <td><?= $value['time'] ?></td> <td><?= $value['reserve_comment'] ?></td> <td><?= $value['reserve_time'] ?></td> <tr> <?php endforeach; ?> <?php var_dump($value); ?> </tbody> </table> </div> </div> </div> </body> </html>
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答3件
あなたの回答
tips
プレビュー