简体   繁体   中英

SQL query duplicated on PHP PDO and different from the result on 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-_FLQAkpeLGm7RAYviP6yfuYvGzE9MR 32h2a5OyXjJLskWupTS9bGcZzabicGxmleJeuyiOotBQUkFto3gLGgkNxXd0n1O4DdFkUoN1WB4Jijr2UDLiW3w68lAJVUgRYn3lnN393cwWIYzKEQJiphZXmNCPGXOQp7idyt5ft9aLm6bppw5rxsJDJ2Z9VzSEn1LKXyRBOIPpAf9GDqJL6g2zk-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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM