PHP : Migration from Mysql Extension to PDO

Surely when we first learn PHP programming most of us learn the mysql_*** functions (mysql_connect(), mysql_query(), mysql_fetch_array(), etc.) to interact with MySql databases.

Get ready to learn new ways to perform database operations because the PHP development team said it will remove the mysql extension (mysql_***) functions in future PHP releases. Based on this link the development team will indeed remove the mysql extension function but not suddenly.

Greetings PHP geeks,

Don’t panic! This is not a proposal to add errors or remove this popular extension. Not yet anyway, because it’s too popular to do that now.

The documentation team is discussing the database security situation, and educating users to move away from the commonly used ext/mysql extension is part of this.

Based on the announcement, PHP developers are also advised to use 2 alternatives to the mysql extension, namely pdo_mysql and mysqli.

Well, if you don’t believe me, just read it yourself at the link above 😀

If you’ve read it means it’s time to learn to use PDO to interact with any database, not just MySQL. How? much cooler right?

PHP PDO

Connection to Database
This PDO supports several popular databases, not only mysql as supported by the mysql_*** function. Some of the databases supported by PDO include:

MySql
firebird
IBM DB2
PostgreSql
Sql Lite
To connect to the MySql database, we use the following method

<?php

/*** mysql hostname ***/
$hostname = ‘localhost’;

/*** mysql username ***/
$username = ‘username’;

/*** mysql password ***/
$password = ‘password’;

/*** mysql database / $database= ‘Databasename’; try { $dbh = new PDO(“mysql:host=$hostname;dbname=$database”, $username, $password); / print a message if the database has been successfully connected ***/
echo ‘Connected to database’;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
Judging from the syntax above, it can be seen that PDO uses the concept of OOP. Well .. because PHP in the future does lead to the path of OOP then it should be started to learn object-oriented programming paradigm :D.

Every time we have opened a connection to a database, we are obliged to close it when it is no longer used. To close it we simply assign a null value to the connection object

<?php

/*** mysql hostname ***/
$hostname = ‘localhost’;

/*** mysql username ***/
$username = ‘username’;

/*** mysql password ***/
$password = ‘password’;

/*** mysql database / $database= ‘Databasename’; try { $connection = new PDO(“mysql:host=$hostname;dbname=$database”, $username,$password); / print a message if the database has been successfully connected ***/
echo ‘Connected to database’;

/*** Close connection ***/
$connection=null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
Insert Query
To enter data into the database, of course, we use an insert query. How to use it on PDO?, see the following example. I use Prepared Statements because this technique is safe from sql injection interference besides that the prepared statement objects are compiled first so that the next call will be fast.

Conclusion
How did your friends respond after getting to know PDO and how to use it as well as OOP techniques?

Of course from now on get used to using OOP techniques in programming.
Of course also start leaving the mysql extension function because in the future it will be removed

Hopefully this article is useful

Get to know the Post and Get Methods in PHP

The post and get methods are two types of methods that are often used on an html form, both of which have their advantages and disadvantages depending on the needs in which a process will use, generally these two methods are placed in the script form as below:
form with post method
or


form method get
php get and post method

post method

this method will not display variables that are sent for processing on the next page and are usually often used on registration forms, here is an example of using the post method Submit Data

methodpost form
and here is the syntax to read the variable passed through the post method


processpost.php

get method

this method can often be used on search forms where the submitted variable will be displayed in the browser url such as a google search, this type is very necessary for pagination, the following is an example of the syntax for using the get method Submit Data

formget.php
and the following syntax is used to read the data sent via the get method:


processget.php
For more details, you might be interested in watching the use of the post and get methods in more detail via the following url

That’s a short article about the post and get methods in PHP, hopefully it’s useful for those of you who are just learning PHP

Top 10 PHP Frameworks

PHP Framework is a very useful tool for building websites, with a framework we can make websites faster, more organized, the code cleaner, and the results look more professional.

PHP Framework
Image by Fernando Lozo

But the problem is that there are so many php frameworks and they all have their advantages and disadvantages. However, it all depends on the programming skills of the programmer, even without a framework, even if the programmer is great, website creation will run smoothly.

In the article tutorial-webdesign.com will provide a list of PHP Frameworks that might be your choice in 2014.

Laravel
laravel

It is undeniable that Laravel has started to be loved by PHP programmers lately, of course this is related to the new features it has but not in other frameworks, such as Autoloading, Unit Testing, etc.

View details / Download

Yii is a high performance php framework for developing web 2.0 applications. Yii comes with quite a lot of features: MVC, DAO/Active Record, I18/L10N, caching, authentication, and role-based control, scaffolding, testing, etc.

View details / Download

Even though codeigniter is already quite outdated, lacks updates and is even said to have reached the end of its life :D, but I still like this framework with the MVC concept. The author himself has used it many times and has never been disappointed with codeigniter.

View details / Download

Phalcon
phalcon

Phalcon is a framework implemented as a C extension offering high performance and lower resource consumption.

View details / Download

Flight
flight

Flight is a fast, simple framework that allows you to create websites quickly and easily. Flight is quite easy to learn even though it is simple and quite powerful

View details / Download

PHPixie
phpixie

Originally a (Fork) of the Kohana framework, PHPixie implements MVC, easy to learn, powerful. Maybe this is worthy enough for you to try in the near future.

View details / Download

Kohana
kohana

Kohana is an open source, Object Oriented MVC web Framework created with PHP5 by a team of volunteers aiming to be a fast, secure, and small/simple framework.

View details / Download

Symphony
symfony

Created in 2005, Symfony is a very powerful MVC framework and is quite popular in the enterprise world. It is heavily inspired by other website frameworks such as Ruby on Rails, Django, and Spring. Symfony is probably one of the most complete php frameworks

View details / Download

PHP Pop
pop

Some frameworks may be quite complex and intense. Pop has been built with all experience levels in mind. Pop has a manageable learning curve to help beginners jump in and use the framework. However, this framework offers features that are quite powerful for advanced/advanced levels

View details / Download

Medo0
medo

Medoo is the lightest PHP database because it only consists of one 10.9kb file. This Micro Framework is sophisticated enough to create small and simple applications.

View details / Download

so much

So much for writing this time, there are actually many other php frameworks that are quite sophisticated, if you have other favorite frameworks besides those mentioned above maybe you can write them in the comments column so you can add other interesting framework references for readers.

CRUD (Create, Read, Update and Delete) Tutorial With CodeIgniter – Part 1

On this occasion we will discuss how to create CRUD (Create, Read, Update, and Delete) using the CodeIgniter Framework. We will learn how to view product lists and add/remove products owned by a store.

Crud Codeigniter

First of all, we need a database to store our data. I have prepared a database called db_product, in which there is a table msProduct which consists of fields / attributes productId, productName, stock. You can download the database file via this link: db_toko.sql

Folder structure that we will create:

crud_ci -> folder name
– application
– – controllers
– – – products.php -> located in the controllers folder
– – models
– – – products_model.php -> located in the models . folder
– – views
– – – products_view.php -> located in the views folder for product list view
– – – add_product_view.php -> is in the views folder for the display when inserting the product
– – – update_product_view.php -> is in the views folder to display when product updates

Because we need a connection to the database, there are several configurations that we need to do:

Setting autoload.php in the config folder to determine what things are loaded every time you open a page (there are helpers, libraries, etc., please see the user_guide codeigniter 🙂 )

$autoload[‘libraries’] = array(‘database’); //library that has been provided by codeigniter to make it easier for us to do things related to the database

$autoload[‘helper’] = array(‘url’); // helper for the url, so we can use base_url(), this base_url() will later generate your root folder, for example you use the url “localhost/tokosaya”, then localhost/tokosaya is your base_url()
after finishing with config.php, then we will continue to setting the database.php file in the config folder

$db[‘default’][‘hostname’] = ‘localhost’; // this is the hostname that we use to store our database, just leave it using localhost, because we use our own host that is on our computer
$db[‘default’][‘username’] = ‘root’; //this is the username used to access the database, default is root
$db[‘default’][‘password’] = ”; // the password used to access the database, the default value is empty, so if it is not set, then leave it as it is
$db[‘default’][‘database’] = ‘db_store’; // the name of the database that we use for this web application, this time I named it db_toko
After all the basic configuration has been completed, then we move on to the next stage, which is to create a controller that is used to handle page requests from users. We create a products.php file and then save it in the controllers folder (application/controllers/products.php). This products.php file will have a structure like this:

products.php :

Basic Tutorial on Using CodeIgniter

In this tutorial, the author will provide a tutorial on the basic use of the PHP framework, namely CodeIgniter. The author uses CodeIgniter v 2.1.4 which can be downloaded directly from the official CodeIgniter web, namely http://ellislab.com/codeigniter. What you need for this tutorial is:

  1. CodeIgniter 2.1.4
  2. Text Editor (Sublime Text, CodeIgniter, Brackets, etc.)
  3. XAMPP / WAMP

PHP CodeIgniter Framework
PHP CodeIgniter Framework

What is done for the first time is to download CodeIgniter v 2.1.4 which has been provided on the official website, then extract the downloaded results and generate a CodeIgniter folder. The folder must be moved into the htdocs folder in the XAMPP / WAMP directory.

Related Article: Codeigniter Initial Setup

After that, the reader must run the web server on XAMPP / WAMP in order to run the PHP script. If so, the reader can directly open the browser page and then write “localhost/CodeIgniter” in the Browser URL (without the “”), if the “Welcome to CodeIgniter” display appears, it means that you have successfully used the CodeIgniter framework for the first time.

Welcome Page CodeIgniter
Welcome Page CodeIgniter

Before we discuss how this page can appear, readers should know that the CodeIgniter framework has an MVC / Model View Controller structure so that if you look at the contents of the application folder in CodeIgniter that was downloaded earlier there will be a models, views and controllers folder. (For those who do not understand the MVC programming model, you can read here: http://en.wikipedia.org/wiki/Model–view–controller)

Folder Structure in CodeIgniter
Folder Structure in CodeIgniter

Let’s take a look at the routes.php file in the application/config folder, in that folder there is code like this:

[php]$route[‘default_controller’] = “welcome”;[/php]

-> this code shows that the default controller or the first controller to run when running our web application is the welcome.php file which is in the controllers folder. Readers can change the default controller according to their wishes later, be patient, hehehe

From there we already know that when the website is first run (when we type localhost/CodeIgniter) it will run the welcome.php file which is in the application/controllers folder. Inside the welcome.php file contains:

<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);

class Welcome extends CI_Controller {

/**

  • Index Page for this controller.
    *
  • Maps to the following URL
  • http://example.com/index.php/welcome
  • – or –
  • http://example.com/index.php/welcome/index
  • – or –
  • Since this controller is set as the default controller in
  • config/routes.php, it’s displayed at http://example.com/
    *
  • So any other public methods not prefixed with an underscore will
  • map to /index.php/welcome/
  • @see http://codeigniter.com/user_guide/general/urls.html
    */
    public function index()
    {
    $this->load->view(‘welcome_message’);
    }
    }
    it is the main structure for a controller in CodeIgniter. the class name used is Welcome, this is because it is adjusted to the controller file name, namely welcome.php . If we have a controller with the file name products.php then we have to create a class like this in it:

class Products extends CI_Controller {
public function index()
{
//Do something here…
}
}
The welcome.php file also has an index function, this function is the default function that will be called or executed the first time the controller runs. we can also add other functions in it, like this for example:

Creating Ajax-Based Data Search Features Using PHP, jQuery and MySql

It is common in a website or web application to have a data search feature based on certain keywords. For example, searching for book data based on the title of the book can also search based on the author of the book. This search feature makes it very easy for users to search data. So there is no reason not to implement it on the website or web application that we are developing.

In this article, I will show you how to create an article data search feature based on the title. Unlike search features in general, here we will implement Ajax to communicate with PHP to get data from the database which then the data will be displayed on the search page without any reloading process.

Similar Articles

Create Login Form with PHP, Jquery & CSS3 + Download
Setting Up Database
Create a table with article names as follows
CREATE TABLE article (
id int(11) NOT NULL AUTO_INCREMENT,
title varchar(75) NOT NULL,
link varchar(100) NOT NULL,
PRIMARY KEY(id)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1
Fill in the sample data as shown in the following image
sample data

Creating HTML+CSS pages
For the html we make it simple, which consists of 3 components. A text input, a button and a list to display search results.
Here is the code

Next we add CSS to beautify the HTML that we created earlier.

10 Best Social Network PHP Scripts

Solution to create powerful social networking sites like Facebook, Twitter, Google Plus etc
Everyone wants to create a social network like Facebook, but no one wants to code or code for years to be able to script their own social network. In this post, you can find similar social network scripts like Facebook, Youtube and Twitter. Check this out!

SocialKit Social Networking Platform PHP Script
SocialKit is a Social Networking Platform which consists of various features including Chats, Pages, Groups, Messages, Stories, Comments, Likes, Shares, Notifications, hashtags, Mentions, etc. It allows users to socialize with each other, share with their favorite communities. , connect with their favorite brands, artists, celebrities, and more.

socialkit

More Info / Demo

phpSFP – Schedule facebook posts
phpSFP is a platform where you can easily manage your scheduling for all your pages & groups in one place. It helps to send messages, advertisements, events, news and so on. It will appreciate the ease of handling: set post messages, select pages/groups for posting, set start date and time interval and your messages will be displayed depending on the schedule and this way you will have more likes – clicks – friends. Your messages will be posted as scheduled, whether you are logged into your account. While you relax, your “partner” works for you. All very easy…!
phpSFP
More Info / Demo
Easy Premium PHP Forum Script
It is very easy to integrate and install on any website. Users can interact with each other by starting lessons in the rooms they want and by leaving comments. With Responsive web design websites can be accessed from any device.

Easy Premium PHP Forum Script

More Info / Demo

Your Social Profiles at one Place – PROFILE PHP Social Network Script
If you are like an online community enthusiast, you probably have many social profiles like Facebook, Twitter, Google Plus and even Linkedin. Managing everyone’s profile links and sharing has become a real hassle especially as it seems like new social platforms are launching every day. PROFILE is a social vcard site offering the most exclusive and innovative way to link all your social profiles into one place and share a single URL with your friends and colleagues instead of separate URLs.

Your_Social_Profiles_at_one_Place_-_PROFILEM_PHP_Social_Network_Scripts

More Info / Demo

iBook The ultimate social networking platform
iBook is a project made of 5 teammebers who will always keep the iBook up-to-date. We welcome every whish and every suggestion to change something. Just let us know in the comments, and we’ll do our best to implement your whishes!

ibook-the-ultimative-social-networking-platform

More Info / Demo

Arwell – Viral media, vine and gag php script
Arwell is much like the 9gag site, a very powerful and functional admin panel, where you can manage jokes, categories, pages, users, comments and the whole app.

arwell-viral-media-vine-and-gag-script

More Info / Demo

phpShark – Social Networking Platform PHP Script
phpShark is a simple, easy and intuitive Social Network Platform.
A social network that allows users to interact with each other by chatting directly, sending messages, comments, likes and more. The app can be configured to be open to everyone, or only those who are registered. It is very easy to configure and does not require a lot of resources to the server.

phpShark_Social_Networking_Platform_PHP_Script

More Info / Demo

Fufu – Viral Media/PHP Gag Script
Fufu provides a quick and easy way to create viral media sites (popularly known as “gag sites”). Built with many of the features you need for a premium website and more. Visitors to your website that allow you to post a gag as a video type (more than 20 sites available for embedding)

fufu-viral-media-php-gag-script

More Info / Demo

Complex Social Ask & Answer PHP Script
A complex PHP Script that will help you build your own Social & Answer community where users can ask every other thing they want to know, sometimes users want to be anonymous or have no account,

Complex_Social_Ask_Answer_PHP_Script

More Info / Demo

Blogger – Community Blogging Platform
Blogger is a blogging platform, after installing Blogger, your site visitors will be able to create a blog and will be able to fully manage it from personal admin panel.

Creating Excel Reports With CodeIgniter

In this tutorial I want to share how to create an excel report with CodeIgniter. here we will use the codeIgniter Excel Generator library. This Excel Generator is a class derived from PHPExcel. Then how to use it?Additional ingredients

Download Excel Generator here https://github.com/didanurwanda/Excel-Generator
Download PHPExcel here phpexcel.codeplex.co
CREATE TABLE IF NOT EXISTS users (
id int(3) NOT NULL AUTO_INCREMENT,
name varchar(100) NOT NULL,
sex_sex varchar(10) NOT NULL,
address varchar(200) DEFAULT NULL,
email varchar(100) DEFAULT NULL,
PRIMARY KEY(id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO users (id, name, gender, address, email) VALUES
(1, ‘Dida Nurwanda’, ‘Male’, ‘Pandeglang’, ‘[email protected]’),
(2, ‘Siwi Septi Hastuti’, ‘Women’, ‘Pandeglang’, ‘-‘),
(3, ‘Ahmad’, ‘Male’, ‘Attack’, ‘[email protected]’),
(4, ‘Saepul’, ‘Male’, ‘Lebak’, ‘[email protected]’);
Create Databases and Tables
then open the phpexcel file that was downloaded earlier then extract the classes folder to the library folder in your CodeIgniter project and rename it to “PHPExcel”. Don’t forget to also extract Excel_generator.php, of course it’s still in the library folder. if everything is set, set your database configuration then create a new controller because just an example here we use welcome.php

<?php

class Welcome extends CI_Controller {

public function __construct() {
    parent::__construct();
    $this->load->database();
    $this->load->library('Excel_generator');
}


public function index() {
    $query = $this->db->get('users');
    $this->excel_generator->set_query($query);
    $this->excel_generator->set_header(array('Name', 'Gender', 'Address', 'Email'));
    $this->excel_generator->set_column(array('name', 'gender', 'address', 'email'));
    $this->excel_generator->set_width(array(25, 15, 30, 15));
    $this->excel_generator->exportTo2007('Users Report');
}

}
Controller welcome.php
Then open your browser and open your project link, there will be a download dialog, please open the excel file. For the results as follows.

Cool isn’t it, hehe.
Actually the main script to create an excel file is just

$query = $this->db->get(‘users’);
$this->excel_generator->set_query($query);
$this->excel_generator->set_column(array(‘name’, ‘gender’, ‘address’, ’email’));
$this->excel_generator->exportTo2007(‘Users Report’);

Then what’s the difference if the script is just like above? ok I explain one by one.

$this->excel_generator->set_query($query);
Used to call queries from the database.

$this->excel_generator->set_column(array(‘name’, ‘gender’, ‘address’, ’email’));
Used to display which fields will be displayed in excel. These fields are retrieved from a database table.

$this->excel_generator->exportTo2007(‘Users Report’);
Used to export reports into .xlsx files (Excel 2007) with the name Report Users

$this->excel_generator->exportTo2003(‘Users Report’);
Used to export reports into .xls files (Excel 2003) with the name Report Users

$this->excel_generator->set_width(array(25, 15, 30, 15));
Used to add column width in excel, the number must match the number of columns you input.

$this->excel_generator->set_header(array(‘Name’, ‘Gender’, ‘Address’, ‘Email’));
Used to add a Title to each column in Excel.

$this->excel_generator->start_at(8);
Used to start report generation from line 8. This can be used to add a Title or Document Header to your report.

Update Your Website Footer Dynamically With Javascript and PHP

It’s already December 30, 2014 now, yes, two days from now the year has changed to 2015. Usually dynamic websites always follow developments, and can always look up to date even though they are not changed manually. Yes, that’s the nature of dynamic websites, it’s easier to make changes.

Usually the date in the footer, to be precise in the copyright section, is always filled with the current year. At the end of this year, have you prepared the changes for the year for your website footer?

Date Time Year Footer

If not, you can use the Javascript or PHP script below so that the year writing on your website automatically changes when you enter a new year. Usually website owners forget this little thing, they don’t make the year change dynamically, and make the website look old.

Read also:

Review of 2013 Web Design Trends
Trend Web Design 2012 version of T-WD
There are several examples that you can follow, just copy and paste them into the footer of your website.

  1. Using Javascript
    For the first example you can use javascript, with javascript you can put it anywhere on your HTML web page, whatever platform you use you can put this script to display the current year.

Result: 2014

  1. Using Javascript 2
    To make it more interesting, you can add complementary words so that what appears is not only the year. Maybe you want to add copyright, and so on

© 2011, Company.
Results: © 2010-2014, Company.

  1. Using PHP
    If you use php you can put the following script in the footer of your website, or if you use a CMS like wordpress, you can simply find the footer.php file in the theme folder you are using and paste the following code.


Result: 2014

  1. Using PHP 2
    If you want to add additional information such as complementary words, you can follow the steps below

© Company.
Results: © 2008-2014 Company.

Those are some ways to keep your footer up to date by writing the year that automatically changes every year.

Check Domain Name Availability With PHP

Scripts and tutorials to check domain availability with PHP — Maybe we often check the availability of the domain that we will order at a web hosting that provides server rental services, but have we ever thought and found out how to make a script to check the availability of the domain?

Then in our minds, is it difficult? consuming a lot of scripts? maybe only senior programmers understand? Turns out it was all wrong. The script to check the domain is very simple, let’s make a domain check script with php:

I’m using xampp in this tutorial, please create a new folder in htdocs named check_domain create a new file index.php, in thecheck_domain folder, also create a new css folder in it create a new file and name it style.css, here is a picture of the folder structure:

Untitled

Here’s the script for css/style.css:

body {
margins: 0px;
padding: 0px;
}

check_domain {

margins: 250px;
font-size:28px;
}

.input {
font-family: “Verdana”;
font-size: 28px;
colors: #000;
text-decoration: none;
padding: 5px;
border: 1px solid #009BDB;
}

.resultdomain {
width: 500px;
padding: 10px;
background: #EEE;
font-size: 16px;
}

.font-green {
color: green;
}

.font-red {
color: red;
}
css/style.css
The main script for index.php, this script is placed in the body, don’t forget to include style.css in the head section by using the rel=”style.css” link.