Wednesday, February 29, 2012

Goodle Doodle: Gioachino Rossini's 220th birthday / Leap Year


Google doodle celebrates leap year

Google's latest doodle celebrates the leap year and Italian composer Gioachino Antonio Rossini's 220th birthday. The doodle showcases four frogs, each closely associated to one of Rossini's best-known comic-opera "The Barber of Seville'. 


Gioachino Rossini, in full Gioachino Antonio Rossini   (born Feb. 29, 1792, Pesaro, Papal States [Italy]—died Nov. 13, 1868, Passy, near Paris, France), Italian composer noted for his operas, particularly his comic operas, of which The Barber of Seville (1816), Cinderella (1817), and Semiramide (1823) are among the best known. Of his later, larger scale dramatic operas, the most widely heard is William Tell (1829).


Google doodles are the decorative changes that are made to the Google logo to celebrate holidays, anniversaries, and the lives of famous artists and scientists. The first doodle was created by Larry Page and Sergy Brin in the year 1998 to mark the celebrations of the Burning Man Festival. Google currently has over 1000 doodles. Google recently revamped its doodle site, which features largely all doodles created by the company. The website also gives a peek into the creative process that goes into creating these doodles. 

Tuesday, February 28, 2012

How to use transactions in MySql?

Today I struggled a few hours just making transactions work.
But first: why should we use transactions? I will give you a short example.
Let's suppose we want to transfer points, money, credit from one user to another.
So we would have two queries:

update user set credits='100' where userId = 1;
update user set credits='0' where userId = 2;

So, in case when first query throws an error all queries run must be `reversed`. So in this case transactions are very useful.
Here is an example I use, it is a very basic one:

SET autocommit=0;
BEGIN;

select * from product where deleted=0 and name='Juice Coke';

insert into product(name, fk_category, fk_packaging, fk_stock, active, decimals, price, has_special_price, special_price_description, added_date, deleted)
values ('aJuice Coke','2','4','1','1','2','100.244','1','DESC SPECIAL PRICE','2012-02-28 08:26:47', 0 );

select * from product where deleted=0 and name='Juice Pepsi';
ROLLBACK;
/*COMMIT;*/

I have some select queries and an insert. Case when insert throws error it should be roll backed all transaction queries. However in these query lines i don't catch errors or so..is just for you to see the idea how it works.
Now, after you catch the idea there might be a problem :) You will not be able to run transactions if your table's engine is MyIsam ! Must be InnoDB !!!
As I found a helpful post I will write here the basics about those two engines and give you the url of the source where you can find more.

MyISAM

Let's start with MyISAM since it is the default engine with MySQL. MyISAM is based on the older but proven ISAM code but has been extended to be fully-featured while retaining the reliability. Data in MyISAM tables is split between three different files on the disk. One for the table format, another for the data, and lastly a third for the indexes.
The maximum number of rows supported amounts to somewhere around ~4.295E+09 and can have up to 64 indexed fields per table. Both of these limits can be greatly increased by compiling a special version of MySQL.
Text/Blob fields are able to be fully-indexed which is of great importance to search functions.
Much more technical information can be found on MySQL's MyISAM Manual Page.

InnoDB

InnoDB is relatively newer so the scene than MyISAM is so people are still weary about its use in environments than run fine under MyISAM. InnoDB is transaction-safe meaning data-integrity is maintained throughout the entire query process. InnoDB also provides row-locking, as opposed to table-locking, meaning while one query is busy updating or inserting a row, another query can update a different row at the same time. These features increase multi-user concurrency and performance.
Another great feature InnoDB boasts is the ability to use foreign-key constraints. FK constraints allows developers to ensure that inserted data referencing another table remains valid. For example, if you had an authors table and a books table and you wanted to insert a new book while referencing the author. The author would have to exist in the authors table before inserting them in the books table because a foreign key was specified in the books table. At the same time you would not be able to delete an author from the authors table if they had any entries associated with them in the books table. More on this in a later article...
Because of its row-locking feature InnoDB is said to thrive in high load environments. Its CPU efficiency is probably not matched by any other disk-based relational database engine.
Here is the link source: http://www.mikebernat.com/blog/MySQL_-_InnoDB_vs_MyISAM
I will give you more details about transactions only if you request :)

How to display current thread in mysql?

This is going to be a very short post. :) If you would like to know how many transactions remained uncommitted(opened) in mysql server then all you have to do is just run this query on your mysql server.

SHOW FULL PROCESSLIST

Short, isn't it? 

Monday, February 27, 2012

How to get an array with properties of an object in Php?

If you ever need to get an array of properties for an object in php this is the method that I created and might help you with that. The key is to use get_object_vars() method, parse each key and memorize in an array.
        
//Returns an array with properties of an object
 function GetAnArrayOfPropertiesForAnObject($Object)
 {
  $arrayProperties = get_object_vars( $Object);
  
  foreach($arrayProperties as $key=>$value)
  {
   $returnArray[] = strtolower($key);
  }
  return $returnArray;
 }

Wednesday, February 22, 2012

Goodle Doodle: Heinrich Rudolf Hertz's birthday marked with Google doodle wave

Heinrich Rudolf Hertz has been honored with a Google doodle marking the 155th anniversary of his birth.

Heinrich Rudolf Hertz (February 22, 1857 – January 1, 1894) was a German physicist who clarified and expanded the electromagnetic theory of light that had been put forth by Maxwell. He was the first to conclusively prove the existence of electromagnetic waves by engineering instruments to transmit and receive radio pulses using experimental procedures that ruled out all other known wireless phenomena.


Hertz was born in Hamburg, then a sovereign state of the German Confederation, into a prosperous and cultured Hanseatic family. His father, Gustav Ferdinand Hertz, was a writer and later a senator. His mother was the former Anna Elisabeth Pfefferkorn. His paternal grandfather David Wolff Hertz (1757–1822), fourth son of Benjamin Wolff Hertz, moved to Hamburg in 1793 where he made his living as a jeweller. He and his wife Schöne Hertz (1760–1834) were buried in the former Jewish cemetery in Ottensen. Their first son Wolff Hertz (1790–1859), was chairman of the Jewish community. His brother Hertz Hertz (1797–1862) was a respected businessman. He was married to Betty Oppenheim, the daughter of the banker Salomon Oppenheim, from Cologne. Hertz converted from Judaism to Christianity and took the name Heinrich David Hertz.

While studying at the Gelehrtenschule des Johanneums in Hamburg, he showed an aptitude for sciences as well as languages, learning Arabic and Sanskrit. He studied sciences and engineering in the German cities of Dresden, Munich and Berlin, where he studied under Gustav R. Kirchhoff and Hermann von Helmholtz.
Part of the source: Wikipidia

Monday, February 20, 2012

How to verify if checkbox was checked in PHP after submit?

Often when you have a form in a html file(might be even php file as you might know) and submit to a php file you want to see if checkbox from your form was checked so you will save in your database accordingly some value.
I have a form like this:

<form name="AddUser" method="post" action="createUser.php">

<fieldset>
<legend>Add a user </legend>
<label for="name">Name</label>
<input type="text" name="name" id="name" value="" />
<br/>

<label for="newsletter">Subscribe to newsletter:</label>
<input type="checkbox" name="newsletter" id=" newsletter " value="" />
<br/>

<input type="submit" name="add_user_btn" value="Create" />
</fieldset>
</form>

As usually the solution is simple, using isset() method:
if(isset($_POST['newsletter']))
 $newsletter= 1;
else
 $newsletter= 0;