php - iOS 到 PDO PHP 和 MySQL

标签 php mysql objective-c ios pdo

我使用 POST 将 JSOn 发送到 PHP Web 服务

JSON:

{"mDel":"NULL","mType":"text","mUserId":4,"lLong":"(null)","mPrivacy":1,"lLat":"(null)","mDate":"2012-13-25 13:13:25","mHeight":"NULL","mDescription":"Test","mPhoneUniqueKey":"425062012131325","mMedia":"4_2_20121325011325.png","lName":"Apple Store, San Francisco"}

PHP:

$request = Slim::getInstance()->request();
$moment = json_decode($request->getBody());
$sql = "INSERT INTO mymo_moment (profile_id, description, media, locationName, lat, long, mDate, privacy, type, iPhoneUniqueID, deleted, height) 
        VALUES (:mUserId, :mDescription, :mMedia, :lName, :lLat, :lLong, :mDate, :mPrivacy, :mType, :mPhoneUniqueKey, :mDel, :mHeight)";
try {
    $db = getConnection();
    $stmt = $db->prepare($sql);
    $stmt->bindParam(":mUserId", $moment->mUserId);
    $stmt->bindParam(":mDescription", $moment->mDescription);
    $stmt->bindParam(":mMedia", $moment->mMedia);
    $stmt->bindParam(":lName", $moment->lName);
    $stmt->bindParam(":lLat", $moment->lLat);
    $stmt->bindParam(":lLong", $moment->lLong);
    $stmt->bindParam(":mDate", $moment->mDate);
    $stmt->bindParam(":mType", $moment->mType);
    $stmt->bindParam(":mPrivacy", $moment->mPrivacy);
    $stmt->bindParam(":mPhoneUniqueKey", $moment->mPhoneUniqueKey);
    $stmt->bindParam(":mDel", $moment->mDel);
    $stmt->bindParam(":mHeight", $moment->mHeight);
    $stmt->execute();

    echo($stmt);

    $moment->id = $db->lastInsertId();
    $db = null;
    echo json_encode($moment);
} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}';
}

我收到以下错误,我已经查看了此错误,但我无法弄清楚我做错了什么,任何帮助都会很棒!

错误:

{"error":{"text":SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'long, mDate, privacy, type, iPhoneUniqueID, deleted, height) 
            VALUES ('4',' at line 1}}

最佳答案

longtype - 这些名称可以被视为关键字,具体取决于您使用的 RDBMS。更改列名或转义其名称,即在 MSSQL 中键入 "long" 而不是 long,在 MySQL 中键入 {grave-accent}long{grave-accent} *

您还可以使用以下语法来准备语句并绑定(bind)参数:

$stmt = $db->prepare($sql);
$stmt->execute(array(
    ':mType'   => $moment->mType,
    ...
    ':mHeight' => $moment->mHeight
));

关于php - iOS 到 PDO PHP 和 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11189237/

相关文章:

objective-c - Objective-C : How to implement MapKit Method - (NSSet *)annotationsInMapRect:(MKMapRect)mapRect

php - Select 语句未正确检查 num_rows

php - PHP 和 mySQL 无法正常工作的文件上传 (.pdf),

jquery - 在 jQuery 中将 sql 表的单个元素集成为变量

mysql - 为什么存在外键时为 'foreign key constraint fails'?

objective-c - 我怎样才能在 Objective-C 中的两个类之间有引用?

php - 如何避免 mysql 查询 RPG 游戏上的每个页面加载?有任何想法吗?

php - 忽略 MySQL PDO PHP 命令行中的 1 行不起作用

php - 我如何将 "UserID"与 "username"链接起来,以便我可以更新我的表?

ios - 如何在 ios 中捕获数据库异常?