Thursday, 28 June 2012

Delete Duplicate Entry from Table Using ( Two Condition )


SELECT * FROM chat;
+----+----------+
| id | name     |
+----+----------+
| 1  | rajneesh |
| 2  | rajneesh |
| 3  | gautam   |
| 4  | rajneesh |
| 5  | gautam   |
| 6  | gautam   |
+----+----------+
 
DELETE A1 FROM chat A1, chat A2 WHERE A1.id > A2.id AND A1.name = A2.name

if you want to keep the row with the lowest id value OR

DELETE A1 FROM chat A1, chat A2 WHERE A1.id < A2.id AND A1.name = A2.name

if you want to keep the row with the highest id value.

Form Submits automatically after 5 seconds


Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<SCRIPT LANGUAGE="JavaScript"><!--
setTimeout('document.test.submit()',5000);
//--></SCRIPT>

</head>

<body>
<form name="test" id="form1" method="post" action="http://www.php.net/search.php">
  <p>
    <input name="pattern" type="text" id="pattern" />
    <input name="show" type="hidden" id="show" value="quickref" />
  </p>
  <p><input type="submit" name="next" value="Next" />
&nbsp;  </p>
</form>
</body>
</html>

Tuesday, 26 June 2012


<?php
 
    $bithdayDate = '1987-01-24';
    $date = new DateTime($bithdayDate);
    $now = new DateTime();
    $interval = $now->diff($date);
    echo $interval->y;

?>

Wednesday, 20 June 2012

how to select only one particular field from model in zend framework

public function ParticularCol($whr,$group)

{

$select = $this->_db->select()

->from(array('tablename'), array('Col1','Col2')) // Required Change

->where($whr)

->group($group);//echo $select;die;

$result = $this->getAdapter()->fetchAll($select);

return $result;

}


Zend Framework SQL Joins Examples

You may have custom of using advanced queries. It often requires writing complex queries if you are working on enterprise, large scale web application(s).
The use of joins can never be ignored.
Zend Framework developers have done tremendous job by providing simple method for implementing joins.
Lets look some examples of different type of joins.
Before discussing joins lets consider we have two tables, “authors” and “books”.
These are associated with author_id.

1. Inner Join
The simplest query will be
$select = $this->_db->select()
                ->from('books',array('col1','col2'…..))
                ->joinInner('authors','books.id=authors.bks_id',array('col1','col3'…))
                ->where('where condition here')
                ->order('column name ASC/DESC');

2. Left Join
$select = $this->_db->select()
                ->from('books',array('col1','col2'…..))
                ->joinLeft('authors','books.id=authors.bks_id',array('col1','col3'…))
                ->where('where condition here')
                ->group('group by column name here')
                ->order('column name ASC/DESC');

3. Right Join
$select = $this->_db->select()
                ->from('books',array('col1','col2'…..))
                ->joinRight('authors','books.id=authors.bks_id',array('col1','col3'…))
                ->where('where condition here')
                ->group('group by column name here')
                ->order('column name ASC/DESC');

4. Full Join
$select = $this->_db->select()
                ->from('books',array('col1','col2'…..))
                ->joinFull('authors','books.id=authors.bks_id',array('col1','col3'…))
                ->where('where condition here')
                ->group('group by column name here')
                ->order('column name ASC/DESC');

5. Cross Join
$select = $this->_db->select()
                ->from('books',array('col1','col2'…..))
                ->joinFull('authors','books.id=authors.bks_id',array('col1','col3'…))
                ->where('where condition here')
                ->group('group by column name here')
                ->order('column name ASC/DESC');

Once you write these queries, you can fetch a single row or multiple rows as

$result = $this->getAdapter()->fetchRow($select);
$result = $this->getAdapter()->fetchAll($select);

The first statement fetch only one row, while the second statement fetch the entire dataset.

Monday, 18 June 2012

Delete duplicate entries/rows/values in mysql table in one query

How can you delete or remove duplicate entries from a mysql table, without having to use PHP logic etc ?

The query would be

CREATE TABLE MyNewTable AS SELECT * FROM MyOldTable WHERE 1 GROUP BY [COLUMN TO remove duplicates BY];
 
This would create a new table with name MyNewTable which will have unique values for the column specified in the query. Now we do not require the MyOldTable, hence can be removed, followed by a query that will rename the new table to the old table..

DROP TABLE MyOldTable;
RENAME TABLE MyNewTable TO MyOldTable;

Difference between InnoDB and MyISAM in MySQL

MyISAM and InnoDB are two most commonly used storage engines of MySQL database. However, MyISAM is the default storage engine chosen by MySQL database, when creating a new table. The major differences between these two storage engines are :
  • InnoDB supports transactions which is not supported by tables which use MyISAM storage engine.
  • InnoDB has row-level locking, relational integrity i.e. supports foreign keys, which is not possible in MyISAM.
  • InnoDB ‘s performance for high volume data cannot be beaten by any other storage engines available.
Tables created in MyISAM are known to have higher speed compared to tables in InnoDB. But since InnoDB supports volume, transactions, integrity it’s always a better option which you are dealing with a larger database. It is worth mentioning that a single database can have tables of different storage engines.

File structure

MyISAM stores each table on disk with three files whose names begin with same as table name. These files have different extensions to differentiate their purpose. A .frm files stores the table format, and a .MYD (MYData) file stores the data of the table. If the table has indexes then these are stored in the .MYI (MYIndex) files.
On the other hand, InnoDB tables and their indexes are stored in the tablespace, which consists of several files. That is why InnoDB tables can be very large and can store large volume of data. The InnoDB storage engine maintains its own buffer pool for caching data and indexes in main memory.

Check the engines being used for existing tables

So these are the main differences between these two engines. You can specify in the query that which engine is to be used while creating the table.
CREATE TABLE test name varchar(30) ENGINE = InnoDB;
Since MyISAM is the default engine assigned when creating a table, so you need not to specify it, if you are planning to use MyISAM. This rule holds good, if you have not changed anything in the configuration. To check the engines of already existing tables, use the following query. It will list all the existing tables which are present in the current database, with their engines specified.
show table status;
If you want to view the status of a particular table, then use the following query,
show table status where Name = ‘tablename’;

Thursday, 7 June 2012

Sending Email with attachment in Zend Framework

Note : You must remember add ( <form enctype="multipart/form-data" > ) in form tag.

<?php
require_once 'Zend/Mail.php' ; // include on header on Controller where you want use

$message='Report of last months log files Please keep in mind Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry"s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.' ;
//echo $message;die;

$subject = 'Subject of Email';

$config = array('auth' => 'login',
                'username' => 'Email Id', // To Be Required rajneeshgautam@hotmail.com
                'password' => 'xxxxxxx'); // To Be Required xxxxxxx

// SMTP must be change according to Mail Server

$transport = new Zend_Mail_Transport_Smtp('smtp.live.com', $config);

$mail = new Zend_Mail();
$mail->setType(Zend_Mime::MULTIPART_RELATED);
$mail->setBodyText($message);
$mail->setBodyHtml($message);
$mail->setFrom('rajneeshgautam@hotmail.com');
$mail->addTo('rajneeshgautam24@gmail.com');
$mail->setSubject($subject);
$fileContents = file_get_contents($_FILES['resume']['tmp_name']); // To Be Required Attachement File name
$at=$mail->createAttachment($fileContents);
$at->filename = $_FILES['resume']['name']; // To Be Required Attachement File name
$mail->send($transport);
$this->_redirect("index/index/msg/suc");
?>