Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySQLi编写的语句正在彻底破坏我的数据。这是预期的行为吗?_Php_Mysql_Mysqli - Fatal编程技术网

Php MySQLi编写的语句正在彻底破坏我的数据。这是预期的行为吗?

Php MySQLi编写的语句正在彻底破坏我的数据。这是预期的行为吗?,php,mysql,mysqli,Php,Mysql,Mysqli,我的代码当前使用mysqli::query并检查mysqli::$warning\u count。尝试将文本插入整数列时,将插入0,并生成警告。然而,我刚刚开始学习准备好的语句,对于同一个查询,不会生成任何警告 以下是我的代码摘录: $db_err_msg = 'Database Error: Failed to update profile'; $sql = "UPDATE tblProfiles SET intBookId = ? WHERE lngProfileId = ?"; $stmt

我的代码当前使用
mysqli::query
并检查
mysqli::$warning\u count
。尝试将文本插入整数列时,将插入
0
,并生成警告。然而,我刚刚开始学习准备好的语句,对于同一个查询,不会生成任何警告

以下是我的代码摘录:

$db_err_msg = 'Database Error: Failed to update profile';
$sql = "UPDATE tblProfiles SET intBookId = ? WHERE lngProfileId = ?";
$stmt = $mysqli->prepare($sql) or output_error($db_err_msg);

$book = 'CA';
$id = 10773;
$stmt->bind_param('ii', $book, $id) or output_error($db_err_msg);
$stmt->execute() or output_error($db_err_msg);
echo '$stmt->affected_rows is ', $stmt->affected_rows, "\n";

if ($mysqli->warning_count) {
    $warnings = $stmt->get_warnings();
    do {
        trigger_error('Database Warning (' . $warnings->errno . '): '
            . $warnings->message, E_USER_WARNING);
    } while ( $warnings->next() );
}
else {
    echo 'no warnings', "\n";
}
$stmt->bind_param('ii', $book, $id) or output_error($db_err_msg);
$stmt->bind_param('si', $book, $id) or output_error($db_err_msg);
产生以下输出:

$db_err_msg = 'Database Error: Failed to update profile';
$sql = "UPDATE tblProfiles SET intBookId = ? WHERE lngProfileId = ?";
$stmt = $mysqli->prepare($sql) or output_error($db_err_msg);

$book = 'CA';
$id = 10773;
$stmt->bind_param('ii', $book, $id) or output_error($db_err_msg);
$stmt->execute() or output_error($db_err_msg);
echo '$stmt->affected_rows is ', $stmt->affected_rows, "\n";

if ($mysqli->warning_count) {
    $warnings = $stmt->get_warnings();
    do {
        trigger_error('Database Warning (' . $warnings->errno . '): '
            . $warnings->message, E_USER_WARNING);
    } while ( $warnings->next() );
}
else {
    echo 'no warnings', "\n";
}
$stmt->bind_param('ii', $book, $id) or output_error($db_err_msg);
$stmt->bind_param('si', $book, $id) or output_error($db_err_msg);
$stmt->受影响的_行为1
没有警告

请注意,
intBookId
列具有
TINYINT
数据类型。当使用
mysqli::query
时,相同的查询会生成警告,但在使用准备好的语句时不会

启用严格模式没有帮助。当使用
mysqli::query
时,它会将警告变为错误,但当使用准备好的语句时,列会以
0
的形式进行静默更新

作为记录,我的应用程序在达到这一点之前已经进行了广泛的验证。但我想通过这个额外的验证来抓住任何我可能错过的东西

注意:切换到PDO超出了当前项目的范围


为什么MySQLi编写的语句会出现这种情况?这是预期的行为吗?

虽然它可能不会立即显现出来,但它是预期的行为

因为这行代码:

$db_err_msg = 'Database Error: Failed to update profile';
$sql = "UPDATE tblProfiles SET intBookId = ? WHERE lngProfileId = ?";
$stmt = $mysqli->prepare($sql) or output_error($db_err_msg);

$book = 'CA';
$id = 10773;
$stmt->bind_param('ii', $book, $id) or output_error($db_err_msg);
$stmt->execute() or output_error($db_err_msg);
echo '$stmt->affected_rows is ', $stmt->affected_rows, "\n";

if ($mysqli->warning_count) {
    $warnings = $stmt->get_warnings();
    do {
        trigger_error('Database Warning (' . $warnings->errno . '): '
            . $warnings->message, E_USER_WARNING);
    } while ( $warnings->next() );
}
else {
    echo 'no warnings', "\n";
}
$stmt->bind_param('ii', $book, $id) or output_error($db_err_msg);
$stmt->bind_param('si', $book, $id) or output_error($db_err_msg);
PHP在将值发送到MySQL之前将其强制转换为整数。MySQL接收值
0
,该值是该列的有效值,不会生成任何警告

如果不希望PHP执行此操作,则需要将其作为字符串发送。喜欢这样:

$db_err_msg = 'Database Error: Failed to update profile';
$sql = "UPDATE tblProfiles SET intBookId = ? WHERE lngProfileId = ?";
$stmt = $mysqli->prepare($sql) or output_error($db_err_msg);

$book = 'CA';
$id = 10773;
$stmt->bind_param('ii', $book, $id) or output_error($db_err_msg);
$stmt->execute() or output_error($db_err_msg);
echo '$stmt->affected_rows is ', $stmt->affected_rows, "\n";

if ($mysqli->warning_count) {
    $warnings = $stmt->get_warnings();
    do {
        trigger_error('Database Warning (' . $warnings->errno . '): '
            . $warnings->message, E_USER_WARNING);
    } while ( $warnings->next() );
}
else {
    echo 'no warnings', "\n";
}
$stmt->bind_param('ii', $book, $id) or output_error($db_err_msg);
$stmt->bind_param('si', $book, $id) or output_error($db_err_msg);
MySQL将接收字符串“CA”,并生成一条警告,指出它是一个不正确的整数值

注意:通过将所有内容作为字符串发送,MySQL将不得不进行更多的处理以将字符串转换为整数,但在使用mysqli::query将整个查询作为字符串发送时,MySQL已经这样做了

注意:使用大于
PHP\u INT\u MAX
的整数时会出现相关问题。如果您认为整数值将超过该最大值(这取决于平台,在32位平台上仅为2147483647),并且精度很重要,则将其作为字符串发送更安全