【问题标题】:Using PHP PDO to INSERT INTO table - hangs on execute() statement使用 PHP PDO 插入表 - 在 execute() 语句上挂起
【发布时间】:2015-02-22 22:56:49
【问题描述】:

我已经为此工作了大约两天了。我正在尝试为我正在测试的 iOS 移动应用程序设置 PHP 后端,并且我已经获得了与 mysqli 一起工作的基本版本,但我想改用 PDO

我正在尝试存储从应用程序发送到测试服务器(在我的计算机上)的用户信息。到目前为止,我已经能够用PDO 找出UPDATESELECT - 但是当我到达我的应用程序发出POST 请求以将信息存储在mysql 数据库中的步骤时,会发生什么如下:

a.) 在我传递参数以绑定到 execute() 函数作为参数数组,或者我使用 bindValue 将它们全部单独绑定到一个foreach 在我尝试 execute() 之前循环。如果我的代码到达它没有找到新用户并尝试输入信息的部分,会发生这种情况:

    $this->strforvalsie = substr($this->strforvalsie, 0, -1);
    $this->stforquee = substr($this->stforquee, 0, -1);
    $this->querystring = substr($this->querystring, 0, -1);

    $this->hh = $this->makeathing($this->stforquee, $this->querystring);

    echo "\n\n".$this->hh."\n\n";

    $stmt = $this->db->prepare($this->hh);


    foreach ($this->arrayfordat as $key => $value) {
        $stmt->bindValue( (string)$key, (string)$value, PDO::PARAM_STR);
    }

    echo "\n\n\n\n\n".json_encode($streetch)."\n\n\n\n\n\n";

    //return 2 - /*** I use this line to exit the code early...before the hang - to see what the variables look like. I will print the outputs below to help. ***/

    if($stmt->execute($streetch)) { /*** IT SEEMS TO HANG HERE AND NEVER MAKE IT TO THE RETURN STATEMENT WHICH IS FOR THE PURPOSES OF EXITING THE INSTANCE OF THE CLASS TO SEND A RESPONSE ***/
        return 3;
    }
    else {
        return 2;
    }

这里是 echo 语句的输出,完全按照它出现的顺序 - 我将解释我在做什么(我隐藏了这些值,但它们看起来都是我想要的......我主要是想知道格式问题...如果我的格式有误,会破坏我对mysqlPOST 请求:

/*** THIS LINE IS RESPONSIBLE FOR CREATING MY QUERY: $this->hh = $this->makeathing($this->stforquee, $this->querystring); Output below: ***/

INSERT INTO users(username,email,userfirst,userlast,updatedtime,usergender,fbverified,timezone,fbprofilelink,fblocale,fbid) VALUES(:username,:email,:userfirst,:userlast,:updatedtime,:usergender,:fbverified,:timezone,:fbprofilelink,:fblocale,:fbid)

/*** THIS IS WHAT A JSON OBJECT LOOKS LIKE OF THE ARRAY THAT I AM PASSING TO THE execute() FUNCTION - created by the line: echo "\n\n\n\n\n".json_encode($streetch)."\n\n\n\n\n\n"; Ouput below: ***/

{":username":"Eamon White",":email":"eamon.white7@gmail.com",":userfirst":"Eamon",":userlast":"White",":updatedtime":"2014-06-15T17:34:06+0000",":usergender":"male",":fbverified":"NO",":timezone":"-5",":fbprofilelink":"https:\/\/www.facebook.com\/app_scoped_user_id\/891510963470\/",":fblocale":"en_US",":fbid":"891510963470"}

所有这一切中最烦人的部分...希望对任何可以提供帮助的人最有说服力...所有信息都以完全可接受的方式成功存储在数据库中...只是程序似乎从未出现过想要移动到return 之后的execute()s 语句,这样我就可以在应用程序端得到处理的响应。任何帮助将不胜感激。总结问题 - 不能通过该死的$stmt->execute() 声明:)。

【问题讨论】:

    标签: php ios mysql json pdo


    【解决方案1】:

    是的...我是一个半新手 - 很有趣...我在我的代码中使用这些方法来响应:

    function getStatusCodeMessage($status)
    {
    // these could be stored in a .ini file and loaded
    // via parse_ini_file()... however, this will suffice
    // for an example
    $codes = Array(
        100 => 'Continue',
        101 => 'Switching Protocols',
        200 => 'OK',
        201 => 'Created',
        202 => 'Accepted',
        203 => 'Non-Authoritative Information',
        204 => 'No Content',
        205 => 'Reset Content',
        206 => 'Partial Content',
        300 => 'Multiple Choices',
        301 => 'Moved Permanently',
        302 => 'Found',
        303 => 'See Other',
        304 => 'Not Modified',
        305 => 'Use Proxy',
        306 => '(Unused)',
        307 => 'Temporary Redirect',
        400 => 'Bad Request',
        401 => 'Unauthorized',
        402 => 'Payment Required',
        403 => 'Forbidden',
        404 => 'Not Found',
        405 => 'Method Not Allowed',
        406 => 'Not Acceptable',
        407 => 'Proxy Authentication Required',
        408 => 'Request Timeout',
        409 => 'Conflict',
        410 => 'Gone',
        411 => 'Length Required',
        412 => 'Precondition Failed',
        413 => 'Request Entity Too Large',
        414 => 'Request-URI Too Long',
        415 => 'Unsupported Media Type',
        416 => 'Requested Range Not Satisfiable',
        417 => 'Expectation Failed',
        500 => 'Internal Server Error',
        501 => 'Not Implemented',
        502 => 'Bad Gateway',
        503 => 'Service Unavailable',
        504 => 'Gateway Timeout',
        505 => 'HTTP Version Not Supported'
    );
    
    return (isset($codes[$status])) ? $codes[$status] : '';
    }
    
    
    // Helper method to send a HTTP response code/message
    function sendResponse($status=null, $body = '', $content_type = 'text/html')
    {
        $status_header = 'HTTP/1.1 ' . $status . ' ' . getStatusCodeMessage($status);
        header($status_header);
        header('Content-type: ' . $content_type);
        print $body;
    }
    

    我使用100 响应只是为了使用不同的东西,这样我就可以从我的应用程序中确定服务器端发生的事情 - 而100 响应代表“继续” - 正如您在上面的数字的定义。我认为这实际上非常相关......但直到现在我才知道它会是 - 我只是在我的本地主机上进行测试。很好奇为什么它很重要,但它解决了我的问题。

    【讨论】:

      【解决方案2】:

      请在您的连接中添加以下属性: $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

      它会打印一些错误吗?

      【讨论】:

      • 我想我明白了 - 看看我的答案,哈哈 - 我已经知道了 - 它只是挂在那里......query不是prepare lol
      • 将来我会将此属性添加到您的连接中。它会打印出错误;)
      • 谢谢 - 如果你还在那里 - 现在我将它用于我的查询语句 INSERT INTO users (username, email, userfirst, userlast, updatedtime, usergender, fbverified, timezone, fbprofilelink, fblocale, fbid) VALUES (:username, :email, :userfirst, :userlast, :updatedtime, :usergender, :fbverified, :timezone, :fbprofilelink, :fblocale, :fbid) 并且我收到以下错误:
      • 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 ':username, :email, :userfirst, :userlast, :updatedtime, :usergender, :fbverified' at line 1
      • 我的回答错了...如果您想帮忙请告诉我...也许您有时间聊天...我是 PDO 的新手
      猜你喜欢
      • 1970-01-01
      • 2013-05-19
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 2013-10-22
      • 2016-09-29
      • 2014-12-14
      • 1970-01-01
      相关资源
      最近更新 更多