PDO double results

Codes here !

Moderators: egami, macek, gesf

Post Reply
klyxmaster
New php-forum User
New php-forum User
Posts: 6
Joined: Tue Mar 28, 2017 2:42 pm

Sun Jul 30, 2017 10:08 am

I see this error all over the net, but the php code they use is not what I use - here are some important snippets. It is an old app, I went back to after my frustration of not finding why the double results are happening, so wrote it from scratch, using PDO (not my fav) and it is still spitting out double reuslts.


my db conn

Code: Select all

 $db = new PDO(MYSQL_TYPE.":host=".MYSQL_HOST.";dbname=".MYSQL_DB, MYSQL_USER, MYSQL_PASS);
            $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
            $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);// force assoc!
            return $db;

The following spits out 2 of each. So if the name is John Doe, I'll get 2 of everything listed below.

Code: Select all

$query = $db->query("SELECT * FROM `collection` WHERE `name` LIKE '%$name_search%'");
            $query->execute();
            $found = false;
            echo '<h2>SEARCH RESULTS FOR: '.$name_search.'</h2>';
            While($result = $query->fetch(PDO::FETCH_ASSOC ))
            {
                $found = true;
                echo "<p><a href='{$result['urlLink']}' target='_blank'>{$result['name']}</a><br/> ";                
                echo $result['desc'].'<br/>';
                                
                echo "<a href='{$result['officialLink']}' target='_blank'>".$result['officialLink']."</a><br/>";
                echo "<a href='{$result['urlLink']}' target='_blank'>download</a></p>";
            }
            echo '</div>';
Why am I getting a double display?
Thanks!

klyxmaster
New php-forum User
New php-forum User
Posts: 6
Joined: Tue Mar 28, 2017 2:42 pm

Sun Jul 30, 2017 1:04 pm

Update:
I moved it to my server(remote) regardless, and funny, there is no double results. I am running php7 and I am sure the site is still php5.x
anything I should check for to compare?

User avatar
Strider64
php-forum Active User
php-forum Active User
Posts: 310
Joined: Sat Mar 23, 2013 8:24 am

Mon Jul 31, 2017 3:54 am

Have you run phpinfo() to see what version of php the server(s) are running?
Life is a fig newton of your imagination! https://www.pepster.com/index

klyxmaster
New php-forum User
New php-forum User
Posts: 6
Joined: Tue Mar 28, 2017 2:42 pm

Mon Jul 31, 2017 5:43 pm

Remote Server: PHP Version 5.5.38
Local Server: PHP Version 5.6.3

I thought I grabbed xampp 7 (no biggy) but still,
so why the double results on local server?
Is there a php.ini file I need to check?
thanks!

klyxmaster
New php-forum User
New php-forum User
Posts: 6
Joined: Tue Mar 28, 2017 2:42 pm

Sat Aug 12, 2017 5:08 pm

/BUMP

User avatar
Strider64
php-forum Active User
php-forum Active User
Posts: 310
Joined: Sat Mar 23, 2013 8:24 am

Sun Aug 13, 2017 4:51 am

Maybe doing this way and it even uses prepared statements

Code: Select all

<?php
$query = 'SELECT * FROM collection WHERE name LIKE :name_search';  // I suggest to lose the ` ticks:

$stmt = $db->prepare($query);

$stmt->execute(array(':name_search' => '%' . $name_search . '%'));

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo '<h2>SEARCH RESULTS FOR: ' . $name_search . '</h2>';
foreach ($result as $row) {
    $found = true;
    echo "<p><a href='{$row['urlLink']}' target='_blank'>{$row['name']}</a><br/> ";
    echo $row['desc'] . '<br/>';

    echo "<a href='{$row['officialLink']}' target='_blank'>" . $row['officialLink'] . "</a><br/>";
    echo "<a href='{$row['urlLink']}' target='_blank'>download</a></p>";
}

echo '</div>';
Obviously this isn't tested and might require a few tweaks to get it working.
Life is a fig newton of your imagination! https://www.pepster.com/index

klyxmaster
New php-forum User
New php-forum User
Posts: 6
Joined: Tue Mar 28, 2017 2:42 pm

Tue Aug 15, 2017 2:45 pm

While this is SUPER embarrasing...but I am posting it for future issues from others that they can check

I decided to load up the db in navcat (easier to read than phpadmin)
an lo-and-behold there were dupes.

apparently (not checked yet at the time of this writing) there is an issue with my add data script. must be duping as it is added. I have since added a unique ID (which I normally do, not sure why I did not this time - prob because I thought this was a rough draft and forgot).

Thanks for your patience!

cheers!

User avatar
hyper
php-forum Active User
php-forum Active User
Posts: 448
Joined: Mon Feb 22, 2016 5:52 pm

Tue Aug 15, 2017 3:05 pm

At least you worked it out, so well done for owning up.

Post Reply