Category Archives: Programming

Backup Your MySQL Database Using PHP

Here is the script to backup MySql database using php. Cheers!



‹?php  
// Check via PhpMyAdmin to see what character database is using
mysql_query('SET NAMES utf8');
mysql_query('SET CHARACTER SET utf8');
$tables = '*';
//get all of the tables
if($tables == '*')
{
    $tables = array();
    $result = mysql_query('SHOW TABLES');
    while($row = mysql_fetch_row($result)) {
    $tables[] = $row[0];
}
}
else
{
    $tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
foreach($tables as $table)
{
    $result = mysql_query('SELECT * FROM '.$table);
    $num_fields = mysql_num_fields($result);
    $return.= 'DROP TABLE '.$table.';';
    $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
    $return.= "\n\n".$row2[1].";\n\n";
    for ($i = 0; $i < $num_fields; $i++)
    {
        while($row = mysql_fetch_row($result))
        {
            $return.= 'INSERT INTO '.$table.' VALUES('; for($j=0; $j<$num_fields; $j++) { $row[$j] =             addslashes($row[$j]);
            $row[$j] = ereg_replace("\n","\\n",$row[$j]);
            if (isset($row[$j]))
            {
                $return.= '"'.$row[$j].'"' ;
            }
            else
            {
                $return.= '""';
            }
            if ($j<($num_fields-1))
            {
                $return.= ',';
            }
        }  
        $return.= ");\n";
    }
}
$return.="\n\n\n";
}
//save file
$handle = fopen('backups/db-backup-'.date('Y-m-d H:i:s').'-'.(md5(implode(',',$tables))).'.sql','w+'); fwrite($handle,$return); fclose($handle); if($handle)
{
    echo "database is successfully stored in backups directory.";
}
else
{
    echo "database backup unsuccessfuly.";
}


Why to choose symfony

I never gone too much deep over the symfony. However, I experienced it for one of my project. Adhering to an MVC pattern and strict object oriented language Symfony framework allows creating more testable code which can be reused in future and accordingly saves the time.

Choosing Symfony means having a long-term vision of application assets. It also means knowing how to make the day-to-day life of developers much easier.

The advantages of using symfony framework and the answers for the question why to choose symfony are:

1. Empowers Developers :

By taking features that would have previously have taken 100’s of lines of code and compressing them into one simple function call empowers developers to integrate more complex features into their Web Sites.
2. Allow for Quicker development of applications :

This is very relevant for people that need websites created in a very small window .

3. Lower Costs :

Allows programmers to pass cost savings onto the customer, a whole new range of customers generated that wanted a website but previously could not afford the higher development costs.

4.  Symfony tries to bring innovation to PHP:

It was one of the first major frameworks to embrace PHP 5.3, to introduce the usage of a Dependency Injection container, and to use a templating engine for its templates by default, Twig, which is now also adopted by major CMSes like Drupal and eZpublish. Symfony also has some unique features like its gorgeous debug toolbar and its great built-in profiler.

 

A very first program in c++ with the concept of class

include<iostream.h>
using namespace std;

class person
{
private:
char name[20];
int age;

public:
void getdata(void);
void dislpay(void);
};
void person :: getdata(void)
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter age:";
cin>>age;
}
void person :: display(void)
{
cout<<"\nName: "<
cout<<"\nAge: "<
}
int main()
{
person p;

p.getdata();
p.display();

return 0;
}

Difference between object-oriented programming (OOP) and procedure-oriented programming (POP)

Difference between object-oriented programming (OOP) and procedure-oriented programming (POP) :

OOP POP
Emphasis on doing things (procedure). Emphasis on data rather than procedure.
Programs are divided into what are known as functions. Programs are divided into what are known as objects.
Data move openly around the system from function to function. Data is hidden and cannot be access by external functions.
Employs top down approach in the program design. Employs bottom up approach in program design.

Doctrine 2 Single Class Inheritance (SCI)

Single Class Inheritance is an inheritance mapping strategy where all classes of a hierarchy are mapped to a single database table. In order to distinguish which row represents which type in the hierarchy a so-called discriminator column is used.

Things to note:

  • The @InheritanceType, @DiscriminatorColumn and @DiscriminatorMap must be specified on the topmost class that is part of the mapped entity hierarchy.
  • The names of the classes in the discriminator map do not need to be fully qualified if the classes are contained in the same namespace as the entity class on which the discriminator map is applied.

<?php

namespace Entities;

/**
* @Entity
* @Table(name=”pets”)
* @InheritanceType(“SINGLE_TABLE”)
* @DiscriminatorColumn(name=”pet_type”, type=”string”)
* @DiscriminatorMap({“cat” = “Cat”, “dog” = “Dog”})
*/
class Pet
{
/** @Id @Column(type=”integer”) @generatedValue */
private $id;

/** @Column(type=”string”, length=300) */
private $name;

/** @ManyToOne(targetEntity=”User”, inversedBy=”id”) */
private $owner;
}

/** @Entity */
class Dog extends Pet
{
/** @Column(type=”string”, length=50) */
private $kennels;
}

/** @Entity */
class Cat extends Pet
{
/** @Column(type=”string”, length=50) */
private $cattery;
}

/**
* @Entity
* @Table(name=”users”)
*/
class User
{
/** @Id @Column(type=”integer”) @generatedValue */
private $id;

/** @Column(length=255, nullable=false) */
private $name;

/** @OneToMany(targetEntity=”Pet”, mappedBy=”owner”) */
private $pets;
}