Mis rij na SQL query en HTML opbouw via PHP
Ben redelijk nieuw in PHP en helemaal nieuw in data opvragen via SQL querys. Tot nu toe ben ik redelijk ver gekomen zonder enig probleem. Echter als ik via een SQL query gegevens ophaal uit mijn DB en ik deze via een While loop wil laten zien op mijn pagina dan mis ik het eerste record uit mijn query.
Heb nu een erg lelijke work arround ingesteld dat hij VOOR de loop het eerst record in HTML echo'd maar ik wil dat alles binnen de loop komt te staan en ik dus geen lelijke dingen dubbel in mij code heb staan. Wat doe ik verkeerd?
Code (php)
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
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
<?php
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());
$result = mysql_query("SELECT Username, Kills, (Kills / Deaths) as kdratio FROM playerdb order by Kills desc")
or die(mysql_error());
$kdratio = mysql_query("SELECT Username, Kills, (Kills / Deaths) as kdratio FROM playerdb order by kdratio desc LIMIT 0, 10")
or die(mysql_error());
$row = mysql_fetch_array( $result );
$kdrow = mysql_fetch_array( $kdratio );
$rank = 1;
echo "<table border='1'>";
echo "<tr><td valign='top'>";
echo "<h1>Most kills</h1>";
echo "<table border='1'>";
echo "<tr> <th>Rank</th> <th>Nickname</th> <th>Kills</th> <th>KillDeath Ratio</th> </tr>";
echo "<tr><td>";
echo $rank;
echo "</td><td>";
echo $row['Username'];
echo "</td><td>";
echo $row['Kills'];
echo "</td><td>";
echo $row['kdratio'];
echo "</td></tr>";
while(($row = mysql_fetch_array( $result )) && ($row['Kills'] >= 10)) {
$rank += 1;
echo "<tr><td>";
echo $rank;
echo "</td><td>";
echo $row['Username'];
echo "</td><td>";
echo $row['Kills'];
echo "</td><td>";
echo $row['kdratio'];
echo "</td></tr>";
}
echo "</table>";
$rank = 1;
echo "</td><td valign='top'>";
echo "<h1>Top 10 KD ratio</h1>";
echo "<table border='1'>";
echo "<tr> <th>Rank</th> <th>Nickname</th> <th>Kills</th> <th>KillDeath Ratio</th> </tr>";
echo "<tr><td>";
echo $rank;
echo "</td><td>";
echo $kdrow['Username'];
echo "</td><td>";
echo $kdrow['Kills'];
echo "</td><td>";
echo $kdrow['kdratio'];
echo "</td></tr>";
while($kdrow = mysql_fetch_array( $kdratio )) {
$rank += 1;
echo "<tr><td>";
echo $rank;
echo "</td><td>";
echo $kdrow['Username'];
echo "</td><td>";
echo $kdrow['Kills'];
echo "</td><td>";
echo $kdrow['kdratio'];
echo "</td></tr>";
}
echo "</td></tr>";
echo "</table>";
?>
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());
$result = mysql_query("SELECT Username, Kills, (Kills / Deaths) as kdratio FROM playerdb order by Kills desc")
or die(mysql_error());
$kdratio = mysql_query("SELECT Username, Kills, (Kills / Deaths) as kdratio FROM playerdb order by kdratio desc LIMIT 0, 10")
or die(mysql_error());
$row = mysql_fetch_array( $result );
$kdrow = mysql_fetch_array( $kdratio );
$rank = 1;
echo "<table border='1'>";
echo "<tr><td valign='top'>";
echo "<h1>Most kills</h1>";
echo "<table border='1'>";
echo "<tr> <th>Rank</th> <th>Nickname</th> <th>Kills</th> <th>KillDeath Ratio</th> </tr>";
echo "<tr><td>";
echo $rank;
echo "</td><td>";
echo $row['Username'];
echo "</td><td>";
echo $row['Kills'];
echo "</td><td>";
echo $row['kdratio'];
echo "</td></tr>";
while(($row = mysql_fetch_array( $result )) && ($row['Kills'] >= 10)) {
$rank += 1;
echo "<tr><td>";
echo $rank;
echo "</td><td>";
echo $row['Username'];
echo "</td><td>";
echo $row['Kills'];
echo "</td><td>";
echo $row['kdratio'];
echo "</td></tr>";
}
echo "</table>";
$rank = 1;
echo "</td><td valign='top'>";
echo "<h1>Top 10 KD ratio</h1>";
echo "<table border='1'>";
echo "<tr> <th>Rank</th> <th>Nickname</th> <th>Kills</th> <th>KillDeath Ratio</th> </tr>";
echo "<tr><td>";
echo $rank;
echo "</td><td>";
echo $kdrow['Username'];
echo "</td><td>";
echo $kdrow['Kills'];
echo "</td><td>";
echo $kdrow['kdratio'];
echo "</td></tr>";
while($kdrow = mysql_fetch_array( $kdratio )) {
$rank += 1;
echo "<tr><td>";
echo $rank;
echo "</td><td>";
echo $kdrow['Username'];
echo "</td><td>";
echo $kdrow['Kills'];
echo "</td><td>";
echo $kdrow['kdratio'];
echo "</td></tr>";
}
echo "</td></tr>";
echo "</table>";
?>
Gewijzigd op 05/09/2011 17:22:18 door Patrick van Kampen
$row['Kills'] bestaat nog niet, en daarom is die false en word de eerste loop overgeslagen.
Je haalt op regel 12 en 13 al de eerste rows op.
- SanThe - op 05/09/2011 18:24:11:
Je haalt op regel 12 en 13 al de eerste rows op.
Thanks dat heeft het probleem opgelost, wist niet dat het statement zo werkte. Bedankt voor het verduidelijken :) Weer wat geleerd!