1

请帮帮我。我插入了 varchar 列

$nul='mystring';
$STH->bindParam(':param',$nul , PDO::PARAM_INT);

如果PDO::PARAM_INT的意思是要进入数据库插入数(int)$nul=0!有一个记录字符串'mystring'。这是为什么?

4

2 回答 2

3

请参阅PHP PDO::bindParam() 数据类型。它是如何工作的?

PDO::PARAM_INT 仅将布尔值转换为整数。没有其他的。

如果要将字符串转换为 int 使用 intval

于 2013-10-08T08:29:16.403 回答
2

假设我正确理解了您的问题,您希望数据库中的值为 null(不确定值),但实际上您得到的是字符串“nul”。

如果您将某些内容设置为 null,则不需要撇号。

// Setting the value of the string
$string_to_insert = 'my_string';

// Checks that the string has a value, if not sets to NULL
$value = isset($string_to_insert) ?  $string_to_insert : null;

// Bind the parameter
$STH->bindParam(':param', $value, PDO::PARAM_INT);

如果您在它周围加上撇号,您将在数据库中收到该文字字符串。

NULL 是一个常量,请查看此处的文档http://www.php.net/manual/en/language.types.null.php

于 2013-10-08T08:13:29.960 回答