CategoryPHP

Remove directory and its sub folders & files in codeigniter

We can extend the core directory helper.  Creating a new file as MY_directory_helper.php and add the following code in helper file to remove directory and it’s sub folders &  files.

 

Perform ajax call in codeigniter

You can simply add the following jquery code to perform ajax using codeigniter

 

Clear APC cache in Apache using PHP

The following code will clear the apc cache using PHP

 

PDO right way to execute queries

We can execute queries in the following ways using PDO . I preferred to use prepare method.
> Query Method
> Execute Method
> Prepare Method

Here we can take an example to delete 3rd record from departments table.

Establish a DB connection.
$conn = new PDO(‘mysql:host=localhost;dbname=demo’, ‘username’, ‘password’);

Query Method
Syntax: $conn->query();
Parameter: SQL Statement
Success: PDOStatement
Failure: false
Example:

Dis-advantage:
We need to care about SQL injection attacks. means if $id contains any vulnerability.

Execute Method
Syntax: $conn->exec();
Parameter: SQL Statement
Success: Number of rows
Failure: false
Example:

Dis-advantage:
We need to care about SQL injection attacks. means if $id contains any vulnerability.

Prepare Method
Syntax: $conn->prepare();
Parameter: SQL Statement
Success: PDOStatement
Failure: false
Example:

Advantages:
Here we can not pass the value directly in SQL statement instead use named parameters (:name or ?). It prevent SQL injection attacks

Use PDO instead of mysqli & mysql extensions

Mysql Extension
Do not use mysql functions for your projects. it is already deprecated from PHP 5.5.0 and it will be removed in future version.
(for eg:)
mysql_connect
mysql_select_db
mysql_close

Mysqli Extension
We can use mysqli functions if you are strict with mysql DB. This syntax not supports other DB.

PDO (PHP Data Objects)
Always, we should choose latest one. So that we are in the right direction. PDO is a latest approach to perform database operations which was introduced from PHP 5.
> It works through PDO drivers and currently it supports number of major databases (Mysql, Postgres, Sqllite..etc.,).
> You don’t need to change your syntax in code if you are planning to switch one DB to another DB
> You don’t need to write 2 different syntax’s if you are using 2 different DB in your projects
> I like to use prepared statement. So that we don’t care a bout SQL injection attacks. No need to filter every input using PHP functions. also, please check this topic “PDO right way to execute queries

© 2017 Asik Blog

Theme by Anders NorenUp ↑