使用 PHP PDO 插入表 - 挂起在 execute() 语句上


Using PHP PDO to INSERT INTO table - hangs on execute() statement

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

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

a.)在我要么将参数作为参数数组传递给 execute() 函数,要么在尝试execute()之前使用 bindValue 将它们单独绑定到foreach循环中后,它会一直到$stmt->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 语句的输出,完全按照它看起来的顺序 - 我将解释我在做什么(我隐藏了这些值,但它们看起来都是我想要的......我主要想知道格式问题...如果我格式化错误的内容破坏了我对mysql POST请求:

/*** 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"}

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

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

它会打印出一些错误吗?

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

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响应代表"继续" - 正如您在上面的数字定义中看到的那样。我认为这实际上是非常相关的...但直到现在我都不知道它会是 - 我只是在我的本地主机上进行测试。很好奇为什么它很重要,但它解决了我的问题。