The SQL syntax doesnot have anything wrong but it has something wrong it feels like it isn't getting inserted in SQL.
function getAllStudentsDetails($param){
$sql = "SELECT * FROM students LIMIT ?,?";
$data = Db::query($sql,$param);
return Db::fetchObject($data);
}
While this code returns values if I simply typed
$sql = "SELECT * FROM students LIMIT $param[0],$param[1];
Am I doing something wrong here? By the way Db::query($sql,$param);
returns value for all other function.
Custom functions:
static function query($sql, $values=null) {
$stmt = self::$conn->prepare($sql);
$stmt->execute($values);
return $stmt;
}
static function fetchObject($result) {
if ($result) {
$result = $result->fetchAll(PDO::FETCH_OBJ);
}else{
$result = "Error in fetching Object the sql returned false";
}
return $result;
}
UPDATE:
I have tried to find out the problem and it seems that the values in LIMIT ?,?
are being passed as a string
no matter how you do either by directly execute
or bind
method both returns the string as it's type. The error message displays '0','10' as the values that is being placed by PDO. It seems PDO escaped my int as string via prepare method.
UPDATE 2
My guess for prepare function in above update was indeed correct when typing sql in mysql like this
SELECT * FROM `students` WHERE 1 LIMIT '10','10'
Returns error as
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near ''10','10'' at line 1
Which is indeed the error returned by PHP. The way prepare method was made is the reason for this. It seems to quote everything in order to escape values to put in database in order to protect from SQL injection.