mysql - Update my PHP API -
firstly i've been told use outdated mysql, , should use mysqli, i'm not entirely sure how migrate over.
also, further explain api shows player stats when search name. reason in background it's querying url/0 , it's making page loading extremely slow.
anyhow here's api.php. give me example of mysqli?
<?php // epicmc cms api $date1 = new datetime('now'); $date2 = new datetime('12/12/2014'); $difference = $date1->diff($date2)->days; $link = mysql_connect("localhost", "username", "password"); mysql_select_db("database", $link); if ($_get['task'] == 'total') { $get_db = 'database'; $result = mysql_query("select * $get_db", $link); echo '{"task":"total","amount":"'; echo mysql_num_rows($result); echo '"}'; } elseif ($_get['task'] == 'info') { $get_player = $_get['player']; $get_db = 'simpleauth_players'; $result = mysql_query("select * $get_db name = '" . mysql_real_escape_string($get_player) . "'", $link); while ($data = mysql_fetch_array($result)) { echo '{"task":"view","registered":"'; echo date('m/d/y h:i a', $data['registerdate']); echo '","verified":"'; echo $data['verified']; echo '","banned":"'; echo $data['banned']; echo '","locked":"'; echo $data['locked']; echo '","theme":"'; echo $data['theme']; echo '","email":"'; echo $data['email']; echo '","cover":"'; echo $data['cover']; echo '","ip":"'; echo $data['lastip']; echo '","lastlogin":"'; echo date('m/d/y h:i a', $data['logindate']); echo '","ip":"'; echo $data['lastip']; echo '"}'; } } elseif ($_get['task'] == 'stats') { $get_player = $_get['player']; $get_db = 'player_stats'; $result = mysql_query("select * $get_db name = '" . mysql_real_escape_string($get_player) . "'", $link); while ($data = mysql_fetch_array($result)) { if($data['skin'] == null){ $skin = $data['name']; } else { $skin = $data['skin']; } echo json_encode(array( 'task' => 'viewstats', 'skin' => $skin, 'deaths' => $data['deaths'], 'kills' => $data['kills'], 'joins' => $data['joins'], 'quits' => $data['quits'], 'kicked' => $data['kicked'], 'places' => $data['places'], 'breaks' => $data['breaks'], 'chats' => $data['chats'], // ratio 'ratio' => $data['kills'] / $data['deaths'] )); } } elseif ($_get['task'] == 'login') { $get_user = $_get['user']; $get_db = 'simpleauth_players'; $result = mysql_query("select * $get_db name = '" . mysql_real_escape_string($get_user) . "'", $link); while ($data = mysql_fetch_array($result)) { echo '{"task":"login","password":"'; echo $data['hash']; echo '","lastip":"'; echo $data['lastip']; echo '","timestamp":"'; echo $data['logindate']; echo '"}'; } } else { echo 'online'; } function hashme($player, $password) { return bin2hex(hash("sha512", $password . strtolower($player), true) ^ hash("whirlpool", strtolower($player) . $password, true)); } ?>
basicaly database comes before queries here updated code:
<?php // epicmc cms api $date1 = new datetime('now'); $date2 = new datetime('12/12/2014'); $difference = $date1->diff($date2)->days; $link = mysqli_connect("localhost", "username", "password", "database"); if ($_get['task'] == 'total') { $get_db = 'database'; $result = mysqli_query($link, "select * $get_db"); echo '{"task":"total","amount":"'; echo mysqli_num_rows($result); echo '"}'; } elseif ($_get['task'] == 'info') { $get_player = $_get['player']; $get_db = 'simpleauth_players'; $result = mysqli_query($link, "select * $get_db name = '" . mysqli_real_escape_string($link, $get_player) . "'"); while ($data = mysqli_fetch_array($result)) { echo '{"task":"view","registered":"'; echo date('m/d/y h:i a', $data['registerdate']); echo '","verified":"'; echo $data['verified']; echo '","banned":"'; echo $data['banned']; echo '","locked":"'; echo $data['locked']; echo '","theme":"'; echo $data['theme']; echo '","email":"'; echo $data['email']; echo '","cover":"'; echo $data['cover']; echo '","ip":"'; echo $data['lastip']; echo '","lastlogin":"'; echo date('m/d/y h:i a', $data['logindate']); echo '","ip":"'; echo $data['lastip']; echo '"}'; } } elseif ($_get['task'] == 'stats') { $get_player = $_get['player']; $get_db = 'player_stats'; $result = mysqli_query($link, "select * $get_db name = '" . mysqli_real_escape_string($link, $get_player) . "'"); while ($data = mysqli_fetch_array($result)) { if($data['skin'] == null){ $skin = $data['name']; } else { $skin = $data['skin']; } echo json_encode(array( 'task' => 'viewstats', 'skin' => $skin, 'deaths' => $data['deaths'], 'kills' => $data['kills'], 'joins' => $data['joins'], 'quits' => $data['quits'], 'kicked' => $data['kicked'], 'places' => $data['places'], 'breaks' => $data['breaks'], 'chats' => $data['chats'], // ratio 'ratio' => $data['kills'] / $data['deaths'] )); } } elseif ($_get['task'] == 'login') { $get_user = $_get['user']; $get_db = 'simpleauth_players'; $result = mysqli_query($link, "select * $get_db name = '" . mysqli_real_escape_string($link, $get_user) . "'"); while ($data = mysqli_fetch_array($result)) { echo '{"task":"login","password":"'; echo $data['hash']; echo '","lastip":"'; echo $data['lastip']; echo '","timestamp":"'; echo $data['logindate']; echo '"}'; } } else { echo 'online'; } function hashme($player, $password) { return bin2hex(hash("sha512", $password . strtolower($player), true) ^ hash("whirlpool", strtolower($player) . $password, true)); } ?>
Comments
Post a Comment