Wednesday, February 18, 2009

Mysql - Add New MySQL User - PHP MySQL Tutorial

Add New MySQL User - PHP MySQL Tutorial

Add New MySQL User

For adding a new user to MySQL you just need to add a new entry to user table in database mysql. Below is an example of adding new user phpcake with SELECT, INSERT and UPDATE privileges with the password mypass the SQL query is :

mysql> use mysql;
Database changed

mysql> INSERT INTO user (host, user, password, select_priv, insert_priv, update_priv) VALUES ('localhost', 'phpcake', PASSWORD('mypass'), 'Y', 'Y', 'Y');
Query OK, 1 row affected (0.20 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 1 row affected (0.01 sec)

mysql> SELECT host, user, password FROM user WHERE user = 'phpcake';
+-----------+---------+------------------+
| host | user | password |
+-----------+---------+------------------+
| localhost | phpcake | 6f8c114b58f2ce9e |
+-----------+---------+------------------+
1 row in set (0.00 sec)

When adding a new user remember to encrypt the new password using PASSWORD() function provided by MySQL. As you can see in the above example the password mypass is encrypted to 6f8c114b58f2ce9e.

Notice the the FLUSH PRIVILEGES statement. This tells the server to reload the grant tables. If you don't use it then you won't be able to connect to mysql using the new user account (at least until the server is reloaded).

You can also specify other privileges to a new user by setting the values of these columns in user table to 'Y' when executing the INSERT query :

  • Select_priv
  • Insert_priv
  • Update_priv
  • Delete_priv
  • Create_priv
  • Drop_priv
  • Reload_priv
  • Shutdown_priv
  • Process_priv
  • File_priv
  • Grant_priv
  • References_priv
  • Index_priv
  • Alter_priv


Monday, February 9, 2009

Php - File writer class - write files and take its backups so that it can be easily reverted

File Writer is a simple class designed to make writing files, or appending to files, much easier. It also has optional functionality to take backups of files it writes to so that changes can be reverted easily.

Usage: Example


<?PHP
//Include the main class file
include("main.php");

//Create a new file_writer object
$file = new file_writer;

//Write to a new file
$file->write("test.txt", "Test Test Test");

//Append more text to the file
$file->append("test.txt", "\r\nTest Test Test");
?>

main.php

<?PHP
//Should file_writer take backups of files
define("WRITER_BACKUP", TRUE);
 
//Where should file_writer store backups
define("WRITER_RESTORE_DIR", "./restore");
 
//Should file_writer lock files while writing to them
define("WRITER_LOCK_FILES", TRUE);
 
class file_writer
    {
    public $files = array();
    
    function __construct()
        {
        
        }
        
    function write($file, $content)
        {
        $date = date("d-m-Y_his");
        //Copy the original file to the restore dir
        if(file_exists($file))
            {
            if(WRITER_BACKUP)
                {
                copy($file, WRITER_RESTORE_DIR."/".$date."_".$file);
                }
            }
            
        if(!isset($files[$file]))
            {    
            //Make the file handle
            $files[$file] = fopen($file, "w");
            if(WRITER_LOCK_FILES)
                {
                flock($files[$file], LOCK_EX);
                }
            }
            
        //Overwrite the file
        fwrite($files[$file], $content);
        }
        
    function append($file, $content)
        {
        $date = date("d-m-Y_his");
        //Copy the original file to the restore dir
        if(file_exists($file))
            {
            if(WRITER_BACKUP)
                {
                copy($file, WRITER_RESTORE_DIR."/".$date."_".$file);
                }
            }
        
        if(!isset($files[$file]))
            {    
            //Make the file handle
            $files[$file] = fopen($file, "a");
            if(WRITER_LOCK_FILES)
                {
                flock($files[$file], LOCK_EX);
                }
            }
            
        //Overwrite the file
        fwrite($files[$file], $content);
        }
        
    function __destruct()
        {
        foreach($this->files as $file)
            {
            fclose($file);
            }
        if(WRITER_LOCK_FILES)
            {
            foreach($this->files as $file)
                {
                flock($file, LOCK_EX);
                }
            }
        }
    }
 
?>


Wednesday, February 4, 2009

Flex - Source code for creating a rss reader for your blog in flex

Source code for creating a rss reader for your blog in flex.Use this
code to create your rss reader using flex builder or flex plugin

I have used flex plugin with Eclipse IDE 3.2


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
width="600"
height="500"
horizontalAlign="center"
verticalAlign="middle"
backgroundColor="#7F7F7F">

!--<mx:HTTPService showBusyCursor="true" id="RSSfeed"
url="http://wallpaperboss.blogspot.com/feeds/posts/default?alt=rss"
resultFormat="object" /> -->
<mx:HTTPService showBusyCursor="true" id="RSSfeed"
url="http://picsweb.blogspot.com/feeds/posts/default?alt=rss"
resultFormat="object" />

<mx:Panel width="600" height="500" layout="absolute"
horizontalCenter="0" verticalCenter="0" resizeEffect="Resize"
backgroundColor="#7F7F7F" title="Bollywood Blog ">
<mx:DataGrid width="580" height="100" id="entries"
dataProvider="{RSSfeed.lastResult.rss.channel.item}"
click="{body.htmlText+=RSSfeed.lastResult.rss.channel.item[entries.selectedIndex].description}"
horizontalCenter="0"
verticalCenter="-180">
<mx:columns> <mx:DataGridColumn dataField="pubDate"
headerText="Date"/>
<mx:DataGridColumn dataField="title" headerText="Title"/>
</mx:columns>
</mx:DataGrid>

<mx:TextArea width="580" height="320" id="body" verticalCenter="60"
horizontalCenter="0" verticalScrollPolicy="on" >
<mx:filters>
<flash.filters:DropShadowFilter
xmlns:flash.filters="flash.filters.*"
blurX="5" blurY="5"
distance="3" angle="45" color="0x000000" alpha="0.4" />
</mx:filters>
</mx:TextArea>

<mx:Button toolTip="Click to load recent posts" label="Check"
click="RSSfeed.send()" verticalCenter="-113" horizontalCenter="0">
<mx:filters>
<flash.filters:DropShadowFilter
xmlns:flash.filters="flash.filters.*"
distance="3" angle="90" color="0x000000"
alpha="0.4" />
</mx:filters>
</mx:Button>
<mx:Label x="9" y="140" htmlText="<a
href="#">http://picsweb.blogspot.com</a>" fontWeight="bold"/>

</mx:Panel>
</mx:Application>