Saturday, 10 September 2016

How to delete duplicate records from a table in MYSQL ?


Table Name : records
Procedure 1 :
Step 1 :
Create new table by removing duplicates
CREATE TABLE uniquerecords AS SELECT * FROM records GROUP BY name HAVING ( COUNT(name)>0 )
Step 2 :
Delete old table
DROP TABLE records
Step 3 :
Rename the New Table to Old Table
RENAME TABLE uniquerecords TO records
Procedure 2 : (IF Ajay is repeated five times, the below query would delete Ajay four times.)
QUERY :

DELETE FROM records USING records, records AS virtualtable WHERE (records.sno>virtualtable.sno) AND (records.name=virtualtable.name)
This would delete all the records except the first one.
Procedure 3 : (IF Ajay is repeated five times, the below query would delete all the five records.)
QUERY :

DELETE FROM records USING records, records AS virtualtable WHERE (records.sno=virtualtable.sno) AND (records.name=virtualtable.name)

How to get first 5 records from a table without using LIMIT in MYSQL ?


Table Name: records
Here, we have 10 records in the table, however, we need to retreive 5 records without using LIMIT keyword.
Query :
 SELECT limitrecordsone.sno,limitrecordsone.name FROM records limitrecordsone WHERE (SELECT COUNT(*) FROM records limitrecordstwo WHERE limitrecordstwo.sno <= limitrecordsone.sno) <=5
OUTPUT:

Get Player Name and Coach Name from a single table in MYSQL ?


Table Name: playercoach
Player Coach Table
Here, the number in the column coach represents the sno of the coach.
Procedure 1:
SELECT player.sno,player.name,(SELECT coach.name FROM playercoach coach WHERE coach.sno=player.coach) AS coachname FROM playercoach player
Procedure 2:
SELECT player.sno,player.name,coach.name AS coachname FROM playercoach player JOIN playercoach coach ON coach.sno=player.coach
Output:
Procedure 1 would be better in the sense of performance.

How to find ID of a new row added to a table or what is the usage of mysql_insert_id() ?


mysql_insert_id() function is useful to get the ID generated in the last query.
<?php
$query=mysql_query("INSERT into testtable VALUES('testvalue')");
$rowid = mysql_inser_id();
?>
$rowid contains the new row id.

What are the different types of Errors in PHP?

Types of error
Basically there are four types of errors in PHP, which are as follows:
  • Parse Error (Syntax Error)
  • Fatal Error
  • Warning Error
  • Notice Error
Please use error_reporting(E_ALL); to get display all types of errors.
1. Parse Errors (syntax errors)
The parse error occurs if there is a syntax mistake in the script; the output is Parse errors. A parse error stops the execution of the script. There are many reasons for the occurrence of parse errors in PHP. The common reasons for parse errors are as follows:
Common reason of syntax errors are:
  • Unclosed quotes
  • Missing or Extra parentheses
  • Unclosed braces 
  • Missing semicolon
Example

<?php
echo "Champion";
echo "Dusk"
echo "Light";
?>
Output:
In the above code we missed the semicolon in the second line. When that happens there will be a parse or syntax error which stops execution of the script, as follows:
Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';' in C:\dev\apache\apache2.2.8\htdocs\testing.php on line 3

2. Fatal Errors
Fatal errors are caused when you're asking PHP to do, that can't be done. Fatal errors stop the execution of the script. If you are trying to access the undefined functions, then the output is a fatal error.
Example
<?php
fun2();
echo "Fatal Error !!";
?>
Output:
In the above code we called function fun2 which is not defined. So a fatal error will be produced that stops the execution of the script. Like as follows:
Fatal error: Call to undefined function fun2() in C:\dev\apache\apache2.2.8\htdocs\testing.php on line 2

3. Warning Errors
Warning errors will not stop execution of the script. The main reason for warning errors are to include a missing file or using the incorrect number of parameters in a function.
Example

<?php 
include ("Welcome.php");
echo "You got Warning Error!!";
?>
Output:
In the above code we include a welcome.php file, however the welcome.php file does not exist in the directory. So there will be a warning error produced but that does not stop the execution of the script i.e. you will see a message Warning Error !!. Like as follows:
Warning: include(Welcome.php) [function.include]: failed to open stream: No such file or directory in C:\dev\apache\apache2.2.8\htdocs\testing.php on line 2

Warning: include() [function.include]: Failed opening 'Welcome.php' for inclusion (include_path='.;C:\php5\pear') in C:\dev\apache\apache2.2.8\htdocs\testing.php on line 2
You got Warning Error!! 

4. Notice Errors
Notice that an error is the same as a warning error i.e. in the notice error execution of the script does not stop. Notice that the error occurs when you try to access the undefined variable, then produce a notice error.
Example

<?php 
echo $test;
echo "You got Notice error!";
?>

Output:

In the above code we tried to print a variable which named $test, which is not defined. So there will be a notice error produced but execution of the script does not stop, you will see a message  "You got Notice error!". Like as follows:
Notice: Undefined variable: test in C:\dev\apache\apache2.2.8\htdocs\testing.php on line 2
You got Notice error! 

Differences between require, require_once, include, include_once?

All these functions are used to include the files in the PHP page, however, there is slight difference between these functions.
Difference between require and include is that if the file you want to include is not found then include function give you warning and executes the remaining code in of php page where you write the include function. While require gives you fatal error if the file you want to include is not found and the remaining code of the php page will not execute.
If you have many functions in the php page then you may use require_once or include_once. There functions only includes the file only once in the php page. If you use include or require then may be you accidentally add two times include file so it is good to use require_once or include_once which will include your file only once in php page, can avoid problems with function redefinitions, Variable value reassignments, etc. Difference between require_once and include_once is same as the difference between require and include.
In general, it would be better to use require(), when, the website needs that file to run. Ex : Site wide configuration files. When it is less critical, such as footers, menus, it would be better to use include(), where the website might not look great if the menu does not get included, however, it would still display the information to people.
Ex for diff between include() and include_once():

Let us create one PHP file and name it as data.php . Here is the content of this file
<?
echo “Hello <br>”; // this will print Hello with one line break
?>
Now let us create one more file and from that we will be including the above data.php file . The name of the file will be inc.php and the code is given below.
<?
include “data.php”; // this will display Hello once
include “data.php”; // this will display Hello once
include “data.php”; // this will display Hello once
include_once “data.php”; // this will not display as the file is already included.
include_once “data.php”; // this will also not display as the file is already included.
?>
So here in the above code the echo command displaying Hello will be displayed three times and not five times. The include_once() command will not include the data.php file again.

What is the difference between echo and print?

The major differences between print and echo are:

1) echo is a language construct while print is a function
2) echo can take multiple parameters while print can't take multiple parameters

3) echo just outputs the contents to screen while print returns true on successful output and false if unable to output. In this sense we usually says that print returns value while echo don't.

4) echo is faster than print in execution because it does not return values.
5) Last but not least, echo has 4 chars where as print has 5 chars.
Ex:
echo $str." test ".$ing;                                                 <---                 Concatenation slows down the process, since, PHP must add strings together.

echo "and a ", 1, 2, 3; echo "<br/>"; echo $test;     <---                  Calling Echo multiple times is not good as using Echo parameters in solo.

echo "and a ", 1, 2, 3,"<br/>",$test;                           <---                 Correct! In a large loop, this could save a couple of seconds in the long run!

$ret = print $testw;