PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: 2013

29 Dec 2013

Change column datatype in mysql

                       You created a table with column in specific datatype. Sometimes you need to change datatype of column. At the time, you can modify the datatype of field. It is possible in mysql. The following mysql query is used for this:
Mysql Query:
                         Alter table table_name modify column_name datatype(length)


Consider the example:
 table1 is name of this table.

changing column size in mysql at phponwebsites

When you click the structure of table in your database, it is look like this:

Structure of table in mysql

                          Suppose you've to change datatype of column 'Game_name'. On that time you can change the field type. The mysql query is:

                     Alter table table1 modify Game_name char(15)

Now your structure look like this:

change column field in mysql

     Now your Game_name column type is changed from varchar into char.


Changing the multiple column datatype at the time in mysql:


                      You can change multiple column datatype at a time in mysql. The mysql query as follow as:
Mysql Query:
                          Alter table table1
                                    modify No varchar(5),
                                    modify Game_name char(15),
                                    modify Image char(15)

                          Now your output look like this:

change multiple column datatype at a time in mysql

                          Now you will get output column 'No' with varchar, 'Game_name' with char and 'Image' with char in your mysql table.

Datatypes in mysql:

datatypes in mysql

27 Dec 2013

Move columns in mysql table

                       You can change the column name and change the column size in mysql. But can you rearrange the column order in mysql table. Yes, you can rearrange it in mysql. It can be done by 'ALTER', 'CHANGE' and 'AFTER' mysql command. The mysql query for move the column in mysql table is:

                   " ALTER TABLE table_name CHANGE column_name column_name datatype(length) AFTER column_name"

                       You can also done by 'MODIFY' mysql command. The mysql query as follows as:

                   " ALTER TABLE table_name MODIFY column_name datatype(length) AFTER column_name"

Consider the following example: table1 is the name of table.
                   
rearrange columns in mysql

                       Now the column 'name' is moved after column 'img'. The mysql query as follows as:

                   " ALTER TABLE table1 CHANGE name name varchar(30) AFTER img"

Now you'll get output like this:

move column using MODIFY mysql command

The column name 'name' is placed after 'img' in mysql table 'table1'.


Move  multiple columns at the time in mysql:


                             You can move multiple columns at the time in mysql. The column name order id, name ,img in table1 is changed into img, name ,id. The mysql query as follows as:

                         "ALTER TABLE table1 CHANGE id id int(5) AFTER img,
                                         CHANGE name name varchar(30) AFTER img"

Now you'll get output like this:

move multiple columns at a time in mysql using CHANGE command

25 Dec 2013

Changing the column size in mysql

                       You created a table with specific size of column. Sometimes you need to increase the size of column, if the size is not enough. At the time, you can modify the size of field. It is possible in mysql. The following mysql query is used for this:
Mysql Query:
                         Alter table table_name modify column_name datatype(length)


Consider the example:
 table1 is name of this table.

changing column size in mysql at phponwebsites



When you click the structure of table in your database. It is look like this:

srtucture of column in mysql


                          Suppose you've to add more characters to column Game_name. On that time you can change the field size. The mysql query is:
                     Alter table table1 modify Game_name varchar(40)  

Now your structure look like this:

change column size in mysql table

     Now your Game_name column size is changed into 40.


Changing the multiple column size at the time in mysql:


                      You can change multiple column size at a time in mysql. The mysql query as follow as:
Mysql Query:
                          Alter table table1
                                    modify Game_name varchar(50),
                                    modify Image varchar(40)

                          Now your output look like this:

changing multiple column size at a time in Mysql at phponwebsites

                          Now you will get output as Game_name with 50 size and Image with 40 size.

Related Post:

22 Dec 2013

Changing column name in mysql table

                       You created a table with specific column name. Sometimes you need to change the column name. At the time, you can change the field name. It is possible in mysql using by CHANGE mysql command. The following mysql query is used for this:
Mysql Query:
                         Alter table table_name change old_column_name  new_column_name datatype(length)


Consider the example:
 table1 is name of this table.

changing column name in mysql at phponwebsites


                          Suppose you've to change the column name. The mysql query is:
                     Alter table table1 change Game_name  name varchar(40)

Now your table look like this:

change column name using CHANGE mysql command

     Now the column name 'Game_name' is changed into 'name'.


Changing the multiple column name at the time in mysql:


                      You can change multiple column name at a time in mysql. The mysql query as follow as:
Mysql Query:
                          Alter table table1
                                    change No id int(5),
                                    modify Image img varchar(40)

                          Now your output look like this:

change multiple column name at the time in mysql at phponwebsites

                          Now you will get output as No to id and Image to img.

Related Post:

19 Dec 2013

Delete column in mysql table

                       You created a table with number of columns. Sometimes you don't need to specific column. At the time, you can delete the column from mysql table. It is possible in mysql using by DROP mysql command. The following mysql query is used for this:
Mysql Query:
                         Alter table table_name drop column column_name


Consider the example:
 table1 is name of this table.

delete column in mysql table using DROP command


                          Suppose you've to delete the column 'Image'. The mysql query is:
                     Alter table table1 drop column Image

Now your table look like this:

drop column from mysql table

     Now the column name 'Image' is deleted from your mysql table.


Deleting the multiple column at the time in mysql:


                      You can delete multiple column at a time in mysql. The mysql query as follow as:
Mysql Query:
                          Alter table table1
                                    drop column Game_name,
                                    drop column Image

                          Now your output look like this:

Delete multiple column at a time in mysql table

                          Now you will get your mysql table with only column 'No'.

Related Post:

17 Dec 2013

Add column after specific field in mysql

                       When you try to add add new column to your table, by default the column should be added to end of your table. But you can add it wherever you want in a table. It is possible in mysql.
Myql Query:
                     alter table tablename add column columnname datatype(length) AFTER specific field name

Add column after particular field in mysql:

               
                    Consider a example. Table1 is name of table. Normally the table look this this:

add column Mysql at phponwebsites


Now you are going to add column after specific column. The mysql query is:
     alter table Table1 add column Game_Tag varchar(30)  AFTER Game_Name

The output look like this:


add column after specific field in Mysql at phponwebsites


Now you will get column 'Game_Tag' is after 'Game_Name'.


Add column at first in mysql:


              Now you are going to add column before specific field. The mysql query is:
    alter table Table1 add column Game_Tag varchar(30) FIRST

The output look like this:

add column first in Mysql at phponwebsites

Now you'll get output as column 'Game_Tag' is present first of your table.


Add multiple columns after specific field in mysql:


               You can add multiple columns after particular column in mysql. The mysql query is:
     alter table Table1
        add column Game_Tag varchar(30),
        add column count int(10),
        add column category varchar(30)
              AFTER Image
The output look like this:


add multiple columns after specific field in Mysql at phponwebsites

15 Dec 2013

Copy values from one database to another in mysql

                       All of you know, you can copy data within a table and between tables. But can you copy data between two database in mysql. Yes, you can done it in mysql. The following mysql query is used for do this.
Mysql Query:
                         INSERT INTO database2.table_name (column_name)
                              SELECT table_name.column_name FROM table_name


Copy data from one database to another in mysql:


                       Consider following example. You've two databases named with db1 and db2. Each database have a table. Table1 is presented in db1 and Table2 is present in db2.
The Table1 in db1 is look like this:

Mysql at phponwebsites

The Table2 in db2 look like this:

Mysql at phponwebsites

Now we copy data from database db1 to db2. The mysql query is:
                     
                     INSERT INTO db2.Table2(name,image)
                              SELECT Table1.name, Table2.name FROM Table1

Now you will get output as follow as:

copy values from one database to another in Mysql at phponwebsites

                   Now you'll get values in Table2 as like as Table1 in db.

Copy data from one database to another with specific condition in mysql:


                  You can copy only specific values from one database to another in mysql using 'WHERE' condition in mysql. Consider a example: Now we are going to copy named with 'sample1' and 'sample2' from database db1. The mysql query is:

                     INSERT INTO db2.Table2(name,image)
                              SELECT Table1.name, Table2.name FROM Table1
                              WHERE Game_name!='sample3'

Now you'll get output as follow as:

copy values from one database to another with specific condition in Mysql at phponwebsites

Note:
          You can copy the values from one database to another using  this method only if two databases are in same server.

Related Post:

13 Dec 2013

Copy values from one column to another within table in mysql

                      Mostly we had copied only particular values from one table to another. Similarly we had copied only particular values within a table. Now the question is rise. That is, can copy the whole column values to another column in same table? Is it possible in mysql?. Yes it is possible in mysql. We can copy the all values in one column to another within a table.
                     Consider a following example:
                                   Table1 is a table name.
                                               
Mysql at phponwebsites
                   
 Now you need to add another column with named as 'Author'. The mysql query is:
                     Alter table Table add column Author varchar(30)

add column in Mysql at phponwebsites

                     You need to copy the Game_Name column values to Author column. The mysql query as follows as:
                              
                               Update tablename set column2=column1

That means, Update Table1 set Author=Game_Name.

Now You will get following as a output:

copy values from one column to another in same table Mysql at phponwebsites

          
                             Now you will get the output both the Game_name and Author column values are same.


Copy values from one column to another except some values within table:


                           Suppose you have to copy all values in a column except some value to another column within a table. It can be done by following mysql query:

                           Update Table1 set Game_Name=Author where Game_Name!='sample1'

Your output look like this:

copy only selected values from one column to another in Mysql at phponwebsites

Now you'll get all values from one column to another except some value.

Related Post:

11 Dec 2013

Copy values from one table to another in mysql

                       We can easily copy the values within a table. But can we copy the values from one table to another table?. Yes, you can done it in mysql. The mysql query is:

                       Insert into table2 (column_name1, column_name2) 
                               select column_name1, column_name2 from table1

Consider the example:
 Table1 is name of this table.

Mysql at phponwebsites


Table2 is name of this table.


Mysql at phponwebsites

Copy values from one table1 to table2 in mysql:

                         Now you have to add values from 'Table1' to 'Table2'. The following mysql query is used to do it.
                         Insert into Table2 (No,Game_name,Image)
                              select No,Game_Name, Image from Table1

The output of  'Table2' is:

copy values from one table to another in Mysql at phponwebsites

Now you'll get values in 'Table2' which is same as 'Table1'.

Copy values from one table to another except some values in mysql:


                        If you want to some coulmn values, then you will add only required column name only in mysql query. Suppose you need only particular values. Then you use 'where' clause to get your answer. The mysql query as follow as:
                        
                        Insert into Table2 (No,Game_name,Image)
                              select No,Game_Name, Image from Table1  
                                where  Game_Name='sample1' and Game_Name='sample2'

Then the output look like this:


copy specific values from one table to another in Mysql at phponwebsites


Now you will get values only named in sample1 and sample2.

Related Post:

9 Dec 2013

Create mysql database in cpanel

                       1.You have to login to your cpanel to create mysql database.
                       2.Then you need to select mysql databases.

Mysql database and cpanel at phponwebsites

                       3.Type your database name. Then click create database button. Here, my database name is 'new'.

create Mysql database at phponwebsites

Now you'll get successful message as created database successfully as follows as:

create database in cpanel
                   
                          4.Then you have to create new user. Type your user name here.

create Mysql user in database and cpanel at phponwebsites

Then click password generator to get password for your user name in mysql. The pop up box is opened as follows as.


create Mysql user at phponwebsites


Select the checked box and click use password button. Then you the strength password is loaded. Then click create user button.

Mysql database and cpanel at phponwebsites

Now the user name is created.
                   5. Then you need to add your user name to mysql database. Select your user name and database as follows as. then click Add button.

add user to Mysql database and cpanel at phponwebsites

                  6. Now you select all the privileges. Then click Make changes button.

all previleges to mysql user


                     7. Finally you will get a message like this:

Mysql database created in cpanel at phponwebsites

Now you created a mysql database , user in cpanel and add user to mysql database. Then you back to home and click phpmyadmin.


create Mysql database in cpanel


Then you can create table in your mysql database.


6 Dec 2013

Robots.txt

                        Robots.txt is a text file which is used to instruct the search engines for how to crawl and index the pages on your site. The search engines should be obey the robots.txt. The search engine follow the instruction specified by robots.txt file. If you want to some of your pages are not crawl by search engine, then you can protect that pages in robots.txt. Then the search engine don't crawl particular pages specified by your robots.txt file.

Robots.txt at phponwebsites

                       The search engine comes to your robots.txt file before it crawl to your pages. It must be placed in your main directory. Otherwise, it can not able to find robots.txt file in your server.

Syntax:
                User-agent: *
                Allow: /

                where,
                           User-agent represents the search engines.
                           Allow means the robots.txt file allow the all search engines to crawl all of your contents.

Disallow the all crawlers by robots.txt:


                User-agent: *
                Disallow:  /
                               
                           Now, the search engine don't crawl and index your pages.

Disallow the particular folder by robots.txt:


                User-agent: *
                Disallow:  /cgi-bin/
                Disallow:  /images/

                 You need a separate disallow for each folder. Now the search engine don't index your images folder.

Disallow the particular file in a folder by robots.txt:


                User-agent: *
                Disallow:  /sample/example.php

                           Now, the search engine don't index example.php file in sample folder.

Robots meta tags by robots.txt:

                               It control the individual pages present in search result. It should be present within head tag.
For example,
                    <meta name="robots" content="noindex">
                       The above line instructs the search engine to don't show this page in search result. 
                       

Disallow the particular search engine by robots.txt:

              If you want to restrict particular search engines don't index your pages, then you can use robots.txt to restrict it.

Syntax:
              User-agent: Googlebot
              Disallow: / 

                  Now all the search engines index your pages except google. Googlebot is the search engine of google.

See also:
                .htaccess

29 Nov 2013

How to access pages through one page in site using .htaccess

                        You can access all pages from your site through a one page. It can be possible in .htaccess. In this type, you have to specify the other page urls on that one page.

Syntax:
               RewriteRule ^(.*)$ index.php?url=$1 [L]

                    Where, when you try to access any pages in your site, at first your index.php file run. Then it
navigate to where you fixed it to go, it goes to that page. In your index.php should be like this:

                  $url=explode('/',$_GET['url']);
                 if($url[0]=='home.php') {
                   require_once('home.php');
                 }

                        where, you try to access your home.php file, first it comes to index.php and then check whether the home.php page is present or not. It navigates to home.php, if it is present in your site.

You can also hide all your files except home page:

Syntax:
               RewriteRule ^(.*)$ index.php

                  where, whatever you type in your address bar, you should be navigates to your index file.

Related Post:

28 Nov 2013

How to enable short tags in php using .htaccess

                       Mostly you would use ' <?php echo $name; ?> ' tag for display anything you want. You can also display this type of single variable value by <?=$name?>. But sometime your browser doesn't take this tag. On that time, you won't get proper output. So you can be done this by .htaccess.

Syntax:
              php_value short_open_tag 1
                 
                      Now you can use short tag in your site.

It can also done by php.ini file. You've to change in php.ini file. In your php.ini file, find " short_open_tag=off", then change 'off ' to 'on'. Save the changes and restart your WAMP server.


Related Post:

27 Nov 2013

How to redirect to www using .htaccess

                       Type your domain name without www in address bar. Check whether it redirects to your site or give a 301 error message. If you got 301 error message, then you will solve this problem using .htaccess.

Syntax:
              RewriteEngine On
              RewriteCond %(HTTP_HOST) ! ^www.domainname.com$ [NC]
              RewriteRule ^(.*)$ http://www.domainname.com/$1 [L, R=301]

Explanation:
              Where,
                          .htaccess force the all http requests to use either www.domainname.com or domainname.com .
                           HTTP_HOST - represents your domain name.
                           RewriteCond   - check the conditions if you typed domain name is not start with www, it 
redirect to next rewrite rule. 
                           ! ^www.domainname.com$ - means you typed domain name is not start with www.
                           NC - represents not case sensitive.
                           RewriteRule - means it redirect domain name without www to http://www.doaminname.com.
                           ^ - starting
                           .* - one or more characters.
                           $ - ending
                           L - if rule matches, don't process any other rewrite rules below this one.

26 Nov 2013

How to redirect the urls using .htaccess

                        You can redirect the urls from one file name to another. It is possible in .htaccess. It can be done by 'RewriteRule' in .htaccess. The 'RewriteRule' is used to redirect the urls from one path to another. You can make your site urls very simple using RewriteRule method.


Rewrite urls using .htaccess:

                       You can rewrite the urls using .htaccess. When your address bar gets one page, the .htaccess can run another page. The .htaccess rewrite the urls from one page to another. Consider the following example.
Syntax:
                RewriteRule ^ex1.php example.php [L]

Explanation:
                First Rule:
                    when the ex1.php file is going to run, .htaccess file runs the example.php instead of ex1.php.

Hide file extension like php, html using .htaccess:

                             You can hide extensions like .php and .html using .htaccess. Consider the following  example.
                       
Syntax:
                RewriteRule ^home home.php [L]

Explanation:
                    When the browser get home to run, .htaccess runs the home.php instead of home. In this method, you can hide your file extensions like .html, .php .

Remove query string using .htaccess:

                               You can remove query string in address bar using .htaccess. There you can pass values in address bar. Consider the following example.


Syntax:
               RewriteRule ^example/(.*)$ example.php?ex=$1 [L]
Explanation:          
                     it avoids the normal rule.
      ie, example.php?ex=something
                 where,
                           when the browser get example.com/example/something, it considered as example.com/example.php?ex=something. Then you can get this value by $_GET['ex']. In this method, you can make your urls very simple and it is easily remembered by any browser.
                            ^  - starting point
                            .* -  one or more characters
                            $  - end point
                            L  - tells the server to stop trying to rewrite a URL after it has applied that rule.


How to redirect one web page url to another website:


                            Suppose you may have one more sites. If you feel when the users try to access the particular file, it directs to another your website.You can also redirect the urls from one website to another. It can be possible in .htaccess.

Syntax:
                Redirect  302 / site2.php  http://www.example2.com
Explanation:        
                    Where,  302 is the temporary redirect.
                                  When the user access the site2.php file in your current domain, it redirect to example2.com website.    
                         

How to redirect from one site to another:

                             
                                    Suppose you feel when the user access your one site, it redirect to another site. Then it can be done by .htaccess.

Syntax:
              Redirect 302 /   http://www.example2.com

              Now if the user come to your current domain, it will redirect to example2.com.


Related Post:

25 Nov 2013

How to enable or disable the directory lists using .htaccess

                      Your public_html folder should be contain many directories like images, js, styles. Someone may be try to access these files from your site without your permission. If you think, don't allow others to access these files, then you can create .htaccess file for protect your files from them.

Syntax:
               Options -Indexes
                    OR
                IndexIgnore *

           It tells the server to disable the directory listings on your site. Now you can not see your directory lists. If you try to get access it, then you will get forbidden error message.

You can also enable this features to your sire.
Syntax:
               Options +Indexes
           It tells the server to enable the directory listings on your site.


How to ignore the particular file based on its extensions in directory lists using .htaccess:


                        Normally when you access the directory list on your browser, you can get all files presented in that directory. Sometimes, you may be feel, other files will display to user except .php, .css, .js. you can done by .htaccess.

Syntax:
             IndexIgnore .*php .*css .*js

                       Now the user can get access your files except the file with extension php, js and css


How to display the directory lists with other details using .htaccess:


                      By default, the directory lists are displays just with only its name. If you want to know the additional details like its size, description, then you can done by .htaccess.

Syntax:
              Indexoptions +FancyIndexing
                 
                 Now you can get directory lists with additional information such as size, description, last modified date etc..

You can also disable the additional details.

Syntax:
              Indexoptions -FancyIndexing