php-mysql-Export to excel sheet

<?php
$host = ‘localhost’;
$user = ‘root’;
$pass = ”;
$db = ‘testexport’;
$table = ‘testdata’;
$file = ‘export’;

$filename = $file.”_”.date(“Y-m-d”);

$link = mysql_connect($host, $user, $pass) or die(“Can not connect.” . mysql_error());
mysql_select_db($db) or die(“Can not connect.”);

$result = mysql_query(“SHOW COLUMNS FROM “.$table.””);
$i = 0;$csv_output=”;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row[‘Field’].”, “;
$i++;
}
}
$csv_output .= “\n”;

$values = mysql_query(“SELECT * FROM “.$table.””);
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].”, “;
}
$csv_output .= “\n”;
}

$filename = $file.”_”.date(“Y-m-d_H-i”,time());
header(“Content-type: application/vnd.ms-excel”);
header(“Content-disposition: csv” . date(“Y-m-d”) . “.csv”);
header( “Content-disposition: filename=”.$filename.”.csv”);
print $csv_output;
exit;
?>

very simple right šŸ˜‰

How to create a stored procedure in MySQL

Introduction to SQL Stored Procedures

Summary: in this tutorial, you will learn what aĀ stored procedureĀ is, its advantages and disadvantages.

Definition of stored procedure

A stored procedure is a segment of declarative SQL code, which is stored in the database catalog. A stored procedure can be invoked by a program, a trigger or even another stored procedure.

A stored procedure which calls itself is known as recursive stored procedure. Almost RDMBS supports recursive stored procedure but MySQL does not support it very well. You should check your version of MySQL database before implementing recursive stored procedures.

Stored Procedure in MySQL

MySQL is known as the most popular open source RDBMS which is widely used by both community and enterprise. However during the first decade of its existence, it did not support stored procedure,Ā trigger, eventā€¦etc. Since MySQL version 5.0, those features have been added to MySQL database engine to make it become flexible and powerful.

Before starting the tutorial series about stored procedure, you should have MySQL version 5.x+ installed in your computer or server.

Stored Procedures Advantages

  • Stored procedures help increase the performance of application using them. Once created, stored procedures are compiled and stored in the database catalog. A stored procedure typically runs faster than uncompiled SQL statements that are sent from external applications.
  • Stored procedures reduce the traffic between application and database server because instead of sending multiple uncompiled lengthy SQL statements, the application only has to send the stored procedureā€™s name with parameters.
  • Stored procedures are reusable and transparent to any applications. Stored procedures expose the database interface to all applications so developers donā€™t have to develop functions that are already supported in stored procedure in those applications.
  • Stored procedures are secured. Database administrator can grant appropriate permission to application that access stored procedures in database catalog without giving any permission on the underlying database tables.

Besides those advantages, stored procedures have their own disadvantages, which you should be aware of before deciding using store procedures.

Stored Procedures Disadvantages

  • Stored procedures make the database server high load in both memory and processors. Instead of being focused on the storing and retrieving data, you could be asking the database server to perform a number of logical operations which is not well-designed for database server.
  • Stored procedures consist of declarative SQL so it is very difficult to write a procedure with complexity of business logic like other languages in application layer such as Java, C#, C++ā€¦
  • Stored procedures are difficult to debug. Only few database mangement system supports debugging stored procedures. There are some workarounds on this problem but it still not easy enough.
  • Store procedures are not easy to develop and maintain. Developing and maintaining stored procedure are usually required specialized skill set that not all application developers possess. This may introduce problems in both application development and maintain phases.

Stored procedures have their own advantages and disadvantages. When you develop applications, you should decide whether to use stored procedure or not based on the requirements. In the following tutorials, we will show you how to leverage stored procedures in your database programming tasks with a couple of practical examples.

Getting Started with MySQL Stored Procedures

Summary: in this tutorial, you will learn how to write the firstĀ MySQL stored procedureĀ using CREATE PROCEDURE statement. We will show you how to call a store procedure using CALL statement.

Writing the first MySQL stored procedure

We are going to develop a simpleĀ stored procedureĀ namedĀ GetAllProducts()Ā to help you get familiar with syntax. The function of this stored procedure is to retrieve all products from theĀ productsĀ table.

First launch theĀ mysqlĀ client tool and type the following commands:

12

3

4

5

6

DELIMITER //CREATE PROCEDURE GetAllProducts()

BEGIN

SELECT *Ā  FROM products;

END //

DELIMITER ;

mysql-stored-procedure-command-linepic

Letā€™s examine the stored procedure in a greater detail:

  • The first command is DELIMITER //, which is not related to the stored procedure syntax. The DELIMITER statement changes the standard delimiter which is semicolon (;) to another. In this case, the delimiter is changed from the semicolon(;) to double-slashes //. Why do we have to change the delimiter? because we want to pass theĀ  stored procedure to the server as a whole instead of lettingmysqlĀ interprets each statement at a time when we type.Ā  Following the END keyword, we use delimiter // to indicate the end of the stored procedure. The last command (DELIMITER;)Ā changes the delimiter back to the semicolon (;).
  • The CREATE PROCEDURE statement is used to create a new stored procedure. You can specify the name of stored procedure after the CREATE PROCEDURE statement. In this case, the name of the stored procedure isĀ GetAllProducts.Ā Do not forget the parenthesis after the name of the store procedure or you will get an error message.
  • Everything inside a pair of keyword BEGIN and END is called stored procedureā€™s body. You can put the declarative SQL code inside the stored procedureā€™s body to handle business logic. In the store procedure we used simple SQL SELECT statement to query data from theĀ productsĀ table.

Itā€™s kind of tedious to write the store procedure inĀ mysqlĀ client tool especially when the stored procedure is complex. Most of the GUI tool for MySQL allows you to create new stored procedures using intuitive interface. For example, in MySQL Workbench, you can create a new stored procedure as follows:

create-mysql-stored-procedure-mysql-workbench-step-1

 

create-mysql-stored-procedure-mysql-workbench-step-2

 

Enter the stored procedure code and click the Apply button

create-mysql-stored-procedure-mysql-workbench-step-3

You can review the code before MySQL stores it in the database. Click Apply button if everything is good.

create-mysql-stored-procedure-mysql-workbench-step-4

MySQL compiles and puts the stored procedure in the database catalog; click the Finish button.

create-mysql-stored-procedure-mysql-workbench-step-5

You can see a new stored procedure created under Routines of theĀ classicmodelsdatabase

We have created a new stored procedure. Now itā€™s time to learn how to call or invoke it.

Calling stored procedures

In order to invoke a stored procedure, you use the following SQL command:

?

1 CALL STORED_PROCEDURE_NAME()

You use CALL statement to call a stored procedure e.g., to invoke theĀ GetAllProductsĀ stored procedure, you use the following command:

?

1 CALL GetAllProducts();

If you execute the command above, you will get all products in theĀ productsĀ table.

mysql-stored-procedure

 

In this tutorial, you have learned how to write a simple stored procedure using CREATE PROCEDURE statement and invoke it using the CALL statement.

References:Ā http://www.mysqltutorial.org/mysql-stored-procedure-tutorial.aspx

http://net.tutsplus.com/tutorials/an-introduction-to-stored-procedures/

http://www.brainbell.com/tutorials/MySQL/Using_Stored_Procedures.htm

http://mysqlstoredprocedure.com/index.php?option=com_content&view=article&id=60:wrapper-procedure-to-load-specific-data&catid=34:select&Itemid=27