Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

Just wondering if I am doing it right or the PHP pdo code below can be optimized?

Forgot to mentioned i used stripslashes and bind param now.. is it safe now from sql injection? please feel free to comment for any better optimization.

What I have tried:

// Secure and sanitize post variables.
$start = trim(stripslashes($_POST['start']));
$end = trim(stripslashes($_POST['list_records']));
$total_RowCount = trim(stripslashes($_POST['total_records']));


<pre><$stmt = $db->prepare("SELECT a,b,c FROM employees ORDER BY emp_no DESC LIMIT ?,?");
$stmt->bindParam(1, $start,PDO::PARAM_INT);
$stmt->bindParam(2, $end,PDO::PARAM_INT);
$stmt->execute();
$data = $stmt->fetchAll();
Posted
Updated 21-Dec-18 4:43am
v4

PHP
/ Prepare Limit Clause from start to end$sqlAll=("SELECT * FROM user_details ORDER BY user_id DESC LIMIT $start, $end ");
...
// Select results
$data = $db_con->query("SELECT * FROM user_details ORDER BY user_id DESC LIMIT $start, $end ")->fetchAll();

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
Possibly.

Unless you absolutely need to SELECT *, it is better to call only the needed columns.
Example: SELECT TableID, Column1, Column2 FROM ...
Besides eliminating the extra load of bundling the data together, there is less network usage and will use less memory in your calling application.

Another item application side which may offer a performance increase is to retrieve your column values by index as opposed to name. In other languages and data connectors using the "name" of a column requires the application to know which column is where. This does after all come back as an indexed array. Most likely you won't notice a significant increase doing this.

Database design can be a big boost. Do you have a Primary Key and Indexes installed?
 
Share this answer
 
Thank you MadMyche, the only thing left is now to transfer the query with PDO param substitution? Anyone around to advise how to use PARAM binding in the above query please.

Here what i got and definately it would be helpful for others.
please vote if BINDING below is helpful to you.

$stmt = $db->prepare("SELECT a,b,c FROM employees ORDER BY emp_no DESC LIMIT ?,?");
$stmt->bindParam(1, $start,PDO::PARAM_INT);
$stmt->bindParam(2, $end,PDO::PARAM_INT);
$stmt->execute();
$data = $stmt->fetchAll();


If anyone have a better suggestion, please comment.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900