简体   繁体   English

PHP中的准备好的语句

[英]Prepared statement in PHP

I'm trying to prepare/bind this statement, but it is failing and not producing an error, so I am struggling on how to proceed. 我正在尝试准备/绑定此语句,但是它失败了,并且没有产生错误,因此我在如何继续上苦苦挣扎。

The prepare() call fails with a bool(false) returned. 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();
}

You seem to be mixing two different PHP functions. 您似乎在混用两个不同的PHP函数。 mysqli & PDO . mysqliPDO

EDIT: This is a way to do it. 编辑:这是一种方法。

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();
    }

For more information I would say to go and read the manual . 有关更多信息,我会说去阅读手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM