0

I am trying to create a universal method/function on php which uses any select statement with a where clause. However, something is not right. Can you give me a hand?

Example of using data:

DB inherit PDO class.
$db=new DB;
$db->select("SELECT count(*) FROM `login` WHERE Email='?'", 
            "papa1980@gmail.com");

Here is function

public function select($sqlString,...$bindParameters)
{

   $stmt=$this->prepare($sqlString);
   foreach($bindParameters as $key=>$bindParameter)
   {

       $stmt->bindParam(($key+1),$bindParameter);
       echo '<pre>' . print_r($stmt, true) . '</pre>';
   }
   echo '<pre>' . print_r($bindParameters, true) . '</pre>';
   if($stmt->execute())
   {
       echo "execution was succesful";
   }
  echo '<pre>' . print_r($stmt->fetchAll(), true) . '</pre>';
}

Output

PDOStatement Object ( [queryString] => SELECT count(*) FROM login WHERE Email='?' )

Array ( [0] => papa1980@gmail.com )

execution was succesful

Array ( [0] => Array ( [count(*)] => 0 [0] => 0 ) )

Why is it returning 0? I use

SELECT count(*) FROM `login` WHERE Email='papa1980@gmail.com' 

in mysql and it returns 1. Something is not correct in my php code.

Theresa
  • 3,515
  • 10
  • 42
  • 47
user3661564
  • 107
  • 2
  • 2
  • 8
  • Is your function returning anything ? – Maximus2012 May 06 '15 at 20:13
  • Maybe it should return `$stmt->fetchAll()` if successful and `FALSE` if not in which case you can do the condition checking where you are using this function. – Maximus2012 May 06 '15 at 20:14
  • Look at the answer(s) to this question and see if that helps: http://stackoverflow.com/questions/20566960/extend-pdo-class . You might also be better off using an ORM solution. – Maximus2012 May 06 '15 at 20:16
  • http://stackoverflow.com/questions/108699/good-php-orm-library – Maximus2012 May 06 '15 at 20:16
  • I need result from each SQL to be return.Howerver as you can see execution was succesful Array ( [0] => Array ( [count(*)] => 0 [0] => 0 ) ) and result shall be 1 – user3661564 May 06 '15 at 20:46
  • Are you getting the result from your function then ? I doubt that because your function is not returning anything. – Maximus2012 May 06 '15 at 20:51
  • It is working my sql was wrong.SELECT count(*) FROM `login` WHERE Email=? you shall not put ? in '' :) – user3661564 May 06 '15 at 20:57

1 Answers1

0

Function is working but SELECT count(*) FROM login WHERE Email=? was wrong because ? shall be not in ''

user3661564
  • 107
  • 2
  • 2
  • 8