Social Icons

Monday, June 24, 2013

Check mysql engine for a specific table

In MySql, there are many storage engines like MyISAM, InnoDB etc. What if you want to check the type of storage engine for a specific table as what storage engine it is using. You can check it as follows:

Run the below query:

SHOW TABLE STATUS WHERE Name='your_table_name'

The above query will give you the storage engine name with some more additional data, your table uses.

Similarly, if you want to check storage engines for all tables in your mysql database at once, then simply run this query:

SHOW TABLE STATUS

It will give you details about the storage engines for all the tables in your mysql database.

Wednesday, June 19, 2013

PHP file upload

In a website, it is a common requirement to upload a file or photo (image). Here, I am providing a solution as how can do it in PHP. It is not advisable to store the file in database directly. Instead, you should store the file in your project folder and in your database, you should store only the path of the file. So, we'll follow the same approach only.

First we design a form containing a file field. HTML code for the form containing a file field is :

<html>
<head>
<title>File Upload Form</title>
</head>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
File: <input type="file" name="myfile" id="myfile"/>
<br/>
<input type="submit" name="submit" id="submit" value="Upload File"/>
</form>
</body>
</html>

Above is just the html code for displaying a file field in the web page. Now, on clicking  of submit button, it will redirect you to upload.php as it is defined in form action. So, we'll write the code for upload.php now.

upload.php

<?php

if ( $_FILES['myfile']['error'] > 0)
{
  echo "Error: ".$_FILES['myfile']['error'];
}
else
{
  if (file_exists("upload/" . $_FILES["myfile"]["name"]))          //upload/file is the path where file                                                                                                                 gets stored
  {
      echo $_FILES["myfile"]["name"] . " already exists. ";
  }
  else
  {
      move_uploaded_file($_FILES["myfile"]["tmp_name"],
      "upload/" . $_FILES["myfile"]["name"]);
      echo "File Stored in: " . "upload/" . $_FILES["myfile"]["name"];
   }}
?>


Thats it. Now, your file upload script is ready. In the above code, "upload/" . $_FILES["myfile"]["name"] is the path where file gets uploaded or stored.  Make sure, you have 'upload' directory in your project folder path. Now as your file gets stored, if you want to save its path in database, then you can run a sql query in your upload.php script as its path after uploading the file. In that case, anytime you can echo the file with the help of the path.

Thursday, June 6, 2013

Find 2nd highest or nth highest salary of employee in sql

It is one of the common questions for developers and also asked in interviews frequently to find 2nd highest salary or nth highest salary of an employee from the employee table. Here, I am providing solution for both.

To find 2nd highest (or maximum) salary of employee, you can write your query like this in MySql:

Select max(salary) as salary 
From employees 
Where salary<(Select max(salary) From employees);

The above sql query will give you the 2nd highest salary of employee. Here, 'employees' is the name of the table in the database.

But, what if you have to find 3rd highest or 4th highest or nth highest salary of employee. If you will follow the above approach then you will have to write the subquery for n times. That doesn't seem good. Here, I am providing a generalized solution to find nth highest salary.  Look at the below sql query:


SELECT DISTINCT(salary) 
FROM employees 
ORDER BY salary DESC LIMIT (n-1) , 1


Here, n is the highest term for which you have to find out highest salary. The above query will give you nth highest salary.

Tuesday, June 4, 2013

Difference between single quote and double quote in PHP

In PHP, if yu have to display or echo something (say a string), then you can do it with the help of single quote and double quote both. But there is a little difference between them as :

A single quoted string doesn't have variables within it interpreted but a double quoted string does. It can be better understood with the following example:

<?php
$a = 'prem';
echo 'My name is $a';
echo "My name is $a";
?>

For the first echo statement having single quote, output will be
My name is $a

For the second echo statement having double quoted string, output will be
My name is prem

So, if you have to define a constant string then better use with single quote and if you have variable too in the string then go with double quotes.

Total Pageviews