Hello, I'm new to the forum, but I need help with an error I have on my site. I'm building an online MMORPG similar to KoC but I can't solve this error. It reads:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2
I just can't figure it out. I was up all night last night searching through my code to find the error, but was unable to find it. I have supplied the code below. Please help. Thanks!
<?php include("inc/connect.php");
include("inc/header.inc"); ?>
<?php
if(time() % 1200 < 20 OR time() % 1200 > 1190) {
$output = "A turn is taking place. Please try again in a minute";
}else{
?>
<h3 align="center">BATTLE</h3>
<?php
echo "<div align='center'>";
echo "<table cellpadding='5' border='0'><tr><td>";
echo "Jump to Page</td><td>";
echo "<form action='rankings.php' method='get'>";
echo "<input class='text' type='text' name='page' size='5'>";
echo "<input class='button' type='submit' value='Go'>";
echo "</form></td></tr></table>";
echo "</div><br>";
$connection = mysql_connect($host, $user ,$password);
$db = mysql_select_db($database, $connection);
// ### GETTING INFO - IDS OF RANKED PEOPLE
if(!isset($_GET[page])) {
$_GET[page] = 1;
}
$sql = "SELECT id FROM ranking WHERE rank > 0";
$result = mysql_query($sql) or die(mysql_error());
$num_rows = mysql_num_rows($result);
$num_rows = ceil($num_rows / 50);
if($_GET[page] < 1) { $_GET[page] = 1; }
if($_GET[page] > $num_rows) { $_GET[page] = $num_rows; }
settype($_GET[page], "int");
$arr_id = array();
$arr_name = array();
$arr_gold = array();
$arr_sett = array();
$arr_pop = array();
$add_sql = "";
$start_rank = 50 * ($_GET[page] - 1);
$start_rank = ($start_rank >= 0) ? $start_rank : 0;
$sql = "SELECT id, rank FROM ranking
WHERE rank > 0
ORDER BY rank
LIMIT 50 OFFSET $start_rank";
$result = mysql_query($sql) or die(mysql_error());
$x = 1;
while($ranking = mysql_fetch_assoc($result)) {
$arr_id[$ranking[id]]= $ranking[rank];
$add_sql = "id=" . $ranking[id];
if($x > 1){
$add_sql .= " OR id=".$ranking[id];
}
$x++;
}
// $arr_min = min($arr_id);
$arr_id = array_flip($arr_id);
mysql_free_result($result);
$add_sql = substr($add_sql, 4);
$sql = "SELECT stats.id, stats_gold, stats_population, stats_clan FROM stats
WHERE $add_sql";
$result = mysql_query($sql) or die(mysql_error());
while($stats = mysql_fetch_assoc($result)) {
$arr_gold[$stats[id]] = $stats[stats_gold];
$arr_pop[$stats[id]] = $stats[stats_population];
$result2 = mysql_query($sql);
$clan = mysql_fetch_assoc($result2);
mysql_free_result($result2);
if($clan[members] > 9) {
$arr_clan[$stats[id]] = $clan[tag];
}
}
$sql = "SELECT user, id FROM user
WHERE $add_sql";
$result = mysql_query($sql) or die(mysql_error());
while($user = mysql_fetch_assoc($result)) {
$arr_name[$user[id]] = $user[user];
}
echo "<table align='center' border='0' cellpadding='5' cellspacing='0'>\n<tr><td align='center' width='50'><b>Rank</b></td><td align='center' width='150'><b>Username</b></td><td align='center' width='120'><b>Population</b></td><td><b>Gold</b></td></tr>";
for($i = $arr_min; $i < $arr_min + 50; $i++) {
$pos = $arr_id[$i];
if(isset($arr_name[$pos])) {
echo "<tr><td align='center'>" . number_format($i, 0, '.', ',') . "</td>";
echo "<td align='center'><a href='stats.php?id=" . $pos . "'>" . $arr_name[$pos] . "</a></td>";
echo "<td align='center'>" . number_format($arr_pop[$pos], 0, '.', ',') . "</td>";
echo "<td align='center'>" . number_format($arr_gold[$pos], 0, '.', ',') . "</td></tr>\n";
}
}
echo "</table>";
}
if(isset($output)) {
include('inc/output.inc');
}else{
$next_page = $_GET[page] + 1;
$prev_page = $_GET[page] - 1;
echo "<br><br>";
echo "<table align='center'>";
echo "<tr>";
echo "<td align='center' width='100'>";
if($_GET[page] > 1) {
echo "<a href='rankings.php?page=" . $prev_page . "'>Previous Page</a>";
}
echo "</td>";
echo "<td align='center'>";
echo " Page " . $_GET[page] . " of " . $num_rows . " ";
echo "</td>";
echo "<td align='center' width='100'>";
if($_GET[page] != $num_rows) {
echo " <a href='rankings.php?page=" . $next_page . "'>Next Page</a>";
}
echo "</td>";
echo "</tr>";
echo "</table>";
?>
<?php
echo "<div align='center'>";
echo "<table cellpadding='5' border='0'><tr><td>";
echo "Jump to Page</td><td>";
echo "<form action='rankings.php' method='get'>";
echo "<input class='text' type='text' name='page' size='5'>";
echo "<input class='button' type='submit' value='Go'>";
echo "</form></td></tr></table>";
echo "<br>Search for Player";
echo "<form action='rankings_search.php' method='get'>";
echo "<input class='text' type='text' name='user' size='20'> ";
echo "<input class='button' type='submit' value='Search'>";
echo "</form>";
echo "</div>";
}
?>
<?php include("inc/footer.inc"); ?>