-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopic-post389.html
132 lines (117 loc) · 11.7 KB
/
topic-post389.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja" dir="ltr">
<head>
<title>日本PHPユーザ会 掲示板: PHP事はじめ » SQL文内にSQL文を用いたページ</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="description" content="日本PHPユーザ会運営の掲示板です。※ただいまテスト運用中です。" />
<meta name="keywords" content="php,web,php.gr.jp,community,usebb" />
<link rel="stylesheet" type="text/css" href="./templates/phpgrjp0.3/styles.css" />
<link rel="shortcut icon" href="./templates/phpgrjp0.3/gfx/usebb.ico" />
<link rel="alternate" type="application/rss+xml" title="日本PHPユーザ会 掲示板 RSS フィード" href="rss.xml" />
<script type="text/javascript" src="sources/javascript.js"></script>
</head>
<body onload="javascript:init_external()">
<div id="pagebox-bg">
<div id="shadow-left">
<div id="shadow-right">
<p id="logo"><a href="index.html"><img src="./templates/phpgrjp0.3/gfx/usebb.png" alt="UseBB" title="ホーム" /></a></p>
<h1 id="boardname"><span id="line">日本PHPユーザ会 掲示板</span></h1>
<h2 id="boarddescr">日本PHPユーザ会運営の掲示板です。※ただいまテスト運用中です。</h2>
<div id="topmenu"><ul>
<li><a href="index.html">ホーム</a></li><li><a href="faq.html">FAQ</a></li><li><a href="active.html">活発なトピック</a></li>
</ul></div>
<div id="topmenu-shadow"></div>
<p class="locationbar">
• <a href="index.html">日本PHPユーザ会 掲示板</a> » <a href="forum-2.html">PHP事はじめ</a> » SQL文内にSQL文を用いたページ
</p>
<h3 id="forumname"><a href="topic-137.html">SQL文内にSQL文を用いたページ</a></h3>
<p id="forummods"></p>
<p id="toolbartop"> </p>
<p id="pagelinkstop">ページ: 1</p>
<table class="maintable">
<tr>
<th>投稿者</th>
<th>投稿</th>
</tr>
<tr class="tr1">
<td class="postername">
<div class="posternamecontainer"><a href="profile-116.html">sats</a></div>
</td>
<td class="postinfo">
<div class="postlinks"></div>
<div class="postdate"><a href="topic-post386.html#post386" name="post386" rel="nofollow">#1</a> 2009 年 5 月 12 日(火) 22:28</div>
</td>
</tr>
<tr class="tr1">
<td class="posterinfo">
会員
<div class="avatar"></div>
<div class="field">登録者: 7 2008</div>
<div class="field">返信数: 2</div>
<div class="field"></div>
</td>
<td class="postcontent">
<div class="post">初心者です。レベルの低い質問で申し訳ありません。<br /><br />//↓以下、ソースです。<br /><br />$sql = "SELECT * FROM tableA";<br /><br />$result = mysql_query($sql);<br /><br />$i = 0;<br />while ( $row = mysql_fetch_assoc($result) ) {<br /><br /> $id = $row['id'];<br /> $name = $row['name'];<br /> $category = $row['category'];<br /> $tel = $row['tel'];<br /> $fax = $row['fax'];<br /><br />echo '<table><br /><br /> <tr><br /> <td>id</td><br /> <td>' . $id . '</td><br /> </tr><br /><br /> <tr><br /> <td>name</td><br /> <td>' . $name . '</td><br /> </tr><br /><br /> <tr><br /> <td>category</td><br /> <td>' . $category . '</td><br /> </tr><br /><br /> <tr><br /> <td>tel</td><br /> <td>' . $tel . '</td><br /> </tr><br /><br /> <tr><br /> <td>fax</td><br /> <td>' . $fax . '</td><br /> </tr>';<br /><br />++$i;<br /><br />}<br /><br />if ( $i == 0 ) echo '<p>データがありません</p>';<br /><br /><br />echo '</table>';<br /><br /><br />上記から得られる結果<br /><br />+-----+------+----------+-----------+-----------+<br />| id | name | category | tel | fax |<br />+-----+------+----------+-----------+-----------+<br />| 01 | john | 1 | 2232-1232 | 2232-1235 |<br />+-----+------+----------+-----------+-----------+<br />| 02 | paul | 0 | 6233-1532 | 2632-1254 |<br />+-----+------+----------+-----------+-----------+<br />| 03 | jack | 1 | 7652-4262 | 4272-2435 |<br />+-----+------+----------+-----------+-----------+<br /><br />を、別のテーブルと組み合わせて以下のような形にしたいです。<br /><br />+-----+------+----------+-----------+-----------+<br />| id | name | category | tel | fax |<br />+-----+------+----------+-----------+-----------+<br />| 01 | john | student | 2232-1232 | 2232-1235 |<br />+-----+------+----------+-----------+-----------+<br />| 02 | paul | teacher | 6233-1532 | 2632-1254 |<br />+-----+------+----------+-----------+-----------+<br />| 03 | jack | student | 7652-4262 | 4272-2435 |<br />+-----+------+----------+-----------+-----------+<br /><br />そこで、以下の通り複数の SQL 文を用いてみました。<br /><br /><br />$sql = "SELECT * FROM tableA";<br /><br />$result = mysql_query($sql);<br /><br />$i = 0;<br />while ( $row = mysql_fetch_assoc($result) ) {<br /><br /> $id = $row['id'];<br /> $name = $row['name'];<br /> $category = $row['category'];<br /> $tel = $row['tel'];<br /> $fax = $row['fax'];<br /><br />echo '<table><br /><br /> <tr><br /> <td>id</td><br /> <td>' . $id . '</td><br /> </tr><br /><br /> <tr><br /> <td>name</td><br /> <td>' . $name . '</td><br /> </tr><br /><br /> $sql = "SELECT * FROM tableB WHERE tableB.id = '$category'";<br /><br /> $result = mysql_query($sql);<br /><br /> $i = 0;<br /> while ( $row = mysql_fetch_assoc($result) ) {<br /><br /> $catname = $row['catname'];<br /><br /> ++$i;<br /><br /> }<br /><br /> if ( $i == 0 ) echo '<p>データがありません</p>';<br /><br /> <tr><br /> <td>category</td><br /> <td>' . $catname . '</td><br /> </tr><br /><br /> <tr><br /> <td>tel</td><br /> <td>' . $tel . '</td><br /> </tr><br /><br /> <tr><br /> <td>fax</td><br /> <td>' . $fax . '</td><br /> </tr>';<br /><br />++$i;<br /><br />}<br /><br />if ( $i == 0 ) echo '<p>データがありません</p>';<br /><br /><br />echo '</table>';<br /><br /><br />が、得られるのは下記の通りです。・・・前述のように 3 行出てきません。<br /><br /><br />+-----+------+----------+-----------+-----------+<br />| id | name | category | tel | fax |<br />+-----+------+----------+-----------+-----------+<br />| 01 | john | student | 2232-1232 | 2232-1235 |<br />+-----+------+----------+-----------+-----------+<br /><br /><br />明らかに間違っているのは分かるのですが、どのように直してよいのか分か<br />らず、ご教授いただけませんでしょうか?<br /><br />よろしくお願い致します。</div>
</td>
</tr>
<tr class="tr2">
<td class="postername">
<div class="posternamecontainer"><a href="profile-2.html" class="administrator">ELF</a></div>
</td>
<td class="postinfo">
<div class="postlinks"></div>
<div class="postdate"><a href="topic-post387.html#post387" name="post387" rel="nofollow">#2</a> 2009 年 5 月 13 日(水) 12:43</div>
</td>
</tr>
<tr class="tr2">
<td class="posterinfo">
管理者
<div class="avatar"></div>
<div class="field">登録者: 10 2005</div>
<div class="field">返信数: 38</div>
<div class="field"></div>
</td>
<td class="postcontent">
<div class="post">コードのインデントが崩れていて読み違いがあるかもしれないですがご了承ください.<br /><br />SQLの実行結果を$rowに取り込んでいると思うのですが,<br />2種類の実行結果を同一の$rowに取り込んでいませんか?<div class="signature">_______________<div>-- <br />Tadashi "ELF" Jokagi<br /><a href="http://blog.poyo.jp/" title="http://blog.poyo.jp/" rel="external">http://blog.poyo.jp/</a><br /><a href="http://wiki.poyo.jp/PHP/" title="http://wiki.poyo.jp/PHP/" rel="external">http://wiki.poyo.jp/PHP/</a><br /><a href="http://news.hotphpper.net/" title="http://news.hotphpper.net/" rel="external">http://news.hotphpper.net/</a></div></div></div>
</td>
</tr>
<tr class="tr1">
<td class="postername">
<div class="posternamecontainer"><a href="profile-36.html">kona@m</a></div>
</td>
<td class="postinfo">
<div class="postlinks"></div>
<div class="postdate"><a href="topic-post389.html#post389" name="post389" rel="nofollow">#3</a> 2009 年 5 月 13 日(水) 21:46</div>
</td>
</tr>
<tr class="tr1">
<td class="posterinfo">
会員
<div class="avatar"></div>
<div class="field">登録者: 10 2007</div>
<div class="field">返信数: 71</div>
<div class="field"></div>
</td>
<td class="postcontent">
<div class="post"> 補足させて下さい。<br /><br /> 問題のソース中、2回目に出てくる<pre class="code">$result = mysql_query($sql);<br /><br /> $i = 0;<br /> while ( $row = mysql_fetch_assoc($result) ) {<br /></pre>にある変数の名前「$result」を「$result<strong>2</strong>」等のように別の名前に変えて下さい。つまり<pre class="code">$result2 = mysql_query($sql);<br /><br /> $i = 0;<br /> while ( $row = mysql_fetch_assoc($result2) ) {<br /></pre>のようにします。<br /><br /> この例での$resultおよび$result2には、SQL実行結果まるごとのデータと、そのデータをどこまで読み込んだかが記録されていると思って下さい。<br /> この例ではtableAのレコードを1つずつ読み込んで出力するループの中で、別のtableBを読み込んでいます。<br /> この時tableBにおけるSQL実行の際にtableAの時と同じ変数$resultを使ってしまうと、tableAの情報が入っていたはずの$resultはtableBの情報に入れ替わってしまいます。<br /> そしてtableAの2つ目のレコードを読み込もうとした時には「(tableBが)もう次のレコードはありません」となってしまいます。<br /><br /> 話が長くなってしまいましたが、ELFさんがおっしゃっているように、同時にSQL実行結果を使用する場合はmysql_query()の返り値を入れる変数を別々にする必要があるということです。<br /> いかがでしょうか?。</div>
</td>
</tr>
</table>
<p id="toolbarbottom"> </p>
<p id="pagelinksbottom">ページ: 1</p>
<div id="bottomfix"></div>
<p id="actionlinks"></p>
<p class="locationbar">
• <a href="index.html">日本PHPユーザ会 掲示板</a> » <a href="forum-2.html">PHP事はじめ</a> » SQL文内にSQL文を用いたページ
</p>
<p id="linkbar">
</p>
<p id="bottom">
日本PHPユーザ会 掲示板 は <a href="http://www.usebb.net">UseBB 1 フォーラムソフトウェア</a> を使用しています
</p>
<div id="bottom-shadow"></div>
</div>
</div>
</div>
</body>
</html>