dpt8910 2019-04-21 18:51
浏览 75
已采纳

在PHP PDO上复制的SQL查询与phpmyadmin上的结果不同

I have duplicated result of the same DB row when I query with PHP PDO and when I execute the same query on phpmyadmin I get 2 distinct rows which is accurate.

I must get the same 2 rows like on phpmyadmin but with pdo, so here is my code and my pdo result:

try {
    $conn = new PDO('mysql:host=mysql;dbname=mydb;charset=utf8mb4', 'root', 'tiger');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $result = $conn->query("SELECT COUNT(*) AS num_rows FROM `banner`");
    $numRows = $result->fetchColumn();

    if ($numRows > 0) {

    $domain = $_GET['domain']; // URL PARAMETER GOES HERE

    $myQuery = $conn->prepare(
"SELECT * 
    FROM
        banner B 
    JOIN dictionary D
            ON B.dictionaryId = D.id
    WHERE
        B.domains = : domains
        AND B.dictionaryId IS NOT NULL
        AND B.startDate <= NOW ()
        AND B.finishDate >= NOW ()
    ORDER BY
        B.position ASC");
    $myQuery->bindParam(':domains', $domain);
    $myQuery->execute();
    $banner = $myQuery->fetch();
    // echo $banner[14];
    var_dump($banner);
    die;

And this is the Results:

/var/www/html/dropbox_db_connection.php:54: array (size=33) 'id' => string '1' (length=1) 0 => string '5' (length=1) 'dictionaryId' => string '1' (length=1) 1 => string '1' (length=1) 'name' => string 'blink' (length=5) 2 => string 'blink' (length=5) 'destinationURL' => string 'github.com' (length=10) 3 => string 'github.com' (length=10) 'domains' => string 'github' (length=6) 4 => string 'github' (length=6) 'imageURL' => string 'https://dl.dropboxusercontent.com/apitl/1/ABimtamHV3_o7EnMC3s5tHpYRwMwS8cyZMtfQGlqpxa6yepgnelceukKJbo9eGONQLquiy-iPAFaeK0NT1cmx0RWGu6Z_DzN0-_FLQAkpeLGm7RAYviP6yfuYvGzE9MR32h2a5OyXjJLskWupTS9bGcZzabicGxmleJeuyiOotBQUkFto3gLGgkNxXd0n1O4DdFkUoN1WB4Jijr2UDLiW3w68lAJVUgRYn3lnN393cwWIYzKEQJiphZXmNCPGXOQp7idyt5ft9aLm6bppw5rxsJDJ2Z9VzSEn1LKXyRBOIPpAf9GDqJL6g2zk-cvw7sRia0' (length=365) 5 => string 'https://dl.dropboxusercontent.com/apitl/1/ABimtamHV3_o7EnMC3s5tHpYRwMwS8cyZMtfQGlqpxa6yepgnelceukKJbo9eGONQLquiy-iPAFaeK0NT1cmx0RWGu6Z_DzN0-_FLQAkpeLGm7RAYviP6yfuYvGzE9MR32h2a5OyXjJLskWupTS9bGcZzabicGxmleJeuyiOotBQUkFto3gLGgkNxXd0n1O4DdFkUoN1WB4Jijr2UDLiW3w68lAJVUgRYn3lnN393cwWIYzKEQJiphZXmNCPGXOQp7idyt5ft9aLm6bppw5rxsJDJ2Z9VzSEn1LKXyRBOIPpAf9GDqJL6g2zk-cvw7sRia0' (length=365) 'position' => string '5' (length=1) 6 => string '5' (length=1) 'startDate' => string '2019-04-18 03:00:00' (length=19)
7 => string '2019-04-18 03:00:00' (length=19) 'finishDate' => string '2019-05-04 00:00:00' (length=19) 8 => string '2019-05-04 00:00:00' (length=19) 9 => string '1' (length=1) 'it' => string 'Sed tempus libero a tristique placerat. ' (length=41) 10 => string 'Sed tempus libero a tristique placerat. ' (length=41) 'en' => string 'Curabitur at justo sit amet mi aliquam vestibulum. ' (length=52) 11 => string 'Curabitur at justo sit amet mi aliquam vestibulum. ' (length=52)
'fr' => string 'Duis sed elit suscipit, venenatis ipsum vitae, molestie elit. ' (length=63) 12 => string 'Duis sed elit suscipit, venenatis ipsum vitae, molestie elit. ' (length=63) 'es' => string 'Ut ac tortor semper, finibus est ac, porta erat. ' (length=50) 13 => string 'Ut ac tortor semper, finibus est ac, porta erat. ' (length=50) 'pt' => string 'Morbi sit amet quam facilisis, tristique mi a, sagittis quam. ' (length=63) 14 => string 'Morbi sit amet quam facilisis, tristique mi a, sagittis quam. ' (length=63) 'de' => string 'Nulla nec sem quis velit tristique tempus vel id augue. ' (length=57) 15 => string 'Nulla nec sem quis velit tristique tempus vel id augue. ' (length=57) 'nl' => string 'Mauris tincidunt leo eget tincidunt bibendum. ' (length=47) 16 => string 'Mauris tincidunt leo eget tincidunt bibendum. ' (length=47)

  • 写回答

1条回答 默认 最新

  • douyi1084 2019-04-21 19:00
    关注

    You have 2 problems, the first which is the row being duplicated, this is down to fetch() defaulting to FETCH_BOTH, which means it will return both an associative set of data as well as a numerically indexed set of data (both with the same values). I usually use PDO::FETCH_ASSOC as this is just values indexed by column names.

    The second is that you only retrieve 1 row, the call to fetch() would normally be in a loop...

    $myQuery->execute();
    while( $banner = $myQuery->fetch(PDO::FETCH_ASSOC)) {
        // echo $banner['pt'];
        var_dump($banner);
    }
    

    or set the default mode for fetch using

    $connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    

    Do this just after making the connection so that all calls to fetch() will give the same style.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证