mysql连接函数pdo

时间:2012-02-11 00:45:37

标签: php mysql pdo

<?php

require_once "includes/db_data_inc.php";

try 
{
    /* In this way I get a db connection handle */
    $DBH = new PDO("mysql:host=$db_host;port=8889;dbname=$db_name",$db_user,$db_pass);

}
catch (PDOException $pdoe) 
{
    error_log($pdoe->getMessage());
    die("Failed to connect to the database.");
}

$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$DBH->setAttribute(PDO::ATTR_AUTOCOMMIT,false);

try
{
    $success_msg = NULL;
    $unsucess_msg = NULL;

    $query = "INSERT INTO users (name,surname,address,birth_place,province,birthdate,sex,treatment) VALUES (?)";
    $SHT = $DBH->prepare($query);

    $SHT->bindParam(1, $_POST['name']);
    $SHT->bindParam(2, $_POST['surname']);
    $SHT->bindParam(3, $_POST['address']);
    $SHT->bindParam(4, $_POST['birth_place']);
    $SHT->bindParam(5, $_POST['province']);
    $SHT->bindParam(6, $_POST['dt']);
    $SHT->bindParam(7, $_POST['gender']);
    $SHT->bindParam(8, $_POST['select']);

    $DBH->beginTransaction();

    if($SHT->execute())
    {
        $DBH->commit();
        $success_msg = "The emergency call was correctly sent...";
    }
    else
    {
        $unsucess_msg = "It couldn't estabilished a connection to call center...";
    }
}
catch(PDOException $pdoe)
{
    $DBH->rollBack();
}


/* Close the db connection */
$DBH = null;

&GT;

这里是我的mysql连接函数,用于连接mysql db和insert查询。代码不起作用。可能有一些错误?因为我没有从php控制台收到任何错误,但我甚至没有在页面中看到任何结果。任何想法?

我在控制台上工作并获得了此错误:

Invalid parameter number: number of bound variables does not match number of tokens
这是什么意思?

1 个答案:

答案 0 :(得分:0)

一个'?'只是与你稍后传递的八个参数不匹配。

查看PDOStatement::bindParam方法的示例。

使用类似:

$query = "INSERT INTO users (name,surname,address,birth_place,province,birthdate,sex,treatment) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";