用PHP编写的语句

时间:2015-02-23 23:40:21

标签: php mysql pdo mysqli

我试图准备/绑定此语句,但它失败并且没有产生错误,所以我在努力如何继续。

prepare()调用失败并返回bool(false)。

// establish connection
$conn = new mysqli(###, ###, ###, ###);

// here are the record values
$dt = date('Y-m-d H:i:s', time());
$record_vals = array(
    'ticket_id'             => 'val',
    'requester_name'        => 'val',
    'domain'                => 'val',
    'created_at'            => $dt,
    'updated_at'            => $dt
);

// insert or update the record
try
{
    $stmt = $conn->prepare("
    INSERT INTO tickets 
        (ticket_id,requester_name,domain,created_at,updated_at)
    VALUES
        (:ticket_id,:requester_name,:domain,:created_at,:updated_at)
    ON DUPLICATE KEY UPDATE
        requester_name=:requester_name,domain=:domain,updated_at=:updated_at
    ");
    foreach ($record_vals as $key => $value)
    {
        $stmt->bindParam(':'.$key, $value);
    }
    $stmt->execute();
}
catch(PDOException $e)
{
    echo "Error: " . $e->getMessage();die();
}

1 个答案:

答案 0 :(得分:2)

您似乎混合了两种不同的PHP函数。 mysqli& PDO

编辑:这是一种方法。

PDO

    $host = "host";
    $username = "username";
    $password = "password";
    $database = "db";

    try {
        // Connection setup
        $pdo = new PDO("mysql:host=$host;dbname=$database", $username, $password);
        $query = $pdo->prepare($sql_query); //Replace $sql_query with SQL statement
        $query->execute();

        while($rows = $query->fetch()) {
           $array = $rows; //Store info in an array
        }

    } catch (PDOException $PDOException) {
        // Get connection errors and exit the script
        print($PDOException->getMessage());
        exit();
    }

有关详细信息,我会说去阅读manual