Flipkart
Monday, September 6, 2010
get the Next Auto Increment number in Mysql
$results = mysql_fetch_array($query);
echo $results["Auto_increment"];
Thursday, August 12, 2010
Export Database Data to .XLS in PHP
$DB_Server = "localhost"; //your MySQL Server
$DB_Username = "username"; //your MySQL User Name
$DB_Password = "password"; //your MySQL Password
$DB_DBName = "databasename"; //your MySQL Database Name
$ttype = $DB_TBLName;
$sql = "Select * from $DB_TBLName";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
or die("Couldn't connect to MySQL:
" . mysql_error() . "
" . mysql_errno());
//select database
$Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn't select database:
" . mysql_error(). "
" . mysql_errno());
//execute query
$result = @mysql_query($sql,$Connect)
or die("Couldn't execute query:
" . mysql_error(). "
" . mysql_errno());
while($row = mysql_fetch_row($result))
{
$data = $row;
}
function cleanData(&$str) {
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"'))
$str = '"' . str_replace('"', '""', $str) . '"';
}
# filename for download $filename = "website_data_" . date('Ymd') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
$flag = false;
foreach($data as $row) {
if(!$flag) { # display field/column names as first row echo implode("\t", array_keys($row)) . "\n";
$flag = true;
}
array_walk($row, 'cleanData');
echo implode("\t", array_values($row)) . "\n";
}
exit;
Wednesday, June 2, 2010
Restoring from a Database Backup (with GZip Compression)
The command to restore is very similar to the one for backing up. Firstly, without GZip compression:
- cat db_backup.sql | mysql -u mysqluser -p mysqldatabase
cat db_backup.sql | mysql -u mysqluser -p mysqldatabaseWe use the “cat” command to output the contents of the backup script, and pipe its contents into the mysql program. As you can see, the mysql program takes the same options as the mysqldump one does in section two.
Now if the script was GZip compressed, we can’t just output its contents into mysql, as it will be compressed data instead of a nice SQL script. So we do the following:
- gunzip < db_backup.sql.gz | mysql -u mysqluser -p mysqldatabase
gunzip < db_backup.sql.gz | mysql -u mysqluser -p mysqldatabaseSee, it's very familiar, just switched around a bit.
Friday, April 16, 2010
mysql - Reset Root User Privileges
OR if your using xamp then it will be C:\xampp\mysql\bin\my.ini
Open the file with notepad (or any text editor) and add the following entry under the [mysqld] section:
skip-grant-tables
Then, save the file and restart the MySQL service.
If you have Phpmyadmin then select the mysql databese and open the user table. In that you will find the all mysql users.
Then you need to edit the root row and update all the radio buttons with Y.
If you dont have Phpmyadmin then
mysql > use mysql;
Delete the old root user like
mysql > delete from user where user='root';
Create root user again (WITH RIGHTS!)
mysql > INSERT INTO user (Host, User, Password, Select_priv, Insert_priv,
Update_priv, Delete_priv, Create_priv, Drop_priv, Reload_priv,
Shutdown_priv, Process_priv, File_priv, Grant_priv, References_priv,
Index_priv, Alter_priv) VALUES ('localhost', 'root', PASSWORD('thepass'),
'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y');
OR
you can update the above instead of re creating the user account.
After that remove the skip-grant-tables from the my.ini file and restart the Mysql. thats it.
Monday, March 22, 2010
Backup Mysql Database From Commandline
mysqldump -h localhost -u username -p database_name > backup_db.sql
take a backup in compress mode:
mysqldump -u username -h localhost -p database_name | gzip -9 > backup_db.sql.gz
(OR)
mysqldump -u username -p database_name | gzip -c backup.sql.gz
Now, to explain what’s going on here! The “mysqldump” program is a tool for creating database backups. The parameters being used are:
- “-u” switch means you’re going to specify a username to connect with, which must follow, like “-u mysqluser” above
- “-p” switch means you’re either going to immediately specify the password to use (with no space), or it’ll prompt you for one
- The final parameter used in the example above is the name of the database to backup (of course!)
To place the contents of the output into a file, for back-up purposes, we need to use what’s called a redirection.
Wednesday, July 15, 2009
Mysql : Get Auto increment value
WHERE table_name = 'club_photogallery'
Left Joins
SELECT
FROM
LEFT JOIN
ON Table1.column = Table2.column
Mysql: Exporting and Importing Dat
'''Exporting Data'''
SELECT
FROM
INTO OUTFILE '/tmp/vworks/sample.txt'
'''Importing data'''
LOAD DATA INFILE 'file_name.txt'
INTO TABLE tbl_name (field1, field2...etc)