Posted by Matteo Mattei / 30th July 2011
The following PHP code is intendend to be used to export a MySQL table in CSV format in order to be used with MS Excel.
$link = mysql_connect($mysql_host,$mysql_user,$mysql_pass) or die('Could not connect: '.mysql_error());
mysql_select_db($mysql_db,$link) or die('Could not select database: '.$mysql_db);
$query = "SELECT * FROM $tablename ORDER BY id";
$result = mysql_query($query) or die("Error executing query: ".mysql_error());
$row = mysql_fetch_assoc($result);
$line = "";
$comma = "";
foreach($row as $name => $value)
{
$line .= $comma . '"' . str_replace('"', '""', $name) . '"';
$comma = ";";
}
$line .= "\n";
$out = $line;
mysql_data_seek($result, 0);
while($row = mysql_fetch_assoc($result))
{
$line = "";
$comma = "";
foreach($row as $value)
{
$line .= $comma . '"' . str_replace('"', '""', $value) . '"';
$comma = ";";
}
$line .= "\n";
$out.=$line;
}
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=listino.csv");
echo $out;
exit;
Posted by Matteo Mattei / 28th December 2010

Today question is: did you ever happened to make an hot backup of an LDAP database? I will show you how to do it creating an LDIF file and then doing the respective restore (in the hope you will not really need it). After all it’s quite simple…
BACKUP:
ldapsearch -x -b "dc=example,dc=com" -h 192.168.0.1 -D "cn=manager,dc=example,dc=com" -w secret_password "(objectclass=*)" > backup_file.ldif
RESTORE: (the new database must be empty!)
ldapadd -D "cn=manager,dc=example,dc=com" -x -w secret_password -h 192.168.0.1 -f backup_file.ldif
Here there is the meaning of the parameters used:
- -x specify that you want to use the “sample authentication” (rather than SASL)
- -b “dc=example,dc=com” indicate the BaseDN of the server, thus the position where we want to copy all nodes and entries
- -h 192.168.0.1 is the address of the remote LDAP server
- -D “cn=manager,dc=example,dc=com” specify the LDAP user that will bind to the remote server
- -w secret let you to specify the password for the user you have previously choosed
- “(objectclass=*)” specify all entries in the database
- backup_file.ldif is the file, in LDIF format, where the backup will be executed