简体   繁体   English

INSERT语句不适用于PDO

[英]INSERT statement does not work with PDO

EDIT: PDO appears to have an issue with the French character in the column name Unité . 编辑:PDO列名称Unité的法语字符似乎有问题。 Is there a solution to this, or do I need to rename the column in the database? 有解决方案吗,还是我需要重命名数据库中的列?

SELECT statements work with PDO, but the INSERT statement does not. SELECT语句与PDO一起使用,但INSERT语句不起作用。

Connection string: 连接字符串:

$dbCon = new PDO("mysql:host=".$host.";dbname=".$dbName.";charset=utf8", $username, $password);

Working SELECT statement: 工作的SELECT语句:

$sql = 'SELECT * FROM availability WHERE event_id=:postID';
$stmt = $dbCon->prepare($sql);
$stmt->execute(array(':postID'=>$postID));
$result = $stmt->fetchAll();

Non-functioning INSERT statement: 无法运行的INSERT语句:

$sql = 'INSERT INTO `availability` (`event_id`,`Nom`,`Address`,`Address2`,`Tel1`,`Tel2`,`Tel3`,`Classement`,`Soleil`,`Unité`,`Dispo`,`Ville`,`Type`,`URL`,`Jour1`,`Jour2`,`Jour3`,`Jour4`,`Jour5`,`Jour6`,`Jour7`,`Jour8`,`Jour9`,`Jour10`,`Jour11`,`Jour12`,`Jour13`,`Jour14`,`Jour15`,`visible`) (SELECT :postID,`Nom`,`Address`,`Address2`,`Tel1`,`Tel2`,`Tel3`,`Classement`,`Soleil`,`Unité`,`Dispo`,`Ville`,`Type`,`URL`,`Jour1`,`Jour2`,`Jour3`,`Jour4`,`Jour5`,`Jour6`,`Jour7`,`Jour8`,`Jour9`,`Jour10`,`Jour11`,`Jour12`,`Jour13`,`Jour14`,`Jour15`,`visible` FROM `default_hotels`)';
$stmt = $dbCon->prepare($sql);
$stmt->execute(array(':postID'=>$postID));
$result = $stmt->fetchAll();

Another working SELECT statement: 另一个有效的SELECT语句:

$sql = 'SELECT post_title FROM wp_posts WHERE id=:postID';
$stmt = $dbCon->prepare($sql);
$stmt->execute(array(':postID'=>$postID));
$records = $stmt->fetchAll();

All of the statements appear in this order in my script. 所有语句均按此顺序出现在我的脚本中。 The variables are re-used and not cleared. 变量将被重新使用且不会被清除。

The SQL INSERT statement that I provided works when submitted through phpmyadmin but not PDO. 通过phpmyadmin而不是PDO提交时,我提供的SQL INSERT语句有效。

The PDO charset parameter in the connection string started being supported in PHP 5.3.6. PHP 5.3.6开始支持连接字符串中的PDO charset参数。 Before that it did nothing. 在此之前,它什么也没做。 So the problem is that the connection to your database is not actually run in utf8, but likely latin1. 因此,问题在于与数据库的连接实际上不是在utf8中运行,而是可能在latin1中运行。 Hence the encoding of the column name in PHP and in MySQL doesn't match, hence MySQL misunderstands the column name, hence can't find the column you ask for. 因此,PHP和MySQL中列名的编码不匹配,因此MySQL会误解列名,因此找不到您要的列。

See https://stackoverflow.com/a/279279/476 for alternative ways to specify the connection encoding, upgrade your version of PHP, or use ASCII-only for column names (that's a good idea anyway, specifically because it makes compatibility issues like this much less of a problem). 有关指定连接编码,升级PHP版本或仅对列名使用ASCII的替代方法,请参见https://stackoverflow.com/a/279279/476 (无论如何,这是一个好主意,特别是因为这会引起兼容性问题)这样的问题就更少了)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM