我正在编写一个使用mysql_语句的相当旧的脚本。每当我试图发布任何mysqli_或PDO语句时,事情都会变得一团糟。因此,我需要重新编写以下代码,以便与我正在重新编程的脚本配合使用。
$ids = $_POST['delete'];
$params = array_fill(0, count($ids), "?");
$sql = "DELETE FROM some_table WHERE id IN (" . implode(",", $params) . ")";
$pdo = new PDO('mysql:host=localhost;dbname=mydb', 'user', 'pass');
$stmt = $pdo->prepare($sql);
$stmt->execute($ids);
我正在使用的代码中的常见连接看起来更像:
include "inc/config.php";
include "inc/funcs.php";
@mysql_connect($dbhost,$dbuser,$dbpass);
@mysql_select_db($dbname) or die( "Unable to select database");
include "inc/userauth.php";
if ($_POST && isset($_POST['value'])) {
$value = mysql_real_escape_string($_POST['value']);
$sql = "UPDATE oto_members SET right_sidebar = '".$value."' WHERE username='".$_SESSION['uid']."'";
mysql_query($sql);
}
那么,我该如何重写PDO代码,使其能够与旧的mysql_风格调用一起工作呢?
Err,将第一个示例-/-pdo的代码复制/粘贴到第二个示例中?
include "inc/config.php";
include "inc/funcs.php";
@mysql_connect($dbhost,$dbuser,$dbpass);
@mysql_select_db($dbname) or die( "Unable to select database");
include "inc/userauth.php";
if ($_POST && isset($_POST['value'])) {
$value = mysql_real_escape_string($_POST['value']);
$ids = $_POST['delete'];
$params = array_fill(0, count($ids), "?");
$sql = "DELETE FROM some_table WHERE id IN (" . implode(",", $params) . ")";
mysql_query($sql);
}