teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

PHP文を追加

2017/04/07 06:20

投稿

Eimei8104
Eimei8104

スコア16

title CHANGED
File without changes
body CHANGED
@@ -2,4 +2,43 @@
2
2
  ____________________________________
3
3
  Fatal error: Uncaught Error: Call to a member function fetch_fields() on boolean in C:\xampp\htdocs\Weight.php:24 Stack trace: #0 C:\xampp\htdocs\Weight.php(54): db_output(false) #1 {main} thrown in C:\xampp\htdocs\Weight.php on line 24
4
4
  _____
5
- 上記のようなエラー文が出ます。
5
+ 上記のようなエラー文が出ます。
6
+
7
+ PHP文です
8
+ ________
9
+ function db_output($result)
10
+ {
11
+ echo "<table>\n\t<tr>";
12
+ $info = $result->fetch_fields();
13
+ foreach($info as $f)
14
+ {
15
+ echo "<th>".$f->name."</th>";
16
+ }
17
+ echo "</tr>\n";
18
+
19
+ while($kekka=$result->fetch_array(MYSQLI_ASSOC))
20
+ {
21
+ echo "\t<tr>";
22
+ foreach($kekka as $f)
23
+ {
24
+ echo "<td>{$f}</td>";
25
+ }
26
+ echo"</tr>\n";
27
+ }
28
+ echo"</table>\n";
29
+ }
30
+
31
+ $mysqli = new mysqli('localhost');
32
+ if($mysqli->connect_error)
33
+ {
34
+ print('<p>データベースへの接続に失敗しました。</p>'.$mysqli->connect_error);
35
+ exit();
36
+ }
37
+ $mysqli->select_db("yoshida");
38
+ $mysqli->set_charset("utf-8");
39
+
40
+ $sql = "select * from weight";
41
+ $result = $mysqli->query($sql);
42
+ db_output($result);
43
+
44
+ _______