domingo, 31 de enero de 2016

Export MySQL table to CSV using PHP


Lets write a simple php program to export data from MySql table to CSV file. 

PHP Code

<?php

// Database Connection

$host="localhost";
$uname="root";
$pass="";
$database = "a2zwebhelp"; 

$connection=mysql_connect($host,$uname,$pass); 

echo mysql_error();

//or die("Database Connection Failed");
$selectdb=mysql_select_db($database) or 
die("Database could not be selected"); 
$result=mysql_select_db($database)
or die("database cannot be selected <br>");

// Fetch Record from Database

$output = "";
$table = ""; // Enter Your Table Name 
$sql = mysql_query("select * from $table");
$columns_total = mysql_num_fields($sql);

// Get The Field Name

for ($i = 0; $i < $columns_total; $i++) {
$heading = mysql_field_name($sql, $i);
$output .= '"'.$heading.'",';
}
$output .="\n";

// Get Records from the table

while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$row["$i"].'",';
}
$output .="\n";
}

// Download the file

$filename = "myFile.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);

echo $output;
exit;

?>
http://www.a2zwebhelp.com/export-data-to-csv

sábado, 30 de enero de 2016

using default values on function Passing arguments by reference and return values

<?php
function alert($type=5) {

if ($type==1){

return "num is equal to 1  ";

}


else  {


return  " Bigger than 1";


}
}

echo alert(1);


?>


Passing arguments by reference ¶




To have an argument to a function always passed by reference, prepend an ampersand (&) to the argument name in the function definition:
<?php
function alert(&$type=5) {


$type +=6;
if ($type==1){


return "type is equal to 1  ";

}


else  {


return  " $type Bigger than 1";


}
}

echo alert();

jueves, 21 de enero de 2016

exporting to csv

<?php

$list = array (
    array('Name', 'LastName', 'Phone', 'State'),
    array('Chris', 'Lopez', '789-344-4543','NY'),

);

$fp = fopen('/var/www/csv/file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
echo  "Ok";
?>


chmod  -R 777 /var/www/csv/file.csv

viernes, 8 de enero de 2016

IP validator

<?php

/* see current location
echo getcwd();
*/
$hacker_IP=$_SERVER['REMOTE_ADDR'];

$auth_IP = file_get_contents('http://65.181.118.232/ip.php');

$allowed_IP=array($auth_IP);


function  username_check($hacker_IP,$allowed_IP) {


if(!in_array($hacker_IP,$allowed_IP)){

$body="Unauthorized login attempt   with the IP $hacker_IP";

$headers = 'From: Asterisk Dominicana <ambiorixg12@asterisk-dominicana.com>' . "\r\n";

$headers  .= 'MIME-Version: 1.0' . "\r\n";

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

mail("ambiorixg12@hotmail.com"," Joomla Alert",$body,$headers);

echo "I'm Sorry $hacker_IP  you are not allowed to be here";
exit();

}


}


username_check($hacker_IP,$allowed_IP);
?>