tutorials

php: connecting to a MySQL database Posted by admin at 8:48AM May 19, 2010 tutorials| 1 comments
I figured some new comers to PHP might like a simple tutorial on how to connect to a MySQL database.
So let's get started.

I, obviously, use PHP to run this website. All the information on this website, and on many, many, many others is dynamically shown or displayed by PHP.

Here's the code I use for this page itself to connect to my database.

// Connect to the database server
$con = mysql_connect("localhost", "YOUR USERNAME GOES HERE", "YOUR PASSWORD GOES HERE");
if(!$con) {
die("Could not connect to database: ".mysql_error());
}
// Open to the database
mysql_select_db("YOUR DATABASE GOES HERE") or die(mysql_error());


In the second line of code, "$con = mysql_connect("localhost", "YOUR USERNAME GOES HERE", "YOUR PASSWORD GOES HERE");", we are setting a variable called "con". The "$" symbol assigns a variable. The variable holds the code to connect to the database. If you wanted to, you could change the entire code I wrote above to:

// Connect to the database server
mysql_connect("localhost", "YOUR USERNAME GOES HERE", "YOUR PASSWORD GOES HERE");

// Open to the database
mysql_select_db("YOUR DATABASE GOES HERE") or die(mysql_error());


The reason why I don't do that, is because if something goes wrong, I don't get an error message. The next part of the code : if(!$con) {
die("Could not connect to database: ".mysql_error());
}
Runs the code (inside the variable "con"), and checks if the condition inside the "if" statement is true. In this case, there is an exclamation mark ("!"), which means not true. So if the connection is not true (well, really, if it doesn't work), the code will display an error message and tell you what's wrong.

The last piece of code:

// Open to the database
mysql_select_db("YOUR DATABASE GOES HERE") or die(mysql_error());


Opens a connection to your database, and if it doesn't work, it displays an error message.
Here, you may notice that instead of using an "if" statement, like before, to display an error message, I simply used "or die(...)". You could really do either way. I did this in the example to show that there is more than one way of doing things. If I wanted to, I could have changed to code to connect to the database itself to:

// Open to the database
$database = mysql_select_db("YOUR DATABASE GOES HERE");
if(!$database) {
die(mysql_error())
}



NOTE: when "//" is in front of a line of code, it makes what ever is on that line a "comment", meaning it is not executed. It is generally used to leave a little note to the person who is writing/changing the code.
css: RGB colors and RGBA colors Posted by admin at 5:11AM Mar 04, 2010 tutorials| 1 comments
In CSS you don't just have to use hex colors (i.e. #333333) but you can also use RGB colors (i.e. 255, 255, 255) and RGBA colors (i.e. 255, 255, 255, .9).

RGB stands for Red Green Blue and RGBA stands for Red Green Blue Alpha (basically, opacity, the maximum value of 1.0, or 100%). The maximum value for each color is 255 and the minimum is 0.

RGB and RGBA can replace hex colors in any part of the style sheet.

Example:
background: rgba(255, 78, 40, .89) url('backgroundimage.jpg') fixed no-repeat;

In the example above I just chose random values for RGBA but it will work to output a translucent color.
css: Rounded corners Posted by admin at 9:59AM Feb 14, 2010 tutorials| 0 comments
Rounded corners require CSS3.

An example of the syntax would be:


-webkit-border-top-right-radius:10px;

-webkit-border-bottom-right-radius: 10px;

-webkit-border-bottom-left-radius: 10px;

-moz-border-radius-bottomleft: 10px;

-moz-border-radius-bottomright: 10px;

-moz-border-radius-topright:10px;

The example above gives the object it's assigned to rounded corners (with a 10 pixel radius) on all corners but the top left.



The code -webkit-border-top-right-radius:10px; makes the top right corner have a 10 pixel radius, but ONLY in browsers that use the webkit engine (Safari, Google Chrome, and others).

The code -moz-border-radius-topright:10px; makes the top right corner have a 10 pixel radius, but ONLY in Mozilla browsers (i.e. Firefox).



To receive the best results, use both syntaxes, in order to help compatibility issues. Unfortunately, Mircrosoft's Internet Explorer, a widely used browser, does not support this feature.
css: Drop Shadows Posted by admin at 7:08AM Feb 07, 2010 tutorials| 0 comments
In CSS3 the syntax for drop shadows is

-moz-box-shadow: 0px 0px 10px black;

-webkit-box-shadow: 0px 0px 10px black;

box-shadow: 0px 0px 10px black;


The first two values (in the code above they would be 0px and 0px) determine the X-offset and Y-offset. The third value (in this case 10px) determines the blur radius for the shadow. Last, but not least, the last value determines the color of the shadow.



Keep in mind that the syntax for drop shadows and the syntax for text-shadows are exactly the same. Here's an example of a text-shadow.text-shadow: 2px 1px 5px #333;
Again, the first two values determine the X and Y offsets, the third value determines the blur radius and the last value determines the color of the shadow.
Copyright © Ittai Svidler