Author Topic: pdo duplicate error  (Read 424 times)

klyxmaster

  • Regular Member
  • **
  • Posts: 29
  • Karma: 0
    • View Profile
I've searched all over for the solution, but the answers I find are for people that are making the same mistake:
1
 ->fetch() or ->fetchAll()

I do not make this mistake, I use the correct format and still get the same results.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  
  
try
  
	
{
  
	
	
$db db_connect('myDB');
  
	
	
$search strtolower($_GET['field']);      
  
//i am using GET vs POST for debug reasons      
  
	
	

  
	
	
$sql "SELECT *
  
	
	
	
	
FROM collection
 
	
	
	
	
WHERE name LIKE ? ORDER BY name ASC"
;
	
	
	

 
	
	
$params = array("%$search%");
 
	
	
$query $db->prepare($sql);
	
	

 
	
	
$query->execute($params);
 
	
	

 
	
	
if(
$query)            
 
	
	
{
 
	
	
   echo
'<table>';
 
	
	
	

 
	
	
	
while(
$link $query->fetch(PDO::FETCH_OBJ)):
	

 
// also done fetch(PDO::FETCH_ASSOC)) as well, but it still gives a double result 
 
echo'<pre>';print_r($link);echo'</pre>';
 
bla bla bla......


the rest works fine...

what am I doing wrong?
obviously if i do a LIMIT 1 it works fine, but then I miss out on the rest of the restuls for a wildcard.
Which by the way, wild card seems to give a dupe for each entry. in other words they all come up twice

dbconnect.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   function db_connect($database)
      {
          try {
              
$dbcfg parse_ini_file('config.ini');
              
$db = new PDO(MYSQL_TYPE.":host=".MYSQL_HOST.";dbname=".$databaseMYSQL_USERMYSQL_PASS);
              
              
$db->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
  
	
	
	
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODEPDO::FETCH_OBJ); 
              
$db->setAttribute(PDO::ATTR_EMULATE_PREPARESfalse);            
             return 
$db;
         }catch(
PDOException $ex) {
             echo 
"construct error: <br/>".$ex->getMessage();
         }
      
     }



thank you in advance for any insite

PHP Help Forum

pdo duplicate error
« on: April 16, 2017, 05:36:23 pm »



PHP Help Forum

Re: pdo duplicate error
« Reply #1 on: April 16, 2017, 07:30:22 pm »


Kevin Rubio

  • Professional PHP Helper
  • Senior Member
  • *
  • Posts: 1780
  • Karma: 107
  • Programmer Available for hire
    • View Profile
    • Galaxy Internet
Have you read the manual?  fetch Fetches a row .
http://php.net/manual/en/pdostatement.fetch.php


 If you want more than one row use fetchall.

"PDOStatement::fetchAll — Returns an array containing all of the result set rows"

You are using LIKE with no parameters so it could to match many times.
« Last Edit: April 17, 2017, 12:38:53 am by Kevin Rubio »
PDO Bumpstart Database

The XY Problem
The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help. http://xyproblem.info/

PHP Help Forum

Re: pdo duplicate error
« Reply #2 on: April 17, 2017, 12:28:55 am »


klyxmaster

  • Regular Member
  • **
  • Posts: 29
  • Karma: 0
    • View Profile
yes.
been programing with php for some time. recently switched to pdo
have many apps that work nicely with LIKE

even doing (direct)
1
 $query $db->query("SELECT * FROM table WHERE name LIKE '%search%'");


gives double results (not many times)

an example dump result for "john doe" would be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  
  stdClass Object
  
(
      [
name] =>John Doe
  
)
  
  
stdClass Object
  
(
      [
name] => John doe
 
)
 
 
stdClass Object
 
(
     [
name] => John doe jr
 
)
 
 
stdClass Object
 
(
     [
name] =>John doe jr
 
)
 



regardless how I do the query, it produces double results w/o the "DISTINCT"

other methods:
1
2
3
4
 
 $query 
$db->prepare("SELECT * FROM table WHERE name LIKE ?");
 
$query $db->prepare("SELECT * FROM table WHERE name LIKE :search");
 




PSS
If you check the code - LIKE has a parameter, it is just producing a duplicate of it's results.

pss
Links are nice - but give an example. php.net is old. How does your link address the duplicate issue? It is formatted as instructed by php.net. Any one who has done ANY search within the last decade, who writes with PHP will be aware of both php.net, phpfreaks and w3schools to name a few.
« Last Edit: April 17, 2017, 06:16:18 am by klyxmaster »

PHP Help Forum

Re: pdo duplicate error
« Reply #3 on: April 17, 2017, 06:11:33 am »


Kevin Rubio

  • Professional PHP Helper
  • Senior Member
  • *
  • Posts: 1780
  • Karma: 107
  • Programmer Available for hire
    • View Profile
    • Galaxy Internet
PDO Bumpstart Database

The XY Problem
The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help. http://xyproblem.info/

PHP Help Forum

Re: pdo duplicate error
« Reply #4 on: April 17, 2017, 10:47:55 am »



PHP Help Forum

Re: pdo duplicate error
« Reply #5 on: April 20, 2017, 11:30:39 am »



PHP Help Forum

Re: pdo duplicate error
« Reply #6 on: April 20, 2017, 01:13:28 pm »



PHP Help Forum

Re: pdo duplicate error
« Reply #7 on: April 21, 2017, 09:29:48 am »



PHP Help Forum

Re: pdo duplicate error
« Reply #8 on: April 21, 2017, 09:39:16 am »



klyxmaster

  • Regular Member
  • **
  • Posts: 29
  • Karma: 0
    • View Profile