Guest User

Untitled

a guest
Nov 21st, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. <?php
  2. /***********************************************************
  3. MYSQL VERSION
  4. ************************************************************/
  5.  
  6. $username="root"; $password=""; $database="exam_codes";
  7. $con = mysql_connect("localhost",$username,$password) or die( "Unable to Connect database");
  8. mysql_select_db($database,$con) or die( "Unable to select database");
  9. // Table Name that you want
  10. // to export in csv
  11. $ShowTable = "blogs";
  12.  
  13. $FileName = "_export.csv";
  14. $file = fopen($FileName,"w");
  15.  
  16. $sql = mysql_query("SELECT * FROM `$ShowTable` LIMIT 11");
  17. $row = mysql_fetch_assoc($sql);
  18. // Save headings alon
  19. $HeadingsArray=array();
  20. foreach($row as $name => $value){
  21. $HeadingsArray[]=$name;
  22. }
  23. fputcsv($file,$HeadingsArray);
  24.  
  25. // Save all records without headings
  26.  
  27. while($row = mysql_fetch_assoc($sql)){
  28. $valuesArray=array();
  29. foreach($row as $name => $value){
  30. $valuesArray[]=$value;
  31. }
  32. fputcsv($file,$valuesArray);
  33. }
  34. fclose($file);
  35.  
  36. header("Location: $FileName");
  37.  
  38. echo "Complete Record saves as CSV in file: <b style=\"color:red;\">$FileName</b>";
  39. ?>
  40. <?php
  41.  
  42. <?php
  43. /***********************************************************
  44. MSSQL VERSION
  45. ************************************************************/
  46.  
  47. // Include the database connections
  48. include '../config/configdb.php';
  49.  
  50. // Get some parameter if you need
  51. $periodo = $_GET['periodo'];
  52.  
  53. // Your query
  54. $sql = "
  55. select distinct c.periodo,v.Nome,v.CPF,c.valor_a_pagar from comissoes_calculadas c
  56. inner join vendedores v on c.idVendedor = v.idVendedor
  57. where c.periodo = '$periodo'
  58. ";
  59.  
  60. // handle the result of the query to a variable
  61. $resultado = sqlsrv_query( $conn, $sql);
  62.  
  63.  
  64. $FileName = "_export.csv";
  65. $file = fopen($FileName,"w");
  66.  
  67. // Save headings alon
  68. $HeadingsArray=array();
  69.  
  70. // handle the results of the query to a variable
  71. $row = sqlsrv_fetch_array($resultado, SQLSRV_FETCH_ASSOC) ;
  72.  
  73. // Get the first row (usually this is the column name of your query)
  74. foreach($row as $name => $value){
  75. $HeadingsArray[]=$name;
  76. }
  77.  
  78. // out put the results to a csv
  79. fputcsv($file,$HeadingsArray,";");
  80.  
  81. // Save all records without headings
  82. while( $row = sqlsrv_fetch_array($resultado, SQLSRV_FETCH_ASSOC) ){
  83.  
  84. // get the lines and do the same as above
  85. $valuesArray=array();
  86.  
  87. foreach($row as $name => $value){
  88. $valuesArray[]=$value;
  89. }
  90.  
  91. fputcsv($file,$valuesArray,";");
  92.  
  93. }
  94. //close the file
  95. fclose($file);
  96.  
  97. // directs the browser to your file, this is when the file is downloaded to the customer machine
  98. header("Location: $FileName");
  99.  
  100. ?>
Add Comment
Please, Sign In to add comment