Skip to content

Commit d259610

Browse files
committed
Properly fixed possible MySQL Errors, will test in prod for 1 day
Had to rewrite all the mysqli->error/errno to mysqli->lastused because of the read/write splitting, shoud be working now Reverted notifications code back to previous version Added the lastused property to mysqlms, so that when calling the error/errno we know what was the lsat connection used, same for insert_id
1 parent 12cee6e commit d259610

File tree

5 files changed

+19
-19
lines changed

5 files changed

+19
-19
lines changed

include/classes/base.class.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,9 @@ protected function sqlError($error_code='E0020') {
253253
$this->setErrorMessage(call_user_func_array(array($this, 'getErrorMsg'), func_get_args()));
254254
}
255255
// Default to SQL error for debug and cron errors
256-
$this->debug->append($this->getErrorMsg('E0019', $this->mysqli->error));
257-
$this->setCronMessage($this->getErrorMsg('E0019', $this->mysqli->error));
256+
$this->debug->append($this->getErrorMsg('E0019', $this->mysqli->lastused->errno));
257+
$this->setCronMessage($this->getErrorMsg('E0019', $this->mysqli->lastused->errno));
258+
258259
return false;
259260
}
260261

include/classes/mysqlims.class.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ class mysqlims extends mysqli
77
private $mysqliW;
88
private $mysqliR = null;
99
private $slave = false;
10-
10+
public $lastused = null;
11+
1112
/*
1213
* Pass main and slave connection arrays to the constructor, and strict as true/false
1314
*
@@ -61,8 +62,10 @@ public function __construct($main, $slave = false, $strict = false)
6162
public function prepare($query)
6263
{
6364
if (stripos($query, "SELECT") && stripos($query, "FOR UPDATE") === false && $this->slave !== false) {
65+
$this->lastused = $this->mysqliR;
6466
return $this->mysqliR->prepare($query);
6567
} else {
68+
$this->lastused = $this->mysqliW;
6669
return $this->mysqliW->prepare($query);
6770
}
6871
}
@@ -78,8 +81,10 @@ public function prepare($query)
7881
public function query($query, $resultmode = MYSQLI_STORE_RESULT)
7982
{
8083
if (stripos($query, "SELECT") && stripos($query, "FOR UPDATE") === false && $this->slave !== false) {/* Use readonly server */
84+
$this->lastused = $this->mysqliR;
8185
return $this->mysqliR->query($query, $resultmode);
8286
} else {
87+
$this->lastused = $this->mysqliW;
8388
return $this->mysqliW->query($query, $resultmode);
8489
}
8590
}

include/classes/notification.class.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,11 @@ public function isNotified($aData) {
2222
$this->debug->append("STA " . __METHOD__, 4);
2323
$data = json_encode($aData);
2424
$stmt = $this->mysqli->prepare("SELECT id FROM $this->table WHERE data = ? AND active = 1 LIMIT 1");
25-
if ($stmt && $stmt->bind_param('s', $data) && $stmt->execute() && $stmt->store_result() && $stmt->num_rows == 1)
26-
{
25+
if ($stmt && $stmt->bind_param('s', $data) && $stmt->execute() && $stmt->store_result() && $stmt->num_rows == 1) {
2726
return true;
2827
}
29-
30-
if( $stmt->errno )
31-
{
32-
return $this->sqlError();
33-
}
3428

35-
return false;
29+
return $this->sqlError('E0041');
3630
}
3731

3832
/**

include/classes/template.class.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function getActiveTemplates() {
7979
}
8080

8181
$this->setErrorMessage('Failed to get active templates');
82-
$this->debug->append('Template::getActiveTemplates failed: ' . $this->mysqli->error);
82+
$this->debug->append('Template::getActiveTemplates failed: ' . $this->mysqli->lastused->error);
8383
return false;
8484
}
8585

@@ -172,7 +172,7 @@ public function getEntry($template, $columns = "*") {
172172
return $result->fetch_assoc();
173173

174174
$this->setErrorMessage('Failed to get the template');
175-
$this->debug->append('Template::getEntry failed: ' . $this->mysqli->error);
175+
$this->debug->append('Template::getEntry failed: ' . $this->mysqli->lastused->error);
176176
return false;
177177
}
178178

@@ -206,7 +206,7 @@ public function updateEntry($template, $content, $active=0) {
206206
return true;
207207

208208
$this->setErrorMessage('Database error');
209-
$this->debug->append('Template::updateEntry failed: ' . $this->mysqli->error);
209+
$this->debug->append('Template::updateEntry failed: ' . $this->mysqli->lastused->error);
210210
return false;
211211
}
212212
}

include/classes/user.class.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ public function updateAccount($userID, $address, $threshold, $donate, $email, $t
575575
}
576576
// Catchall
577577
$this->setErrorMessage('Failed to update your account');
578-
$this->debug->append('Account update failed: ' . $this->mysqli->error);
578+
$this->debug->append('Account update failed: ' . $this->mysqli->lastused->error);
579579
return false;
580580
}
581581

@@ -832,7 +832,7 @@ public function register($username, $coinaddress, $password1, $password2, $pin,
832832
$signup_time = time();
833833

834834
if ($this->checkStmt($stmt) && $stmt->bind_param('sssissi', $username_clean, $password_hash, $email1, $signup_time, $pin_hash, $apikey_hash, $is_locked) && $stmt->execute()) {
835-
$new_account_id = $this->mysqli->insert_id;
835+
$new_account_id = $this->mysqli->lastused->insert_id;
836836
if (!is_null($coinaddress)) $this->coin_address->add($new_account_id, $coinaddress);
837837
if (! $this->setting->getValue('accounts_confirm_email_disabled') && $is_admin != 1) {
838838
if ($token = $this->token->createToken('confirm_email', $stmt->insert_id)) {
@@ -855,8 +855,8 @@ public function register($username, $coinaddress, $password1, $password2, $pin,
855855
}
856856
} else {
857857
$this->setErrorMessage( 'Unable to register' );
858-
$this->debug->append('Failed to insert user into DB: ' . $this->mysqli->error);
859-
echo $this->mysqli->error;
858+
$this->debug->append('Failed to insert user into DB: ' . $this->mysqli->lastused->error);
859+
echo $this->mysqli->lastused->error;
860860
if ($stmt->sqlstate == '23000') $this->setErrorMessage( 'Username or email already registered' );
861861
return false;
862862
}
@@ -895,7 +895,7 @@ public function resetPassword($token, $new1, $new2) {
895895
} else {
896896
$this->setErrorMessage('Invalid token: ' . $this->token->getError());
897897
}
898-
$this->debug->append('Failed to update password:' . $this->mysqli->error);
898+
$this->debug->append('Failed to update password:' . $this->mysqli->lastused->error);
899899
return false;
900900
}
901901

0 commit comments

Comments
 (0)