PHP:将INSERT MySQLi转换为PDO [英] PHP: Convert INSERT MySQLi to PDO

查看:118
本文介绍了PHP:将INSERT MySQLi转换为PDO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PDO编程的新手.根据我的问题,有人可以帮助我将MySQLi转换为PDO吗?下面是代码:

I'm new in PDO programming. Based on my question, can anyone help me to convert MySQLi to PDO? Below is the code:

<?php
require_once "config.php";

$photo_before = $_POST['photo_before'];
$report_id = $_GET["report_id"] ?? "";

$sql_query = "UPDATE report SET photo_before ='$photo_before', time_photo_before = NOW(), ot_start = '16:00:00' WHERE report_id = '$report_id'";

if(mysqli_query($conn,$sql_query))
{
    echo "Data Save!";
}
else
{
    echo "Error!! Not Saved".mysqli_error($conn);
}

?>  

希望有人能帮助我.谢谢

Hope there's a kind people to help me. Thanks

推荐答案

您可以按以下方式使用PDO:

You can use PDO as this:

$dsn = "mysql:host=localhost;dbname=myDatabase;charset=utf8mb4";
$options = [
  PDO::ATTR_EMULATE_PREPARES   => false, // turn off emulation mode for "real" prepared statements
  PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION, //turn on errors in the form of exceptions
  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, //make the default fetch be an associative array
];
try {
  $pdo = new PDO($dsn, "username", "password", $options);
} catch (Exception $e) {
  error_log($e->getMessage());
  exit();
}

和准备好的语句:

$stmt = $pdo->prepare("UPDATE report SET photo_before =?, time_photo_before = NOW(), ot_start = '16:00:00' WHERE report_id = ?");
$stmt->execute([$_POST['photo_before'],$_GET["report_id"] ?? ""]);

这篇关于PHP:将INSERT MySQLi转换为PDO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆