PHP:PDO prepare()导致脚本暂停

时间:2013-10-29 15:25:52

标签: php mysql pdo

我正在为部分测试站点开发一个简单的数据库助手。我希望能够通过以下方式访问数据库:

require_once 'include/database_system.php'
...
$row = DB_query("SELECT userID FROM users WHERE username = :username",
                   array(':username' => $ourUsername));

所以我写了一个小脚本来做到这一点:

<?php
session_start();

$username = "xxxx";
$password = "xxxx";
$host = "localhost";
$dbname = "xxxx";

$dboptions = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');

try
{
    $db = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    var_dump($db);
}
catch(PDOException $ex)
{
    die("MySQL: Failed to connect to API Testing Database");
}

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

var_dump($db);

// break magic quotes
if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
    function break_magic_quotes(&$array)
    {
        foreach($array as &$value)
        {
            if(is_array($value))
            {
                break_magic_quotes($value);
            }
            else
            {
                $value = stripslashes($value);
            }
        }
    }

    break_magic_quotes($_POST);
    break_magic_quotes($_GET);
    break_magic_quotes($_COOKIE);
}

header('Content-Type: text/html; charset=utf-8');

function DB_query($query, $queryArgs)
{
    global $db;
    echo 'TRYING TO QUERY SERVER ';
    var_dump($query);
    var_dump($queryArgs);
    var_dump($db);
    try
    {
        echo 'PREPARE ';
        $stmt = $db->prepare($query);
        echo 'EXECUTE ';
        $result = $stmt->execute($queryArgs);
    }
    catch(PDOException $ex)
    {
        echo "QUERY FAILED: $query";
        die("Query failed: " . $query);
        return;
    }
    echo 'SERVER QUERY OK';
    return $stmt->fetch();
}

很自然地,我正在为登录页面制作一些表单操作代码,方法是:

require_once 'database.php'
....
$row = DB_query("SELECT * FROM users WHERE username = :username",
                array(':username' => $_POST['u']) );

输出结果不是很确定。它不仅无法通过$ db-&gt; prepare()语句,而且看起来甚至看起来PDO无效。

object(PDO)#1 (0) { } object(PDO)#1 (0) { } TRYING TO LOGINobject(PDO)#1 (0) { } TRYING TO QUERY SERVER string(46) "SELECT * FROM users WHERE username = :username" array(1) { [":username"]=> string(4) "derp" } NULL PREPARE

我不知道为什么会这样做。我检查了PHP设置,看起来PDO已正确打开。我上下检查了一切,但我无法到达任何地方。如果有人有任何见解,那就太棒了。

0 个答案:

没有答案