再说得详细一点:
程序内容:
$sql = 'INSERT INTO `serviceonline` (`Unit`, `Type`, `UserID`, `Title`, `Content`)
VALUES (:unit, :type, :userID, :title, :content)';
$dataConn->prepareSQL($sql, 'bindParam',
array(':unit', $u),
array(':type', $t),
array(':userID', $userID),
array(':title', $speechTitle, PDO::PARAM_STR),
array(':content', $content, PDO::PARAM_STR));用到的函数代码:
public function prepareSQL($sql, $bindFunctionName = '') {
if (defined('DEBUG') && DEBUG) {
echo SQLConnAlert::COMMANDLINE . $sql;
}
self::$rsObj = parent::prepare($sql);
if (!empty($bindFunctionName)) {
$bindArray = func_get_args(); // 获取全部参数
array_shift($bindArray); // 去掉开始的两个参数
array_shift($bindArray);
// 遍历参数数组, 进行参数绑定
foreach ($bindArray as $key => $value) {
if (is_array($value)) { // 当前参数是数组时的处理
$bindKey = $value[0];
$bindValue = $value[1];
if (isset($value[3])) {
self::$rsObj->$bindFunctionName($bindKey, $bindValue, $value[2], $value[3]);
} elseif (isset($value[2])) {
self::$rsObj->$bindFunctionName($bindKey, $bindValue, $value[2]);
} else {
self::$rsObj->$bindFunctionName($bindKey, $bindValue);
}
} else {// 当前参数不是数组的处理
$bindKey = $key + 1;
$bindValue = $value;
self::$rsObj->$bindFunctionName($bindKey, $bindValue);
}
}
}
try {
$this->beginTransaction();
self::$rsObj->execute();
$this->commit();
} catch (Exception $e) {
$this->rollBack();
}
}
程序内容:
$sql = 'INSERT INTO `serviceonline` (`Unit`, `Type`, `UserID`, `Title`, `Content`)
VALUES (:unit, :type, :userID, :title, :content)';
$dataConn->prepareSQL($sql, 'bindParam',
array(':unit', $u),
array(':type', $t),
array(':userID', $userID),
array(':title', $speechTitle, PDO::PARAM_STR),
array(':content', $content, PDO::PARAM_STR));用到的函数代码:
public function prepareSQL($sql, $bindFunctionName = '') {
if (defined('DEBUG') && DEBUG) {
echo SQLConnAlert::COMMANDLINE . $sql;
}
self::$rsObj = parent::prepare($sql);
if (!empty($bindFunctionName)) {
$bindArray = func_get_args(); // 获取全部参数
array_shift($bindArray); // 去掉开始的两个参数
array_shift($bindArray);
// 遍历参数数组, 进行参数绑定
foreach ($bindArray as $key => $value) {
if (is_array($value)) { // 当前参数是数组时的处理
$bindKey = $value[0];
$bindValue = $value[1];
if (isset($value[3])) {
self::$rsObj->$bindFunctionName($bindKey, $bindValue, $value[2], $value[3]);
} elseif (isset($value[2])) {
self::$rsObj->$bindFunctionName($bindKey, $bindValue, $value[2]);
} else {
self::$rsObj->$bindFunctionName($bindKey, $bindValue);
}
} else {// 当前参数不是数组的处理
$bindKey = $key + 1;
$bindValue = $value;
self::$rsObj->$bindFunctionName($bindKey, $bindValue);
}
}
}
try {
$this->beginTransaction();
self::$rsObj->execute();
$this->commit();
} catch (Exception $e) {
$this->rollBack();
}
}
array(':unit', $u),
array(':type', $t),
array(':userID', $userID),
---------------------------------------------------
array(':title', $speechTitle, PDO::PARAM_STR),
array(':content', $content, PDO::PARAM_STR)); $dataConn->prepareSQL($sql, 'bindParam',
array(':unit', $u),
array(':type', $t),
array(':userID', $userID, PDO::PARAM_INT),
-----------------------------------------------------
array(':title', $speechTitle, PDO::PARAM_STR),
array(':content', $content, PDO::PARAM_STR)); 试试上面的改动!PHP5.2.6里面要正确指定参数类型!默认是PdO::PARAM_STR
$sql = "INSERT INTO `serviceonline` (`Unit`, `Type`, `UserID`, `Title`, `Content`)
VALUES ($u, $t, $userID, '$speechTitle', '$content')";
$dataConn->prepareSQL($sql);
这样使用就没有任何问题,不知是什么原因?
肯请各位达人说明之。
绑定代码:self::$rsObj->bindParam(:unit, 1, 1)
绑定代码:self::$rsObj->bindParam(:type, 4, 1)
绑定代码:self::$rsObj->bindParam(:userID, 1, 1)
绑定代码:self::$rsObj->bindParam(:title, d, 2)
绑定代码:self::$rsObj->bindParam(:content, ee, 2)
批事务处理失败: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'ee' for column 'Unit' at row 1