Monday, October 31, 2011

Convert dates in PHP and MySQL

Today I was working in project and I was confronted by one problem. As I live in Europe, the date's format are not the same in United States even MySQL. US use mm/dd/yyyy format, Europe use dd/mm/yyyy and MySQL use yyyy-mm-dd.


After I researched on PHP and MySQL official sites, I wrote algorithms that allowed me insert correctly data in SQL. 


PHP to MySQL:


SQL:


create schema test;


create tabela xpto(
id int not null auto_increment,
start_date date,
end_date date,
primary key(id)
)engine=innodb;




PHP:
<?php
$start_date = "10/10/2011";
$end_date = "12/10/2011";


$query0 = "select str_to_date('$start_date','%d/%m/%Y')";
$result0 = mysql_query($query0) or die(mysql_error());
$row0 = mysql_fetch_array($result0);


$query1 = "select str_to_date('$end_date','%d/%m/%Y')";
$result1 = mysql_query($query1) or die(mysql_error());
$row1 = mysql_fetch_array($result1);


$query2 = "insert into xpto (start_date, end_date) values ('".$row0[0]."','".$row1[0]."')";
$result2 = mysql_query($query2) or die(mysql_error());
?>


To check out the data inserted, type on mysql terminal:
select * from xpto;


MySQL to PHP:
It is enough select data stored and use two functions to show date format correctly.




PHP:
<?php
mysql_connect("localhost","user","pass") or die (mysql_error());
mysql_select_db("test");


$query0="select start_date from xpto where id=1";
$result0 = mysql_query($query0) or die (mysql_error());
$row0 = mysql_fetch_array($result0,MYSQL_NUM);



$query1="select end_date from xpto where id=1";
$result1 = mysql_query($query1) or die (mysql_error());
$row1 = mysql_fetch_array($result1,MYSQL_NUM);


$start_date=$row0[0];//"2011-10-10"
$end_date=$row1[0];//"2011-10-12"


$d0 = strtotime($start_date);
$d1 = date('d/m/Y',$d0);
echo $d1."<br />";


$d2 = strtotime($end_date);
$d3 = date('d/m/Y',$d2);
echo $d3."<br />";
?>


Happy Halloween! :)

Saturday, October 22, 2011

How to install osCommerce

osCommerce (open source Commerce) is an e-commerce and can be used on web server that has PHP and MySQL installed. You can simply download on this site. After you download, you have to unzip and copy to a folder on web server.

The goal of this post is show you how to install osCommerce. I am installing on a virtual machine that works like a web server (LAMP) to simulate real scenario of installation osCommerce.

If you don't know install or configure a web server on Ubuntu, please read this post or see this video.

If you already installed LAMP, please following the next steps to install:


  1. Go to this site and download "oscommerce-2.3.1.zip";
  2. Unzip "oscommerce-2.3.1.zip";
  3. Copy to web server ;
  4. Step #3

  5. Check out the copy;
  6. Step #4

  7. Modify the permissions on specific folders;
  8. Step #5

    Step #5

  9. After modifying the permissions, will show up  the next screen. Press the button "Continue";
  10. Step #6

  11. It will show up a form where you have fill with right values. But I didn't create a database yet. For that, I went to terminal and typed "mysql -h localhost -u root -p" to login MySQL Server. And then, I created database called "oscommerce" with next command: "create schema oscommerce". Finish! Now you can fill form like you can see in third image.

    Note: I made a mistake to input "192.168.1.199" instead a right IP address which is localhost or 127.0.0.1.

  12. Step #7

    Step #7

    Step #7

  13. I agreed with those values by default. If you want to change, you are comfortable to change.
  14. Step #8

  15. The next form is about your online store. You have to give information like your online store name, owner name, owner email address, administrator username and administrator password.
  16. Step #9

  17. Please read the post-installation notes. It is very important follow the steps to finish the installation.
  18. Step #10

  19. The next image will demonstrate you how to set permissions on step #3 and #4 according the previous image.
  20. Step #11

  21. Then, type in your browser: http://localhost/oscommerce/catalog/admin. This address will drive at administrator page where you have insert administrator username and password.
  22. Step #12

  23. Follow the path Administration Tool -> Tools -> Security Directory Permissions. This page will list permissions that you have change to with command "chmod 777 ..." like you can see in first image. After you changed permissions, press F5 to reload page to see values changed. Finish! You already finished the part of administration. Now let see the online store.
  24. Step #13

    Step #13

  25. Type "http://localhost/oscommerce-2.3.1/catalog/" and press Enter. Voilá! Congratulations! You installed your online store!
  26. Step #14


And now you can do anything with your online store and sell anything. Good luck for your sales!







Friday, October 14, 2011

Gliffy

Today I found out a great, great application to modeling data. I am working to create information system to company where I work. And I need a software where I am able to drawing data model. I went to Google and tried Gliffy. Gliffy is absolutely amazing. Much better than Microsoft Visio, no offense Microsoft.
For who work with Microsoft Visio, always can drawing and exporting diagram in SVG.

Thursday, October 13, 2011

Saturday, October 8, 2011

OpenSUSE

I am so regretted to install OpenSUSE in my 9 years old PC. I wanted to install Compiz but I could not install because there are problems between KDE and Compiz. I wanted install Google Chrome instead update Mozilla Firefox, and also I could not install Google Chrome because of Zypper. What the hell is Zypper? Why can not I simply type 'apt-get install google-chrome'?

I already made a decision. I will install other Linux Distro with GNOME. I just wanted install Compiz, Google Chrome...

I am disappointed with OpenSUSE. I thought was fantastic but is not! However, I will show a picture with system information with OpenSUSE.


Friday, October 7, 2011

SQL - Procedural Programming - TRY ... CATCH

The TRY...CATCH construnct was introduced with SQL Server 2005 for error handling using T-SQL. Statements to be tested for an error are enclosed in a BEGIN TRY ... END TRY block.
A CATCH block immediately follows the TRY block, and error-handling logic is stored here. The following examples shows the basic syntax:

BEGIN TRY
-- code that may produce errors
END TRY
BEGIN CATCH
-- error handling logic
END CATCH

SQL Server evaluates each statement in TRY block sequentially. If a runtime error is encountered, control immediately jumps to the CATCH block, and error information can be retrieved, logged, and displayed to the user.

Thursday, October 6, 2011

Steve Jobs


“Your time is limited, so don’t waste it living someone else’s life,”
Jobs said. “Don’t be trapped by dogma — which is living with the results
of other people’s thinking. Don’t let the noise of others’ opinions
drown out your own inner voice. And most important, have the courage to
follow your heart and intuition. They somehow already know what you
truly want to become. Everything else is secondary.”
R.I.P. Steve Jobs


Tuesday, October 4, 2011

Ubuntu vs OpenSUSE vs Fedora

In past few weeks, I upgraded my 9 years old PC. I bought a Ethernet card, 1GB memory RAM and 160GB Hard Drive IDE/ATA to upgrade my old PC.

My question was what operating system I will install to my 9 years old PC. I was undecided between Ubuntu and OpenSUSE. So I researched more deep, and I found out a site who helped me a lot.


I had two choices: OpenSUSE or Fedora. To my surprise, Ubuntu had 90% of recommendation. So I ignored Ubuntu and I tried OpenSUSE and Fedora with LiveCD. After I experimented both, I decided to install OpenSUSE.

SQL - Procedural Programming - Variable

A variable is a container for a single data value of a particular type. In Transact-SQL (T-SQL), all variables are preceded by an @ symbol.
Local variables are used in T-SQL scripts for a variety of purposes including:

  • Storing values to be tested by control-of-flow statement
  • Acting as counter in a loop
  • Storing the results of an expression
  • Retrieving field values for a single record using a SELECT statement
Variable are also used to pass values into parameters for stored procedures and user-defined functions. When declaring a variable, you must specify its name, datatype and sometimes the length and precision of datatype. The DECLARE statement can be used to declare multiple variables by separating them with commas.

Example

DECLARE @var1 int, @var2 varchar(255);

The preferred way to set the contents of a variable is use the SET statement. It is also possible to use the SELECT statement to set the value of one or more variables.

SET @var1=5;
SELECT @var2='A varchar string';