将PDO SQL降级为mysql_query

时间:2015-11-13 04:25:30

标签: php mysql mysqli pdo

我正在使用一个使用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_样式调用一起使用?

1 个答案:

答案 0 :(得分:0)

错误,将第一个示例 - / - 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);
}