/** * Define the default mysql connection to be used for this object. * @param mysqli object $mysqli_connection * @throws Exception */ public static function setMySqlConnection($mysqli_connection) { if (!is_a($mysqli_connection, 'mysqli') || $mysqli_connection->connect_errno) { throw new Exception("Failed to connect to MySQL: (" . $mysqli_connection->connect_errno . ") " . $mysqli_connection->connect_error); } self::$mysqli_connection = $mysqli_connection; }
public function getRowsAtRank($rank, $total = 1) { if (!isset($this->_additional_where)) { return parent::getRowsAtRank($rank, $total); } $rank = intval(self::$mysqli_connection->real_escape_string($rank)); $total = intval(self::$mysqli_connection->real_escape_string($total)); $query = "SELECT * " . "FROM `{$this->table_name}` " . "WHERE " . $this->_additional_where . " " . "ORDER BY {$this->row_score} DESC " . "LIMIT {$rank}, {$total}"; $res = self::$mysqli_connection->query($query); if (!$res) { throw new Exception("Query rows failed: (" . self::$mysqli_connection->errno . ") " . self::$mysqli_connection->error); } if ($res->num_rows === 0) { return array(); } $rows = array(); while ($row = $res->fetch_assoc()) { $rows[] = $row; } return $rows; }
$score = $mysqli->real_escape_string($score); $query = "INSERT INTO {$table}(" . $data_row . ",score) VALUES ('{$name}', '{$score}')"; if (!$mysqli->query($query)) { echo "Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error; } // show some info about records being added. if ($time < time() - 10) { echo "{$i} records created.\n"; $time = time(); } } } elseif ($number == 2) { require_once 'SimpleRanking.php'; try { SimpleRanking::setMySqlConnection($mysqli); $simple_ranking = new SimpleRanking($table, $row_score, $rank_row); $simple_ranking->run(); } catch (Exception $e) { print $e->getMessage() . "\n"; die; } } elseif ($number == 3) { require_once 'SimpleRanking.php'; $stdin = fopen('php://stdin', 'r'); print "Which name to search?\n"; fscanf($stdin, "%s\n", $name); // reads number from STDIN $total_time = microtime(true); try { SimpleRanking::setMySqlConnection($mysqli); $simple_ranking = new SimpleRanking($table, $row_score, $rank_row);