#エラー
Warning: mysqli_fetch_assoc() expects exactly 1 parameter, 2 given in C:\xampp\htdocs\index.php on line 43
#やりたいこと
phpでmysqlからデータをとってきて出力したいのですが43行目で引っかかってるようです...
#試したこと
初心者で何もわからないので検索しました。
https://www.php.net/manual/ja/function.mysql-fetch-assoc.php
パラメータが足りないのでしょうか(間違ってたらごめんなさい)?
はっきり言ってよくわからないです
php
1<?php 2require('connect.php'); 3$page = $_REQUEST['page']; 4if ($page == '') { 5$page = 1; 6} 7$page = max($page, 1); 8 9// 最終ページを取得する 10$sql = 'SELECT COUNT(*) AS cnt FROM my_items'; 11$recordSet = mysqli_query($db,$sql); 12$table = mysqli_fetch_assoc($db,$recordSet); 13$maxPage = ceil($table['cnt'] / 5); 14$page = min($page, $maxPage); 15 16$start = ($page - 1) * 5; 17 18$recordSet = mysqli_query($db,'SELECT m.name, i.* FROM maker m, my_items i WHERE m.id=i.maker_id ORDER BY id DESC LIMIT ' . $start . ',5'); 19?> 20<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 21<html xmlns="http://www.w3.org/1999/xhtml"> 22<head> 23<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 24<link rel="stylesheet" type="text/css" href="style.css" /> 25<title>商品管理</title> 26</head> 27 28<body> 29<div id="wrap"> 30<div id="head"> 31<h1>商品管理</h1> 32</div> 33 34<div id="content"> 35 <table width="100%"> 36 <tr> 37 <th scope="col">ID</th> 38 <th scope="col">メーカー</th> 39 <th scope="col">商品名</th> 40 <th scope="col">価格</th> 41 </tr> 42<?php 43while ($table = mysqli_fetch_assoc($db,$recordSet)) { 44?> 45 <tr> 46 <td><?php print(htmlspecialchars($table['id'])); ?></td> 47 <td><?php print(htmlspecialchars($table['maker_id'])); ?></td> 48 <td><?php print(htmlspecialchars($table['item_name'])); ?></td> 49 <td><?php print(htmlspecialchars($table['price'])); ?></td> 50 </tr> 51<?php 52} 53?> 54 </table> 55 56<ul class="paging"> 57<?php 58if ($page > 1) { 59?> 60<li><a href="index.php?page=<?php print($page - 1); ?>">前のページへ</a></li> 61<?php 62} else { 63?> 64<li>前のページへ</li> 65<?php 66} 67?> 68<?php 69if ($page < $maxPage) { 70?> 71<li><a href="index.php?page=<?php print($page + 1); ?>">次のページへ</a></li> 72<?php 73} else { 74?> 75<li>次のページへ</li> 76<?php 77} 78?> 79</ul> 80 81</div> 82 83<div id="foot"> 84<p><img src="images/txt_copyright.png" width="136" height="15" alt="(C) H2O Space. MYCOM" /></p> 85</div> 86 87</div> 88</body> 89</html> 90
#変更後
PHP
1<?php 2require('connect.php'); 3$page = $_REQUEST['page']; 4if ($page == '') { 5$page = 1; 6} 7$page = max($page, 1); 8 9// 最終ページを取得する 10$sql = 'SELECT COUNT(*) AS cnt FROM my_items'; 11$recordSet = mysqli_query($db,$sql); 12$table = mysqli_fetch_assoc($db,$recordSet); 13$maxPage = ceil($table['cnt'] / 5); 14$page = min($page, $maxPage); 15 16$start = ($page - 1) * 5; 17 18$recordSet = mysqli_query($db,'SELECT m.name, i.* FROM maker m, my_items i WHERE m.id=i.maker_id ORDER BY id DESC LIMIT ' . $start . ',5'); 19?> 20<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 21<html xmlns="http://www.w3.org/1999/xhtml"> 22<head> 23<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 24<link rel="stylesheet" type="text/css" href="style.css" /> 25<title>商品管理</title> 26</head> 27 28<body> 29<div id="wrap"> 30<div id="head"> 31<h1>商品管理</h1> 32</div> 33 34<div id="content"> 35 <table width="100%"> 36 <tr> 37 <th scope="col">ID</th> 38 <th scope="col">メーカー</th> 39 <th scope="col">商品名</th> 40 <th scope="col">価格</th> 41 </tr> 42<?php 43while ($table = mysqli_fetch_assoc($recordSet)) { 44?> 45 <tr> 46 <td><?php print(htmlspecialchars($table['id'])); ?></td> 47 <td><?php print(htmlspecialchars($table['maker_id'])); ?></td> 48 <td><?php print(htmlspecialchars($table['item_name'])); ?></td> 49 <td><?php print(htmlspecialchars($table['price'])); ?></td> 50 </tr> 51<?php 52} 53?> 54 </table> 55 56<ul class="paging"> 57<?php 58if ($page > 1) { 59?> 60<li><a href="index.php?page=<?php print($page - 1); ?>">前のページへ</a></li> 61<?php 62} else { 63?> 64<li>前のページへ</li> 65<?php 66} 67?> 68<?php 69if ($page < $maxPage) { 70?> 71<li><a href="index.php?page=<?php print($page + 1); ?>">次のページへ</a></li> 72<?php 73} else { 74?> 75<li>次のページへ</li> 76<?php 77} 78?> 79</ul> 80 81</div> 82 83<div id="foot"> 84<p><img src="images/txt_copyright.png" width="136" height="15" alt="(C) H2O Space. MYCOM" /></p> 85</div> 86 87</div> 88</body> 89</html> 90
43行目が
while ($table = mysqli_fetch_assoc($recordSet)) {
になってます
#変更後のエラー
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\index.php on line 43
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/12 05:22
2019/04/12 05:24
2019/04/12 05:30
2019/04/12 05:33