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.

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

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上