php pdo函数连接到DB并进行查询

时间:2013-09-17 20:49:09

标签: php mysql pdo connect execute

我希望尽可能提高代码效率。不幸的是,我似乎无法找到一个很好的方法来做到这一点。我尝试过使用Sammitch的代码看起来更干净但不幸的是它似乎没有用。

我想要一种方法来停止使用准备,执行,每次和功能对我来说都是最有意义的。使用Simmitch的建议,我删除了与数据库的初始连接以停止不必要的开销,但代码仍然不起作用。显示“SQLSTATE [HY093]:参数号无效:参数未定义”错误。

我目前的代码(某些部分省略,因为没有必要):

/*Function to talk to database*/
function doQuery($myDB, $myQuery, $myValues)
{
    try
    {     
        $st = $myDB->prepare($myQuery);       
        $st->execute($myValues);
        //echo $success;
    }
    catch (PDOException $e)
    {
        echo "Failed because: " . $e->getMessage();
    }
}

    $db = new PDO('mysql:host=localhost;dbname='dbanme';charset=utf8', 'dbuser', 'dbpass');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Set error mode
    $query = "INSERT INTO users(login,pass,email,county) VALUES(:username,:password,:email,:count)";
    $values = array('username' => $_POST['username'],
                     'password' => $_POST['password1'],
                     'email' => $_POST['email'],
                     'county' => $_POST['county']
                     );
    doQuery($db, $query, $values);

2 个答案:

答案 0 :(得分:2)

<?php

function doQuery($db, $query, $arguments) {
    try {
        //Prepare and execute an insert into DB         
        $st = $db->prepare($query);       
        $st->execute(array($values));
        echo $success; // 4. use echo
        // you should probably return something here...
    } catch (PDOException $e) {
        // 5. Fail ~descriptively~
        echo "Failed because: " . $e->getMessage();
        // you should probably return something here...
    }
}

// 1. Don't create the database inside of the same function that does the queries,
//    creation/destruction of the objects/connections will cause unnecessary overhead,
$myDb = new PDO('mysql:host=localhost;dbname=dbname;charset=utf8', 'dbuser', 'dbpassword');
$myDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Set error mode

$myQuery = "INSERT INTO users(login,pass,email,county) VALUES(:username,:password,:email,:count)";

// 2. You can't define an array like that.
// 3. You do not need to add colons to the array indexes.
$myValues = array(
    'username' => $_POST['username'],
    'password' => $_POST['password1'],
    'email' => $_POST['email'],
    'county' => $_POST['county']
);

doquery($myDb, $myQuery, $myValues)

答案 1 :(得分:1)

不要忘记在参数数组中包含“:”?

/*Function to talk to database*/
function doQuery($myDB, $myQuery, $myValues)
{
    try
    {     
        $st = $myDB->prepare($myQuery);       
        $st->execute(array($myValues));
        //echo $success;
    }
    catch (PDOException $e)
    {
        echo "Failed because: " . $e->getMessage();
    }
}

    $db = new PDO('mysql:host=localhost;dbname='dbanme';charset=utf8', 'dbuser', 'dbpass');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Set error mode
    $query = "INSERT INTO users(login,pass,email,county) VALUES(:username,:password,:email,:count)";
    $values = array(':username' => $_POST['username'],
                     ':password' => $_POST['password1'],
                     ':email' => $_POST['email'],
                     ':county' => $_POST['county']
                     );
    doQuery($db, $query, $values);

我希望这有效!