Install Phalcon PHP framework in Fedora

In my last article i discussed that how to install phalcon framework in openSUSE, and i guess it was quite fun and easy. Today i am extending the installation to Fedora. I installed it on Fedora 19, 20 and i hope there would be no problems for anyone, it will be quite simple and easy to install. As we know, we don’t have any repo or RPM at the moment for Fedora, so we will build it from source. Don’t worry it will not be scary. Read this for the dependencies 1st.

Fire up the terminal and i assume that you are in your home dir ([user@localhost ~]$). But no worries, you can change the dir to any where you want like your backup drive etc. Let’s go with home in this article. We need to clone the phalcon repository from github and create the extension from source.

[cc lang=”bash” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”false” lines=”3″ tab_size=”4″]
[user@localhost ~]$ git clone git://github.com/phalcon/cphalcon.git
[user@localhost ~]$ cd cphalcon/build
[user@localhost ~]$ sudo ./install
[/cc]

That will do the necessary for now, one last thing now. Open php.ini in terminal or whatever GUI editor you prefer(i will use vim):

[cc lang=”bash” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”false” lines=”1″ tab_size=”4″]
[user@localhost ~]$ sudo vim /etc/php.ini
[/cc]

And add this line:

[cc lang=”bash” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”false” lines=”1″ tab_size=”4″]
extension=phalcon.so
[/cc]

Save and that’s it. Time to restart the server:

[cc lang=”bash” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”false” lines=”1″ tab_size=”4″]
[user@localhost ~]$ sudo service httpd restart
[/cc]

Download any of the app from the phalcon repo like invo OR vokuro etc, and give it a try.

Enjoy!

Install Phalcon PHP framework in openSUSE

Phalcon is a web framework implemented as a C extension offering high performance and lower resource consumption”. From the benchmark page, “The compiled nature of Phalcon offers extraordinary performance that outperforms all other frameworks measured in these benchmarks”. Personally i find it very easy to work with, less dirs and files for the project. And it’s documentation is quite organized and useful. Installation in Linux distros is quite easy for most of the parts. Let’s install in openSUSE 12.3 and 13.1. 1st of all let’s thank Mariusz Łączak for his useful work to provide repositories. Fire up a terminal and become root by su – .

If you are on openSUSE 12.3, add this repo:

[cc lang=”bash” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”false” lines=”2″ tab_size=”4″]

zypper ar -f http://download.opensuse.org/repositories/home:/mruz/openSUSE_12.3/ Phalcon

[/cc]

For openSUSE 13.1:

[cc lang=”bash” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”false” lines=”2″ tab_size=”4″]

zypper ar -f http://download.opensuse.org/repositories/home:/mruz/openSUSE_13.1/ Phalcon

[/cc]

zypper-add-repo-phalcon

Refresh the repos:

[cc lang=”bash” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”false” lines=”2″ tab_size=”4″]

zypper ref

[/cc]

It will ask for the key to trust for the new repo, just type a to trust always. We are almost there.

Before installation of phalcon, we need to make sure that these dependencies are installed: mbstring, mcrypt, opensll, pdo/MySQL

If not, no worries install it by using this command:

[cc lang=”bash” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”false” lines=”2″ tab_size=”4″]

zypper in php5-mbstring mcrypt openssl php5-pdo

[/cc]

Time to install phalcon:

[cc lang=”bash” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”false” lines=”2″ tab_size=”4″]

zypper in php5-phalcon

[/cc]

And that’s it, but before jumping to your web dir to start a demo project, restart apache:

[cc lang=”bash” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”false” lines=”2″ tab_size=”4″]

systemctl restart apache2.service

[/cc]

Phalcon documentation are self explanatory and easy to follow. Start here for a test project.

Backup MySQL data into CSV with PHP [Part-3]

In the previous article we discuss how to “Backup MySQL data into CSV” in terminal. In this post we will discuss how to create csv file with PHP and force it to download. We will be pulling data from MySQL database. 1st of all we will need connection to database, for that we will use this script. Change the necessary details and save it as conn.php. Now lets start with the csv.

[cc lang=”php” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”true” lines=”40″ tab_size=”4″]
<?php
require_once ‘conn.php’;

$sql = $dbh->prepare(“SELECT u.name, u.email, u.phone, u.city, s.filename FROM_UNIXTIME(s.sub_time, ‘%M %D %Y – %l:%i %p’) from users u join submissions s on u.uid=s.uid where s.uid != ” and s.filename != ” order by s.sub_time desc”);
$sql->execute();
$row = $sql->rowCount();

if ($row > 0){

$columns = array(‘NAME’,’EMAIL’,’PHONE’,’CITY’,’FILENAME’,’TIME’);

$filep = fopen(‘php://output’, ‘w’);
if ($filep) {
$file = uniqid().’_’.time().’.csv’;
header(“Content-Type: text/csv”);
header(“Content-Disposition: attachment; filename=$file”);
header(“Pragma: no-cache”);
header(“Expires: 0”);
fputcsv($filep, $columns);

while ($rows = $sql->fetch(MYSQL_NUM)) {
fputcsv($filep, $rows);
}
die;
}
}
?>
[/cc]
The script is quite straight forward and simple to use/follow. Little bit explanation: we are joining two tables here and then checking if any record found with rowCount function, if so we proceed to create csv. The array store the headers which will be column headers in the csv file. Giving a unique name to the file we will download and then using PHP fputcsv function to write data, quite easy and painless.

Continuation:

Part-1: Setup web server on Amazon AMI or CentOS

Part-2: Backup MySQL data into CSV

Part-4: Backup and Restore MySQL databases

Part-5: Speed up your website loading time by using PHP APC

Connect to database with PDO in PHP

We can connect to MySQL the easy way or the other way by writing a MySQL wrapper, that’s upto the programmer. In this post, i will not go for any wrapper or class but will share my approach with PDO, which itself is OO.

In the code, i declare a variable this is to switch for local and live server easily, if you don’t want it just remove that piece of code.

[cc lang=”php” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”false” lines=”42″ tab_size=”4″]
<?php
$live = 0 ; // 1=live, 0=local

if($live === 0) :
$hostname = ‘localhost’;
$username = ‘admin’;
$password = ‘passlocal’;
$database = ‘local_db’;

elseif($live === 1) :

$hostname = ‘localhost’;
$username = ‘admin’;
$password = ‘passlive’;
$database = ‘live_db’;
endif;

if (defined(‘PDO::ATTR_DRIVER_NAME’)):
if(extension_loaded (‘PDO’)):
if(extension_loaded (‘pdo_mysql’)):
try {
$dbh = new PDO(“mysql:host=$hostname;dbname=$database”, $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => “SET NAMES utf8”));
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//echo ‘Connected to database<br />’;

}
catch(PDOException $e)
{
echo $e->getMessage();
}
else:
echo “PDO support is available and is loaded, but it seems that pdo mysql extension is not loaded.”;
endif;
else:
echo “PDO support is available, but it seems that it is not loaded.”;
endif;
else:
echo “Sorry, but it seems that PDO support is not available.”;
endif;
?>[/cc]
In the 1st few lines of if statement we check that whether POD driver is available if yes, is it loaded?. By default PHP already load this module and it is installed by default. but this is in case some thing is wrong. You can uncomment the echo line to make sure you are connected with the database.

And that’s it..