gpt4 book ai didi

mysql - 无法将新数据库连接转换为旧数据库连接

转载 作者:行者123 更新时间:2023-11-30 23:01:30 24 4
gpt4 key购买 nike

我需要为我的项目转换这个新的数据库连接并从数据库中检索信息到旧的方式。我已经根据较旧的时尚方式 PHP 4.0 完成了连接,但我需要将此 PDO 连接代码用于较旧的数据库连接,因为我不熟悉如何使用较旧的数据库连接来使用和检索信息。谢谢。

<?php 
$pdo = new PDO('mysql:host=localhost;dbname=sitepoint', 'root', '*****');
$opts = $_POST['filterOpts'];
$qMarks = str_repeat('?,', count($opts) - 1) . '?';
$statement = $pdo->prepare("SELECT mobile_phone.id, name, model, price FROM mobile_phone INNER JOIN brand ON brand_id = brand.id WHERE name IN ($qMarks)");
$statement -> execute($opts);
$results = $statement -> fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo($json);
?>

我已经用旧的数据库连接尝试过这种方式,但它什么也没给我:

$opts = $_POST['filterOpts'];
$qMarks = str_repeat('?,', count($opts) - 1) . '?';
$statement = "SELECT mobile_phone.id, name, model, price FROM mobile_phone INNER JOIN brand ON brand_id = brand.id WHERE name IN ($qMarks)";
$statement1 = mysql_query($statement);
$results = mysql_fetch_assoc($statement1);
$json = json_encode($results);
echo($json);

最佳答案

mysql_query(及相关)不支持绑定(bind),所以最终查询如下
从 mobile_phone INNER JOIN brand ON brand_id = brand.id WHERE name IN (?,?,?,?) 选择 mobile_phone.id、名称、型号、价格

为了能够以与 PDO 相同的方式使用动态参数,您应该手动包装每个参数。

一种方法可以如下,

$opts = array();
foreach ($_POST['filterOpts'] as $filter) {
$opts[] = '\'' . mysql_real_escape_string($filter) . '\'';
}

$qMarks = implode(',', $opts);
$statement = "SELECT mobile_phone.id, name, model, price FROM mobile_phone INNER JOIN brand ON brand_id = brand.id WHERE name IN ($qMarks)";
$statement1 = mysql_query($statement);
$results = mysql_fetch_assoc($statement1);
$json = json_encode($results);
echo($json);

关于mysql - 无法将新数据库连接转换为旧数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23787407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com