feat(uoj/1/app): modify the DB operation to support PHP7

In order to support PHP7, change the way to operate database. PHP7
removed the mysql extension, so the old way to operate database is
not usable. This commit use a new way to operate database.

BREAKING CHANGE: the way to operate database has changed.
This commit is contained in:
ceabrobot 2017-11-25 15:29:18 +00:00 committed by Masco Skray
parent babd30364e
commit 0ec962b4af
25 changed files with 143 additions and 126 deletions

View File

@ -39,9 +39,9 @@
$esc_name = $_POST['name'];
$esc_name = $purifier->purify($esc_name);
$esc_name = mysql_real_escape_string($esc_name);
$esc_name = DB::escape($esc_name);
mysql_query("insert into contests (name, start_time, last_min, status) values ('$esc_name', '$start_time_str', ${_POST['last_min']}, 'unfinished')");
DB::query("insert into contests (name, start_time, last_min, status) values ('$esc_name', '$start_time_str', ${_POST['last_min']}, 'unfinished')");
};
$time_form->succ_href="/contests";
$time_form->runAtServer();
@ -53,4 +53,4 @@
$time_form->printHTML();
?>
</div>
<?php echoUOJPageFooter() ?>
<?php echoUOJPageFooter() ?>

View File

@ -29,7 +29,7 @@
{
return "失败:无效电子邮箱。";
}
$esc_email = mysql_real_escape_string($email);
$esc_email = DB::escape($email);
DB::update("update user_info set email = '$esc_email' where username = '{$myUser['username']}'");
if ($_POST['Qtag'])
@ -39,7 +39,7 @@
{
return "失败无效QQ。";
}
$esc_qq = mysql_real_escape_string($qq);
$esc_qq = DB::escape($qq);
DB::update("update user_info set qq = '$esc_qq' where username = '{$myUser['username']}'");
}
else
@ -47,7 +47,7 @@
if ($_POST['sex'] == "U" || $_POST['sex'] == 'M' || $_POST['sex'] == 'F')
{
$sex = $_POST['sex'];
$esc_sex = mysql_real_escape_string($sex);
$esc_sex = DB::escape($sex);
DB::update("update user_info set sex = '$esc_sex' where username = '{$myUser['username']}'");
}

View File

@ -39,26 +39,26 @@
$cur = queryZanVal($id, $type, $myUser);
if ($cur != $delta) {
$row = mysql_fetch_array(mysql_query("select zan from $table_name where id = $id"));
$row = DB::selectFirst("select zan from $table_name where id = $id");
if ($row == null) {
die('<div class="text-danger">failed</div>');
}
$cur += $delta;
if ($cur == 0) {
mysql_query("delete from click_zans where username = '{$myUser['username']}' and type = '$type' and target_id = $id");
DB::query("delete from click_zans where username = '{$myUser['username']}' and type = '$type' and target_id = $id");
} else if ($cur != $delta) {
mysql_query("update click_zans set val = '$cur' where username = '{$myUser['username']}' and type = '$type' and target_id = $id");
DB::query("update click_zans set val = '$cur' where username = '{$myUser['username']}' and type = '$type' and target_id = $id");
} else {
mysql_query("insert into click_zans (username, type, target_id, val) values ('{$myUser['username']}', '$type', $id, $cur)");
DB::query("insert into click_zans (username, type, target_id, val) values ('{$myUser['username']}', '$type', $id, $cur)");
}
$cnt = $row['zan'] + $delta;
mysql_query("update $table_name set zan = $cnt where id = $id");
DB::query("update $table_name set zan = $cnt where id = $id");
} else {
$row = mysql_fetch_array(mysql_query("select zan from $table_name where id = $id"));
$row = DB::selectFirst("select zan from $table_name where id = $id");
if ($row == null) {
die('<div class="text-danger">failed</div>');
}
$cnt = $row['zan'];
}
?>
<?= getClickZanBlock($type, $id, $cnt, $cur) ?>
<?= getClickZanBlock($type, $id, $cnt, $cur) ?>

View File

@ -18,9 +18,9 @@
}
if (isset($_POST['check_notice'])) {
$result = mysql_query("select * from contests_notice where contest_id = '${contest['id']}' order by time desc limit 1");
$result = DB::query("select * from contests_notice where contest_id = '${contest['id']}' order by time desc limit 1");
try {
while ($row = mysql_fetch_array($result)) {
while ($row = DB::fetch($result)) {
if (new DateTime($row['time']) > new DateTime($_POST['last_time'])) {
die(json_encode(array('msg' => $row['title'] . ' : ' . $row['content'], 'time' => UOJTime::$time_now_str)));
}
@ -44,18 +44,18 @@
$problems = array();
$prob_pos = array();
$n_problems = 0;
$result = mysql_query("select problem_id from contests_problems where contest_id = ${contest['id']} order by problem_id");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$result = DB::query("select problem_id from contests_problems where contest_id = ${contest['id']} order by problem_id");
while ($row = DB::fetch($result, MYSQLI_NUM)) {
$prob_pos[$problems[] = (int)$row[0]] = $n_problems++;
}
$data = array();
if ($contest['cur_progress'] < CONTEST_FINISHED) {
$result = mysql_query("select id, submit_time, submitter, problem_id, score from submissions where contest_id = {$contest['id']} and score is not null order by id");
$result = DB::query("select id, submit_time, submitter, problem_id, score from submissions where contest_id = {$contest['id']} and score is not null order by id");
} else {
$result = mysql_query("select submission_id, date_add('{$contest['start_time_str']}', interval penalty second), submitter, problem_id, score from contests_submissions where contest_id = {$contest['id']}");
$result = DB::query("select submission_id, date_add('{$contest['start_time_str']}', interval penalty second), submitter, problem_id, score from contests_submissions where contest_id = {$contest['id']}");
}
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
while ($row = DB::fetch($result, MYSQLI_NUM)) {
$row[0] = (int)$row[0];
$row[3] = $prob_pos[$row[3]];
$row[4] = (int)$row[4];
@ -63,8 +63,8 @@
}
$people = array();
$result = mysql_query("select username, user_rating from contests_registrants where contest_id = {$contest['id']} and has_participated = 1");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$result = DB::query("select username, user_rating from contests_registrants where contest_id = {$contest['id']} and has_participated = 1");
while ($row = DB::fetch($result, MYSQLI_NUM)) {
$row[1] = (int)$row[1];
$people[] = $row;
}
@ -137,8 +137,8 @@
$start_test_form = new UOJForm('start_test');
$start_test_form->handle = function() {
global $contest;
$result = mysql_query("select id, problem_id, content from submissions where contest_id = {$contest['id']}");
while ($submission = mysql_fetch_array($result, MYSQL_ASSOC)) {
$result = DB::query("select id, problem_id, content from submissions where contest_id = {$contest['id']}");
while ($submission = DB::fetch($result, MYSQLI_ASSOC)) {
if (!isset($contest['extra_config']["problem_{$submission['problem_id']}"])) {
$content = json_decode($submission['content'], true);
if (isset($content['final_test_config'])) {
@ -148,11 +148,11 @@
if (isset($content['first_test_config'])) {
unset($content['first_test_config']);
}
$esc_content = mysql_real_escape_string(json_encode($content));
$esc_content = DB::escape(json_encode($content));
DB::update("update submissions set judge_time = NULL, result = '', score = NULL, status = 'Waiting Rejudge', content = '$esc_content' where id = {$submission['id']}");
}
}
mysql_query("update contests set status = 'testing' where id = {$contest['id']}");
DB::query("update contests set status = 'testing' where id = {$contest['id']}");
};
$start_test_form->submit_button_config['class_str'] = 'btn btn-danger btn-block';
$start_test_form->submit_button_config['smart_confirm'] = '';
@ -202,10 +202,10 @@ EOD;
EOD;
}
sendSystemMsg($user['username'], 'Rating变化通知', $content);
mysql_query("update user_info set rating = {$ratings[$i]} where username = '{$standings[$i][2][0]}'");
mysql_query("update contests_registrants set rank = {$standings[$i][3]} where contest_id = {$contest['id']} and username = '{$standings[$i][2][0]}'");
DB::query("update user_info set rating = {$ratings[$i]} where username = '{$standings[$i][2][0]}'");
DB::query("update contests_registrants set rank = {$standings[$i][3]} where contest_id = {$contest['id']} and username = '{$standings[$i][2][0]}'");
}
mysql_query("update contests set status = 'finished' where id = {$contest['id']}");
DB::query("update contests set status = 'finished' where id = {$contest['id']}");
};
$publish_result_form->submit_button_config['class_str'] = 'btn btn-danger btn-block';
$publish_result_form->submit_button_config['smart_confirm'] = '';
@ -412,7 +412,7 @@ EOD;
global $contest;
$title = DB::escape($_POST['title']);
$content = DB::escape($_POST['content']);
mysql_query("insert into contests_notice (contest_id, title, content, time) values ('{$contest['id']}', '$title', '$content', now())");
DB::query("insert into contests_notice (contest_id, title, content, time) values ('{$contest['id']}', '$title', '$content', now())");
};
$post_notice->runAtServer();
@ -520,4 +520,4 @@ EOD;
<?php } ?>
</div>
</div>
<?php echoUOJPageFooter() ?>
<?php echoUOJPageFooter() ?>

View File

@ -60,9 +60,9 @@
function($type, $username) {
global $contest;
if ($type == '+') {
mysql_query("insert into contests_permissions (contest_id, username) values (${contest['id']}, '$username')");
DB::query("insert into contests_permissions (contest_id, username) values (${contest['id']}, '$username')");
} else if ($type == '-') {
mysql_query("delete from contests_permissions where contest_id = ${contest['id']} and username = '$username'");
DB::query("delete from contests_permissions where contest_id = ${contest['id']} and username = '$username'");
}
}
);
@ -205,7 +205,7 @@
<?php
$row_id = 0;
$result = DB::query("select username from contests_permissions where contest_id = {$contest['id']}");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
while ($row = DB::fetch($result, MYSQLI_ASSOC)) {
$row_id++;
echo '<tr>', '<td>', $row_id, '</td>', '<td>', getUserLink($row['username']), '</td>', '</tr>';
}
@ -227,7 +227,7 @@
<tbody>
<?php
$result = DB::query("select problem_id from contests_problems where contest_id = ${contest['id']} order by problem_id asc");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
while ($row = DB::fetch($result, MYSQLI_ASSOC)) {
$problem = queryProblemBrief($row['problem_id']);
$problem_config_str = isset($contest['extra_config']["problem_{$problem['id']}"]) ? $contest['extra_config']["problem_{$problem['id']}"] : 'sample';
echo '<tr>', '<td>', $problem['id'], '</td>', '<td>', getProblemLink($problem), ' ', "[$problem_config_str]", '</td>', '</tr>';
@ -259,4 +259,4 @@
</div>
<?php endif ?>
</div>
<?php echoUOJPageFooter() ?>
<?php echoUOJPageFooter() ?>

View File

@ -16,7 +16,7 @@
$unregister_form = new UOJForm('unregister');
$unregister_form->handle = function() {
global $myUser, $contest;
mysql_query("delete from contests_registrants where username = '{$myUser['username']}' and contest_id = {$contest['id']}");
DB::query("delete from contests_registrants where username = '{$myUser['username']}' and contest_id = {$contest['id']}");
updateContestPlayerNum($contest);
};
$unregister_form->submit_button_config['class_str'] = 'btn btn-danger btn-xs';
@ -108,4 +108,4 @@
)
);
?>
<?php echoUOJPageFooter() ?>
<?php echoUOJPageFooter() ?>

View File

@ -14,7 +14,7 @@
$register_form = new UOJForm('register');
$register_form->handle = function() {
global $myUser, $contest;
mysql_query("insert into contests_registrants (username, user_rating, contest_id, has_participated) values ('{$myUser['username']}', {$myUser['rating']}, {$contest['id']}, 0)");
DB::query("insert into contests_registrants (username, user_rating, contest_id, has_participated) values ('{$myUser['username']}', {$myUser['rating']}, {$contest['id']}, 0)");
updateContestPlayerNum($contest);
};
$register_form->submit_button_config['class_str'] = 'btn btn-primary';

View File

@ -8,8 +8,8 @@
$visible = isProblemVisibleToUser($problem, $myUser);
if (!$visible && $myUser != null) {
$result = mysql_query("select contest_id from contests_problems where problem_id = {$_GET['id']}");
while (list($contest_id) = mysql_fetch_array($result, MYSQL_NUM)) {
$result = DB::query("select contest_id from contests_problems where problem_id = {$_GET['id']}");
while (list($contest_id) = DB::fetch($result, MYSQLI_NUM)) {
$contest = queryContest($contest_id);
genMoreContestInfo($contest);
if ($contest['cur_progress'] != CONTEST_NOT_STARTED && hasRegistered($myUser, $contest) && queryContestProblemRank($contest, $problem)) {
@ -44,4 +44,4 @@
header("X-Sendfile: $file_name");
header("Content-type: $mimetype");
header("Content-Disposition: attachment; filename=$download_name");
?>
?>

View File

@ -23,7 +23,7 @@
$delete_form = new UOJForm('delete');
$delete_form->handle = function() {
global $hack;
mysql_query("delete from hacks where id = {$hack['id']}");
DB::query("delete from hacks where id = {$hack['id']}");
};
$delete_form->submit_button_config['class_str'] = 'btn btn-danger';
$delete_form->submit_button_config['text'] = '删除此Hack';

View File

@ -42,7 +42,7 @@
$content['first_test_config'] = $content['config'];
$content['config'] = $content['final_test_config'];
unset($content['final_test_config']);
$esc_content = mysql_real_escape_string(json_encode($content));
$esc_content = DB::escape(json_encode($content));
DB::update("update submissions set status = 'Judged, Waiting', content = '$esc_content' where id = ${_POST['id']}");
}
@ -77,11 +77,11 @@
$ok = DB::update("update hacks set success = {$result['score']}, details = '$esc_details' where id = {$_POST['id']}");
if ($ok) {
list($hack_input) = mysql_fetch_array(mysql_query("select input from hacks where id = {$_POST['id']}"), MYSQL_NUM);
list($hack_input) = DB::fetch(DB::query("select input from hacks where id = {$_POST['id']}"), MYSQLI_NUM);
unlink(UOJContext::storagePath().$hack_input);
if ($result['score']) {
list($problem_id) = mysql_fetch_array(mysql_query("select problem_id from hacks where id = ${_POST['id']}"), MYSQL_NUM);
list($problem_id) = DB::selectFirst("select problem_id from hacks where id = ${_POST['id']}", MYSQLI_NUM);
if (validateUploadedFile('hack_input') && validateUploadedFile('std_output')) {
svnAddExtraTest(queryProblemBrief($problem_id), $_FILES["hack_input"]["tmp_name"], $_FILES["std_output"]["tmp_name"]);
} else {
@ -209,4 +209,4 @@
}
echo json_encode($submission);
?>
?>

View File

@ -110,9 +110,9 @@
$result_json = json_encode($result);
if ($is_in_contest) {
mysql_query("insert into submissions (problem_id, contest_id, submit_time, submitter, content, language, tot_size, status, result, is_hidden) values (${problem['id']}, ${contest['id']}, now(), '${myUser['username']}', '$esc_content', '$esc_language', $tot_size, '${result['status']}', '$result_json', 0)");
DB::query("insert into submissions (problem_id, contest_id, submit_time, submitter, content, language, tot_size, status, result, is_hidden) values (${problem['id']}, ${contest['id']}, now(), '${myUser['username']}', '$esc_content', '$esc_language', $tot_size, '${result['status']}', '$result_json', 0)");
} else {
mysql_query("insert into submissions (problem_id, submit_time, submitter, content, language, tot_size, status, result, is_hidden) values (${problem['id']}, now(), '${myUser['username']}', '$esc_content', '$esc_language', $tot_size, '${result['status']}', '$result_json', {$problem['is_hidden']})");
DB::query("insert into submissions (problem_id, submit_time, submitter, content, language, tot_size, status, result, is_hidden) values (${problem['id']}, now(), '${myUser['username']}', '$esc_content', '$esc_language', $tot_size, '${result['status']}', '$result_json', {$problem['is_hidden']})");
}
}
function handleCustomTestUpload($zip_file_name, $content, $tot_size) {
@ -262,4 +262,4 @@ $('#contest-countdown').countdown(<?= $contest['end_time']->getTimestamp() - UOJ
</div>
<?php endif ?>
</div>
<?php echoUOJPageFooter() ?>
<?php echoUOJPageFooter() ?>

View File

@ -526,7 +526,7 @@ EOD
}
$hackable = $problem['hackable'] ? 1 : 0;
mysql_query("update problems set hackable = $hackable where id = ${problem['id']}");
DB::query("update problems set hackable = $hackable where id = ${problem['id']}");
};
$hackable_form->submit_button_config['class_str'] = 'btn btn-warning btn-block';
$hackable_form->submit_button_config['text'] = $problem['hackable'] ? '禁止使用hack' : '允许使用hack';
@ -598,7 +598,7 @@ EOD
$config['view_all_details_type'] = $_POST['view_all_details_type'];
$config['view_details_type'] = $_POST['view_details_type'];
$esc_config = DB::escape(json_encode($config));
mysql_query("update problems set extra_config = '$esc_config' where id = '{$problem['id']}'");
DB::query("update problems set extra_config = '$esc_config' where id = '{$problem['id']}'");
};
$view_type_form->submit_button_config['class_str'] = 'btn btn-warning btn-block top-buffer-sm';

View File

@ -19,9 +19,9 @@
function($type, $username) {
global $problem;
if ($type == '+') {
mysql_query("insert into problems_permissions (problem_id, username) values (${problem['id']}, '$username')");
DB::query("insert into problems_permissions (problem_id, username) values (${problem['id']}, '$username')");
} else if ($type == '-') {
mysql_query("delete from problems_permissions where problem_id = ${problem['id']} and username = '$username'");
DB::query("delete from problems_permissions where problem_id = ${problem['id']} and username = '$username'");
}
},
function() {
@ -51,8 +51,8 @@
<tbody>
<?php
$row_id = 0;
$result = mysql_query("select username from problems_permissions where problem_id = ${problem['id']}");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$result = DB::query("select username from problems_permissions where problem_id = ${problem['id']}");
while ($row = DB::fetch($result, MYSQLI_ASSOC)) {
$row_id++;
echo '<tr>', '<td>', $row_id, '</td>', '<td>', getUserLink($row['username']), '</td>', '</tr>';
}

View File

@ -6,9 +6,9 @@
if (isSuperUser($myUser)) {
$new_problem_form = new UOJForm('new_problem');
$new_problem_form->handle = function() {
mysql_query("insert into problems (title, is_hidden, submission_requirement) values ('New Problem', 1, '{}')");
$id = mysql_insert_id();
mysql_query("insert into problems_contents (id, statement, statement_md) values ($id, '', '')");
DB::query("insert into problems (title, is_hidden, submission_requirement) values ('New Problem', 1, '{}')");
$id = DB::insert_id();
DB::query("insert into problems_contents (id, statement, statement_md) values ($id, '', '')");
svnNewProblem($id);
};
$new_problem_form->submit_button_config['align'] = 'right';

View File

@ -21,7 +21,7 @@
$is_res_empty = true;
$has_score_0 = false;
$has_score_100 = false;
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
while ($row = DB::fetch($result, MYSQLI_NUM)) {
if ($row[0] == 0) {
$has_score_0 = true;
} else if ($row[0] == 100) {

View File

@ -35,9 +35,9 @@
$svn_pw = uojRandString(10);
if (!DB::selectCount("SELECT COUNT(*) FROM user_info"))
mysql_query("insert into user_info (username, email, password, svn_password, register_time, usergroup) values ('$username', '$esc_email', '$password', '$svn_pw', now(), 'S')");
DB::query("insert into user_info (username, email, password, svn_password, register_time, usergroup) values ('$username', '$esc_email', '$password', '$svn_pw', now(), 'S')");
else
mysql_query("insert into user_info (username, email, password, svn_password, register_time) values ('$username', '$esc_email', '$password', '$svn_pw', now())");
DB::query("insert into user_info (username, email, password, svn_password, register_time) values ('$username', '$esc_email', '$password', '$svn_pw', now())");
return "欢迎你!" . $username . ",你已成功注册。";
}

View File

@ -19,7 +19,7 @@
$conds[] = "score <= $q_max_score";
}
if ($q_language != null) {
$conds[] = sprintf("language = '%s'", mysql_real_escape_string($q_language));
$conds[] = sprintf("language = '%s'", DB::escape($q_language));
}
$html_esc_q_language = htmlspecialchars($q_language);

View File

@ -74,7 +74,7 @@
$blog_link_contests->handle = function() {
$blog_id = $_POST['blog_id'];
$contest_id = $_POST['contest_id'];
$str = mysql_fetch_array(mysql_query("select * from contests where id='${contest_id}'"));
$str = DB::selectFirst(("select * from contests where id='${contest_id}'"));
$all_config = json_decode($str['extra_config'], true);
$config = $all_config['links'];
@ -97,8 +97,8 @@
$all_config['links'] = $config;
$str = json_encode($all_config);
$str = mysql_real_escape_string($str);
mysql_query("update contests set extra_config='${str}' where id='${contest_id}'");
$str = DB::escape($str);
DB::query("update contests set extra_config='${str}' where id='${contest_id}'");
};
$blog_link_contests->runAtServer();

View File

@ -107,10 +107,10 @@
var rating_data = [[
<?php
$user_rating_min = $user_rating_max = 1500;
$result = mysql_query("select contest_id, rank, user_rating from contests_registrants where username = '{$user['username']}' and has_participated = 1 order by contest_id");
$result = DB::query("select contest_id, rank, user_rating from contests_registrants where username = '{$user['username']}' and has_participated = 1 order by contest_id");
$is_first_row = true;
$last_rating = 1500;
while ($row = mysql_fetch_array($result)) {
while ($row = DB::fetch($result)) {
$contest = queryContest($row['contest_id']);
$rating_delta = $row['user_rating'] - $last_rating;
if (!$is_first_row) {

View File

@ -15,23 +15,23 @@
return 'fail';
}
$receiver = $_POST['receiver'];
$esc_message = mysql_real_escape_string($_POST['message']);
$esc_message = DB::escape($_POST['message']);
$sender = $myUser['username'];
if (!validateUsername($receiver) || !queryUser($receiver)) {
return 'fail';
}
mysql_query("insert into user_msg (sender, receiver, message, send_time) values ('$sender', '$receiver', '$esc_message', now())");
DB::query("insert into user_msg (sender, receiver, message, send_time) values ('$sender', '$receiver', '$esc_message', now())");
return "ok";
}
function getConversations() {
global $myUser;
$username = $myUser['username'];
$result = mysql_query( "select * from user_msg where sender = '$username' or receiver = '$username' order by send_time DESC" );
$result = DB::query( "select * from user_msg where sender = '$username' or receiver = '$username' order by send_time DESC" );
$ret = array();
while ($msg = mysql_fetch_array($result)) {
while ($msg = DB::fetch($result)) {
if ($msg['sender'] !== $username) {
if (isset($ret[$msg['sender']])) {
$ret[$msg['sender']][1] |= ($msg['read_time'] == null);
@ -63,11 +63,11 @@
$conversationName = $_GET['conversationName'];
$pageNumber = ($_GET['pageNumber'] - 1) * 10;
mysql_query("update user_msg set read_time = now() where sender = '$conversationName' and receiver = '$username' and read_time is null");
DB::query("update user_msg set read_time = now() where sender = '$conversationName' and receiver = '$username' and read_time is null");
$result = mysql_query("select * from user_msg where (sender = '$username' and receiver = '$conversationName') or (sender = '$conversationName' and receiver = '$username') order by send_time DESC limit $pageNumber, 11");
$result = DB::query("select * from user_msg where (sender = '$username' and receiver = '$conversationName') or (sender = '$conversationName' and receiver = '$username') order by send_time DESC limit $pageNumber, 11");
$ret = array();
while ($msg = mysql_fetch_array($result)) {
while ($msg = DB::fetch($result)) {
$ret[] = array($msg['message'], $msg['send_time'], $msg['read_time'], $msg['id'], ($msg['sender'] == $username));
}
return json_encode($ret);
@ -81,13 +81,13 @@ select * from user_msg
where id = $msgId
and read_time is null
EOD;
$result = mysql_query($str);
if (mysql_fetch_array($result)) {
$result = DB::query($str);
if (DB::fetch($result)) {
$str = <<<EOD
delete from user_msg
where id = $msgId
EOD;
mysql_query($str);
DB::query($str);
return 1;
}
return 0;

View File

@ -159,13 +159,13 @@
}
function rejudgeProblem($problem) {
mysql_query("update submissions set judge_time = NULL , result = '' , score = NULL , status = 'Waiting Rejudge' where problem_id = ${problem['id']}");
DB::query("update submissions set judge_time = NULL , result = '' , score = NULL , status = 'Waiting Rejudge' where problem_id = ${problem['id']}");
}
function rejudgeProblemAC($problem) {
mysql_query("update submissions set judge_time = NULL , result = '' , score = NULL , status = 'Waiting Rejudge' where problem_id = ${problem['id']} and score = 100");
DB::query("update submissions set judge_time = NULL , result = '' , score = NULL , status = 'Waiting Rejudge' where problem_id = ${problem['id']} and score = 100");
}
function rejudgeSubmission($submission) {
mysql_query("update submissions set judge_time = NULL , result = '' , score = NULL , status = 'Waiting Rejudge' where id = ${submission['id']}");
DB::query("update submissions set judge_time = NULL , result = '' , score = NULL , status = 'Waiting Rejudge' where id = ${submission['id']}");
}
function updateBestACSubmissions($username, $problem_id) {
$best = DB::selectFirst("select id, used_time, used_memory, tot_size from submissions where submitter = '$username' and problem_id = $problem_id and score = 100 order by used_time, used_memory, tot_size asc limit 1");

View File

@ -30,29 +30,29 @@ function hasContestPermission($user, $contest) {
}
function hasRegistered($user, $contest) {
return mysql_fetch_array(mysql_query("select * from contests_registrants where username = '${user['username']}' and contest_id = ${contest['id']}")) != null;
return DB::selectFirst("select * from contests_registrants where username = '${user['username']}' and contest_id = ${contest['id']}") != null;
}
function hasAC($user, $problem) {
return mysql_fetch_array(mysql_query("select * from best_ac_submissions where submitter = '${user['username']}' and problem_id = ${problem['id']}")) != null;
return DB::selectFirst("select * from best_ac_submissions where submitter = '${user['username']}' and problem_id = ${problem['id']}") != null;
}
function queryUser($username) {
if (!validateUsername($username)) {
return null;
}
return DB::selectFirst("select * from user_info where username='$username'", MYSQL_ASSOC);
return DB::selectFirst("select * from user_info where username='$username'", MYSQLI_ASSOC);
}
function queryProblemContent($id) {
return mysql_fetch_array(mysql_query("select * from problems_contents where id = $id"), MYSQL_ASSOC);
return DB::selectFirst("select * from problems_contents where id = $id", MYSQLI_ASSOC);
}
function queryProblemBrief($id) {
return mysql_fetch_array(mysql_query("select * from problems where id = $id"), MYSQL_ASSOC);
return DB::selectFirst("select * from problems where id = $id", MYSQLI_ASSOC);
}
function queryProblemTags($id) {
$tags = array();
$result = mysql_query("select tag from problems_tags where problem_id = $id order by id");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$result = DB::query("select tag from problems_tags where problem_id = $id order by id");
while ($row = DB::fetch($result, MYSQLI_NUM)) {
$tags[] = $row[0];
}
return $tags;
@ -64,24 +64,24 @@ function queryContestProblemRank($contest, $problem) {
return DB::selectCount("select count(*) from contests_problems where contest_id = {$contest['id']} and problem_id <= {$problem['id']}");
}
function querySubmission($id) {
return mysql_fetch_array(mysql_query("select * from submissions where id = $id"), MYSQL_ASSOC);
return DB::selectFirst("select * from submissions where id = $id", MYSQLI_ASSOC);
}
function queryHack($id) {
return mysql_fetch_array(mysql_query("select * from hacks where id = $id"), MYSQL_ASSOC);
return DB::selectFirst("select * from hacks where id = $id", MYSQLI_ASSOC);
}
function queryContest($id) {
return mysql_fetch_array(mysql_query("select * from contests where id = $id"), MYSQL_ASSOC);
return DB::selectFirst("select * from contests where id = $id", MYSQLI_ASSOC);
}
function queryContestProblem($id) {
return mysql_fetch_array(mysql_query("select * from contest_problems where contest_id = $id"), MYSQL_ASSOC);
return DB::selectFirst("select * from contest_problems where contest_id = $id", MYSQLI_ASSOC);
}
function queryZanVal($id, $type, $user) {
if ($user == null) {
return 0;
}
$esc_type = mysql_real_escape_string($type);
$row = mysql_fetch_array(mysql_query("select val from click_zans where username='{$user['username']}' and type='$esc_type' and target_id='$id'"));
$esc_type = DB::escape($type);
$row = DB::selectFirst("select val from click_zans where username='{$user['username']}' and type='$esc_type' and target_id='$id'");
if ($row == null) {
return 0;
}
@ -89,18 +89,18 @@ function queryZanVal($id, $type, $user) {
}
function queryBlog($id) {
return mysql_fetch_array(mysql_query("select * from blogs where id='$id'"), MYSQL_ASSOC);
return DB::selectFirst("select * from blogs where id='$id'", MYSQLI_ASSOC);
}
function queryBlogTags($id) {
$tags = array();
$result = DB::select("select tag from blogs_tags where blog_id = $id order by id");
while ($row = DB::fetch($result, MYSQL_NUM)) {
while ($row = DB::fetch($result, MYSQLI_NUM)) {
$tags[] = $row[0];
}
return $tags;
}
function queryBlogComment($id) {
return mysql_fetch_array(mysql_query("select * from blogs_comments where id='$id'"), MYSQL_ASSOC);
return DB::selectFirst("select * from blogs_comments where id='$id'", MYSQLI_ASSOC);
}
function isProblemVisibleToUser($problem, $user) {
@ -175,4 +175,4 @@ function deleteBlog($id) {
DB::delete("delete from blogs_comments where blog_id = $id");
DB::delete("delete from important_blogs where blog_id = $id");
DB::delete("delete from blogs_tags where blog_id = $id");
}
}

View File

@ -9,10 +9,10 @@
exec("cd /var/uoj_data; rm $id.zip; zip $id.zip $id -r -q");
}
function svnRefreshPasswordOfProblem($id) {
$result = mysql_query("select user_info.username, svn_password from problems_permissions, user_info where problem_id = $id and user_info.username = problems_permissions.username");
$result = DB::query("select user_info.username, svn_password from problems_permissions, user_info where problem_id = $id and user_info.username = problems_permissions.username");
$content = "[users]\n";
$content .= UOJConfig::$data['svn']['our-root']['username']." = ".UOJConfig::$data['svn']['our-root']['password']."\n";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
while ($row = DB::fetch($result, MYSQLI_NUM)) {
$content .= "${row[0]} = ${row[1]}\n";
}
file_put_contents("/var/svn/problem/$id/conf/passwd", $content);

View File

@ -3,63 +3,81 @@
class DB {
public static function init() {
global $uojMySQL;
@$uojMySQL = mysql_connect(UOJConfig::$data['database']['host'] . ':3306', UOJConfig::$data['database']['username'], UOJConfig::$data['database']['password']);
$uojMySQL = new mysqli(UOJConfig::$data['database']['host'] . ':3306', UOJConfig::$data['database']['username'], UOJConfig::$data['database']['password'], UOJConfig::$data['database']['database']);
if($uojMySQL == null){
echo '-----------------------\n';
echo $uojMySQL->connect_errno;
echo '========================\n';
}
if (!$uojMySQL) {
echo 'There is something wrong with database >_<.... ' . mysql_error();
echo 'There is something wrong with database >_<.... ' . mysqli_error($uojMySQL);
die();
}
mysql_select_db(UOJConfig::$data['database']['database']);
}
public static function escape($str) {
return mysql_real_escape_string($str);
global $uojMySQL;
return mysqli_real_escape_string($uojMySQL, $str);
}
public static function fetch($r, $opt = MYSQL_ASSOC) {
return mysql_fetch_array($r, $opt);
public static function fetch($r, $opt = MYSQLI_ASSOC) {
global $uojMySQL;
return mysqli_fetch_array($r, $opt);
}
public static function query($q) {
return mysql_query($q);
global $uojMySQL;
return mysqli_query($uojMySQL, $q);
}
public static function update($q) {
return mysql_query($q);
global $uojMySQL;
return mysqli_query($uojMySQL, $q);
}
public static function insert($q) {
return mysql_query($q);
global $uojMySQL;
return mysqli_query($uojMySQL, $q);
}
public static function insert_id() {
return mysql_insert_id();
global $uojMySQL;
return mysqli_insert_id($uojMySQL);
}
public static function delete($q) {
return mysql_query($q);
global $uojMySQL;
return mysqli_query($uojMySQL, $q);
}
public static function select($q) {
return mysql_query($q);
global $uojMySQL;
return mysqli_query($uojMySQL, $q);
}
public static function selectAll($q, $opt = MYSQL_ASSOC) {
public static function selectAll($q, $opt = MYSQLI_ASSOC) {
global $uojMySQL;
$res = array();
$qr = mysql_query($q);
while ($row = mysql_fetch_array($qr, $opt)) {
$qr = mysqli_query($uojMySQL, $q);
while ($row = mysqli_fetch_array($qr, $opt)) {
$res[] = $row;
}
return $res;
}
public static function selectFirst($q, $opt = MYSQL_ASSOC) {
return mysql_fetch_array(mysql_query($q), $opt);
public static function selectFirst($q, $opt = MYSQLI_ASSOC) {
global $uojMySQL;
return mysqli_fetch_array(mysqli_query($uojMySQL, $q), $opt);
}
public static function selectCount($q) {
list($cnt) = mysql_fetch_array(mysql_query($q), MYSQL_NUM);
global $uojMySQL;
list($cnt) = mysqli_fetch_array(mysqli_query($uojMySQL, $q), MYSQLI_NUM);
return $cnt;
}
public static function checkTableExists($name) {
global $uojMySQL;
return DB::query("select 1 from $name") !== false;
}
public static function num_rows() {
return mysql_num_rows();
global $uojMySQL;
return mysqli_num_rows($uojMySQL);
}
public static function affected_rows() {
return mysql_affected_rows();
global $uojMySQL;
return mysqli_affected_rows($uojMySQL);
}
}
}

View File

@ -23,12 +23,11 @@ $mail->FromName = 'List manager';
$mail->Host = 'smtp1.example.com;smtp2.example.com';
$mail->Mailer = 'smtp';
@mysqli_connect('localhost','root','password');
@mysqli_select_db("my_company");
@$mm = new mysqli('localhost','root','password', 'my_company');
$query = "SELECT full_name, email, photo FROM employee";
$result = @mysqli_query($query);
$result = @mysqli_query($mm, $query);
while ($row = mysqli_fetch_assoc($result))
while ($row = mysqli_fetch_assoc($mm, $result))
{
// HTML body
$body = "Hello &lt;font size=\"4\"&gt;" . $row['full_name'] . "&lt;/font&gt;, &lt;p&gt;";
@ -126,4 +125,4 @@ if(!$mail->send())
echo 'Message was sent successfully';
</pre>
</body>
</html>
</html>