我正在尝试使用 PDO 插入 MySQL 表。代码:
public function sendMail($to, $body) {
/* this all works
*
error_reporting(E_ALL);
ini_set('display_errors', '1');
$stmt = $this->database->prepare("SELECT * FROM users");
$stmt->execute();
$fetch = $stmt->fetchAll();
print_r($fetch);
*/
$stmt = $this->database->prepare("INSERT INTO
inbox_unread (date, body, to, from) VALUES (:date, :body, :to, :from)");
echo $stmt->bindParam(":date", date("Y-m-d H:i:s"));
echo $stmt->bindParam(":body", $body);
echo $stmt->bindParam(":to", $to);
echo $stmt->bindParam(":from", $_SESSION['username']);
if ($stmt->execute()) {
echo "yes";
} else {
print_r($this->database->errorInfo());
}
}
这是我得到的输出:
1111Array ( [0] => 00000 [1] => [2] => )
数据库:
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `sandbox`
--
-- --------------------------------------------------------
--
-- Table structure for table `inbox_unread`
--
CREATE TABLE IF NOT EXISTS `inbox_unread` (
`mail_id` int(11) NOT NULL AUTO_INCREMENT,
`date` datetime NOT NULL,
`body` text NOT NULL,
`to` varchar(255) NOT NULL,
`from` varchar(255) NOT NULL,
PRIMARY KEY (`mail_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
'00000' 显然意味着成功,但如果成功则不应打印。没有向数据库中插入任何内容。我确定这是一个错字,但我找不到它。
SQL 语句:
INSERT INTO inbox_unread (date, body, to, from) VALUES ('2013-01-04 23:40:58', 'test', 'will', 'will')