Notice: Undefined variable: 変数
変数が未定義というエラー(箇所:index.php)
【functions.php で定義した変数を】
`
<?php function select_comments($dbh){ $sql = "SELECT * FROM board"; $stmt = $dbh->prepare($sql); $stmt->execute(); $count = $stmt->rowCount(); while($row = $stmt->fetch(PDO::FETCH_ASSOC)){ $data[] = $row; } return $data; } ?>`
【index.php でエラーがでます。】
`
<table border=1> <tr><th>名前</th><th>プロフィール</th><th>時刻</th></tr> <?php foreach($data as $row): ?> <tr> <td><?php echo $row['name'] ?></td> <td><?php echo $row['profile'] ?></td> <td><?php echo $row['created'] ?></td> </tr> <?php endforeach; ?> </table> `functions.php を使わずにindex.phpのみで記述するとエラーにはなりません。
検索すると
変数のスコープ・変数の有効範囲はローカル関数の中、の問題のような感じです。
実際には、何を追加すれば解決できるのか分かりません
よろしくお願い致します!
回答1件
あなたの回答
tips
プレビュー