Grouping Date with Datetime in MySQL

This is very basic and very useful command for mysql. I need user registration date by date. Firstly, i calculate with PHP, after i think it must be a basic way. I found this mysql command. I hope you’ll enjoy.

SELECT DATE_FORMAT(registerdate, '%Y-%m-%d') AS dd, COUNT(id) as TotalUser FROM UserTable GROUP BY dd;

Unique Email Address in Mysql

I have a big mail database but there are some dublicate email addresses. I need unique email address lists and i follow this way;

Firstly i create temporary table for unique email address

CREATE  TABLE  mail_temp (  `email` varchar( 255  )  NOT  NULL default  ''

) ENGINE  =  MyISAM;

After i add unique email address to that temp table

INSERT INTO mail_temp SELECT DISTINCT(email) FROM mail_table

I dropped my real email table

DROP TABLE mail_table;

After all, i create new mail table and moved the temporary mail table to in.

CREATE TABLE mail_table( `email` varchar( 255 ) NOT NULL default ''
) ENGINE  = MyISAM;

INSERT INTO mail_table SELECT  DISTINCT(email) FROM mail_temp;

DROP TABLE mail_temp;

Select Users Age in MySQL

SELECT count(id) AS toplam,YEAR(tarih_dogum) AS YIL FROM uyeler GROUP BY YEAR(tarih_dogum)

Advertising agencies wants to user profile for advertisements. I need my users age. I found a little solution. I write this SQL statement for select which year my users born and group them.

SELECT count(id) AS total,YEAR(birthday) AS birth_year FROM users GROUP BY YEAR(birthday);
After i copied this results to Excel. I calculate age groups like “18-24 years old” in Excel and send my offer to agencies.

Php 5.2.9 Updated But Mysql Doesnt Work

I blogged Php 5.2.9 Upgrade. Some of my user updated with using this entry. But i miss something in these entry. Firstly, i have to say. If you upgrade php version, you have to upgrade mysql and php-mysql.

To Upgrade Mysql and Php-Mysql tool

yum --enablerepo=remi update mysql*
yum --enablerepo=remi update php-mysql

And there one more thing, he was php4 and upgrade to 5. Firstly we can not upgrade, we install php newly.

Installing Php

yum --enablerepo=remi install php

When you install on old version, there will be somethings to change.  Their extentions directory are different
Old One
extension_dir = /usr/lib/php4

New one (Php 5)
extension_dir = /usr/lib/php/modules

And i restarted services

/etc/init.d/mysqld restart
/etc/init.d/httpd restart

I get error in httpd restart

PHP Warning:  Module ‘mysql’ already loaded in Unknown on line 0
PHP Warning:  Module ‘mysqli’ already loaded in Unknown on line 0

Because mysql is standart module in php5, we dont need load mysql in config file. I change php.ini file (/etc/php.ini) Only i add ; character, it is for comments.

;extension=mysql.so
;extension=mysqli.so

Creating Google Sitemap in PHP

We can create dynamic sitemap with using PHP & Mysql. My example code is easy to apply your system.

<?PHP
header('content-type: text/xml');

$mysql_username = "";
$mysql_password = "";
$mysql_database = "";

$dblink = mysql_connect("localhost", "$mysql_username", "$mysql_password");
if (!$dblink) {
die('ERROR: Can not connect database' . mysql_error());
}
@mysql_select_db("$mysql_database") or die ("ERROR: Cannot select database");

echo('<'.'?xml version="1.0" encoding="UTF-8"'.'?'.'>');
echo('<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">');

$contentQ = mysql_query("SELECT * FROM content_table");
while($contentR = mysql_fetch_array($contentQ))
{
$url = $contentR["url"];
$adding_date = date("Y-m-d",strtotime($contentR["adding_date"]));
echo("<url>
<loc>http://www.example.com/".$url."</loc>
<lastmod>".$adding_date."</lastmod>
</url>
");
}
$content2Q = mysql_query("SELECT * FROM content2_table");
while($content2R = mysql_fetch_array($content2Q))
{
$url = $content2R["url"];
$adding_date = date("Y-m-d",strtotime($content2R["adding_date"]));
echo("<url>
<loc>http://www.example.com/".$url."</loc>
<lastmod>".$adding_date."</lastmod>
</url>
");
}
echo("</urlset>");
?>

Google Sitemap Plugins for Wordpress
http://www.dicontas.co.uk/blog/google-sitemap-utw-tag-wordpress-plugin/
http://www.arnebrachhold.de/redir/sitemap-home/
http://www.karailiev.net/karailievs-sitemap/
http://southcoastwebsites.co.uk/wordpress/

Calculate Last Week, Last Month in Mysql

We need get datas from last week, last month, yesterday and today. We can do it easily in MySQL.

Getting Current Date

SELECT CURDATE();

Result : 2009-06-04

Getting Last Week Datas

SELECT DATE_ADD('2009-06-04', INTERVAL 7 DAY);

Getting Last Month Datas

SELECT DATE_ADD('2009-06-04', 1 MONTH);

Getting Last Year Datas

SELECT DATE_ADD('2009-06-04', INTERVAL 1 YEAR);

And Php example for this calculations, getting last month orders

mysql_query("SELECT * FROM orders WHERE DATE_SUB(CURDATE(),INTERVAL 1 month) <= order_date")); 

For more information of Date and time functions :
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

Change MySQL Password via SSH

login ssh, type mysqladmin. A tip if you can not remember mysql password type

mysql -uadmin -p`cat /etc/psa/.psa.shadow`
mysql> USE mysql;
mysql> SELECT * FROM user;
mysql> SET PASSWORD FOR 'username'@'hostname' = PASSWORD('password_here');
mysql> FLUSH PRIVILEGES;

Converting Unescape Chars in MySQL for SEO

Sometimes, i need to convert datas for search engine optimization (SEO). I use MySQL replace command for this. Just login to phpmyadmin or any tool for run SQL command. Paste command set, change table and field name than run.

UPDATE table SET field = replace(field, 'À','A');
UPDATE table SET field = replace(field, 'Á','A');
UPDATE table SET field = replace(field, 'Â','A');
UPDATE table SET field = replace(field, 'Ã','A');
UPDATE table SET field = replace(field, 'Ä','A');
UPDATE table SET field = replace(field, 'Å','A');
UPDATE table SET field = replace(field, 'Æ','AE');
UPDATE table SET field = replace(field, 'Ç','C');
UPDATE table SET field = replace(field, 'È','E');
UPDATE table SET field = replace(field, 'É','E');
UPDATE table SET field = replace(field, 'Ê','E');
UPDATE table SET field = replace(field, 'Ë','E');
UPDATE table SET field = replace(field, 'Ì','I');
UPDATE table SET field = replace(field, 'Í','I');
UPDATE table SET field = replace(field, 'Î','I');
UPDATE table SET field = replace(field, 'Ï','I');
UPDATE table SET field = replace(field, 'Ò','O');
UPDATE table SET field = replace(field, 'Ó','O');
UPDATE table SET field = replace(field, 'Ô','O');
UPDATE table SET field = replace(field, 'Õ','O');
UPDATE table SET field = replace(field, 'Ö','O');
UPDATE table SET field = replace(field, '×','Z');
UPDATE table SET field = replace(field, 'Ø','O');
UPDATE table SET field = replace(field, 'Ù','U');
UPDATE table SET field = replace(field, 'Ú','U');
UPDATE table SET field = replace(field, 'Û','U');
UPDATE table SET field = replace(field, 'Ü','U');
UPDATE table SET field = replace(field, 'ß','ss');
UPDATE table SET field = replace(field, 'à','a');
UPDATE table SET field = replace(field, 'á','a');
UPDATE table SET field = replace(field, 'â','a');
UPDATE table SET field = replace(field, 'ã','a');
UPDATE table SET field = replace(field, 'ä','a');
UPDATE table SET field = replace(field, 'å','a');
UPDATE table SET field = replace(field, 'æ','ae');
UPDATE table SET field = replace(field, 'ç','c');
UPDATE table SET field = replace(field, 'è','e');
UPDATE table SET field = replace(field, 'é','e');
UPDATE table SET field = replace(field, 'ê','e');
UPDATE table SET field = replace(field, 'ë','e');
UPDATE table SET field = replace(field, 'ì','i');
UPDATE table SET field = replace(field, 'í','i');
UPDATE table SET field = replace(field, 'î','i');
UPDATE table SET field = replace(field, 'ï','i');
UPDATE table SET field = replace(field, 'ñ','n');
UPDATE table SET field = replace(field, 'ü','u');
UPDATE table SET field = replace(field, 'ò','o');
UPDATE table SET field = replace(field, 'ó','o');
UPDATE table SET field = replace(field, 'ô','o');
UPDATE table SET field = replace(field, 'õ','o');
UPDATE table SET field = replace(field, 'ö','o');
UPDATE table SET field = replace(field, 'ø','o');
UPDATE table SET field = replace(field, 'ù','u');
UPDATE table SET field = replace(field, 'ú','u');
UPDATE table SET field = replace(field, 'û','u');
UPDATE table SET field = replace(field, 'ÿ','y');

Forgot WordPress Admin Password

Firstly; you need access to your MySQL Server and maybe access to your wp-config.php file in the root of your site.

  1. Go www.md5-creator.com and type in a password you want to use.Example type “123″ then copy. “123 in MD5 :” ’s result is “202cb962ac59075b964b07152d234b70″
  2. Than open your MySQL database, and click the wp_users table
  3. Change “user_pass” value to copied value. In our example “202cb962ac59075b964b07152d234b70″
  4. Now go to your blog and log in :)
Oyun