mysqli 插入查询的结果不稳定
Erratic results with mysqli insert query
我 运行 使用 完全 相同参数的相同 mysqli 插入查询。大约有1/3的时间是成功的,我不知道哪里出了问题。
<?php
//...
$decrypted_session_key = 'unavailable'; // initialize
$res = openssl_get_privatekey($priv_key, $passphrase);
$result = openssl_private_decrypt($encrypted_session_key, $decrypted_session_key, $res, OPENSSL_PKCS1_OAEP_PADDING);
if ($decrypted_session_key == 'unavailable') {
mysqli_close($link);
echo json_encode(array('result' => 'failed', 'message' => 'failed to decrypt the session key'));
die();
}
if (!$decrypted_session_key) {
echo json_encode(array('result'=>'failed', 'message'=>'decrypted session key has failed'));
die();
}
$updated_at = date("Y-m-d H:i:s");
// save this record to the database
$result = mysqli_query($link, "INSERT INTO Session_Keys (session_id, session_key, iv, updated_at)
VALUES ($session_id, '$decrypted_session_key', '$iv', '$updated_at')");
if (!$result) {
$param_check = $session_id . " " . base64_encode($iv) . " " . base64_encode($decrypted_session_key) . " " . $updated_at;
echo json_encode(array('result'=>'failed', 'message'=>$param_check));
die();
}
// ...
}
每当失败时,我都会返回最后一个 echo 语句。我怀疑 php 解密例程失败了,但事实并非如此。所有的参数都是完美的,包括解密值。是我的插入语句错误的问题吗?我尝试了引用字段的各种组合,但没有一致的结果。
table结构是这样的:
'session_id' 整数(11)
'session_key' 小斑点
'iv' 小斑点
'updated_at'日期时间
我不明白为什么我的结果如此不一致。如果失败了,为什么不每次都失败呢?如果有效,为什么不是每次都有效?很困惑。任何帮助表示赞赏。谢谢!
您是否尝试提交交易?
/* commit transaction */
if (!$mysqli->commit()) {
print("Transaction commit failed\n");
exit();
}
在评论中向 OP 建议使用 or die(mysqli_error($link))
到 mysqli_query()
让他们找到查询失败的原因。
他们的评论:
"I finally got at the error message here it is: 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 '<´§y?'T]î‘Á gô, EÈöJÀÊ?Xû¶T¢ÑÖ?, 2015-04-05 19:26:00)' at line 2 Must be something to do with the bytes that I am trying to store."
和
"Now I understand the problem. Your suggestion about looking at mysqli_error is what led me to the solution (after lots of difficulty on my end to gain access to it!). The real error (after I correctly quoted my fields) is Duplicate entry '2147483647' for key 'PRIMARY'. My ios app is randomly assigning an unsigned integer value (0 - 4294967294) but an int(11) field maxex out at 2147483647. So, half of my values were received as duplicates, thereby causing an error. Problem solved. Thanks for your help!"
来自:
2147483647 是 mysql 的最大整数值。只需将类型从 int 更改为 bigint。
- 这解释了重复的错误。
我 运行 使用 完全 相同参数的相同 mysqli 插入查询。大约有1/3的时间是成功的,我不知道哪里出了问题。
<?php
//...
$decrypted_session_key = 'unavailable'; // initialize
$res = openssl_get_privatekey($priv_key, $passphrase);
$result = openssl_private_decrypt($encrypted_session_key, $decrypted_session_key, $res, OPENSSL_PKCS1_OAEP_PADDING);
if ($decrypted_session_key == 'unavailable') {
mysqli_close($link);
echo json_encode(array('result' => 'failed', 'message' => 'failed to decrypt the session key'));
die();
}
if (!$decrypted_session_key) {
echo json_encode(array('result'=>'failed', 'message'=>'decrypted session key has failed'));
die();
}
$updated_at = date("Y-m-d H:i:s");
// save this record to the database
$result = mysqli_query($link, "INSERT INTO Session_Keys (session_id, session_key, iv, updated_at)
VALUES ($session_id, '$decrypted_session_key', '$iv', '$updated_at')");
if (!$result) {
$param_check = $session_id . " " . base64_encode($iv) . " " . base64_encode($decrypted_session_key) . " " . $updated_at;
echo json_encode(array('result'=>'failed', 'message'=>$param_check));
die();
}
// ...
}
每当失败时,我都会返回最后一个 echo 语句。我怀疑 php 解密例程失败了,但事实并非如此。所有的参数都是完美的,包括解密值。是我的插入语句错误的问题吗?我尝试了引用字段的各种组合,但没有一致的结果。
table结构是这样的:
'session_id' 整数(11)
'session_key' 小斑点
'iv' 小斑点
'updated_at'日期时间
您是否尝试提交交易?
/* commit transaction */
if (!$mysqli->commit()) {
print("Transaction commit failed\n");
exit();
}
在评论中向 OP 建议使用 or die(mysqli_error($link))
到 mysqli_query()
让他们找到查询失败的原因。
他们的评论:
"I finally got at the error message here it is: 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 '<´§y?'T]î‘Á gô, EÈöJÀÊ?Xû¶T¢ÑÖ?, 2015-04-05 19:26:00)' at line 2 Must be something to do with the bytes that I am trying to store."
和
"Now I understand the problem. Your suggestion about looking at mysqli_error is what led me to the solution (after lots of difficulty on my end to gain access to it!). The real error (after I correctly quoted my fields) is Duplicate entry '2147483647' for key 'PRIMARY'. My ios app is randomly assigning an unsigned integer value (0 - 4294967294) but an int(11) field maxex out at 2147483647. So, half of my values were received as duplicates, thereby causing an error. Problem solved. Thanks for your help!"
来自:
2147483647 是 mysql 的最大整数值。只需将类型从 int 更改为 bigint。
- 这解释了重复的错误。