Learn PHP : Making a connection to MySQL database

The benefit of choosing PHP as a web platform is its utility in dynamic and database driven web applications. It is fairly easy in PHP script to make a connection to MySQL database.

<?php
$connection =  mysql_connect("DataBase-Host", "DataBase-user","Password-of-databse") OR die('Error connecting to mysql'); //means do not execute code any further.
$dbname = 'databasename';
mysql_select_db($dbname);
?>

There is one more better way to go with above code statement.
Good practice is to make a single file which can be called as and when it is required.
like you can save a file with a name e.g. connection.php with following code in it and can include it in any file to have a Database connection.
Here we will assign values to variables and it can be handy when you start coding more and more. You do not have to type data base connection script code every time just use include function and code will become part of that script.
e.g. <?php include “config.php”;?>
You can edit parameters to your own database values.
Save this file as config.php

<?php
// This is an example of connection.php
$dbhost = 'localhost';
// HOST OF DATABSE generally its localhost
$dbuser = 'root';
// USER OF DATABASE
$dbpass = 'password';
// PASSWORD of the Database User account
$dbname = 'learnphp';
// Name of Databse
$conn = mysql_connect($dbhost, $dbuser, $dbpass)
or die  ('Error connecting to mysql');
mysql_select_db($dbname);
?>

Script Code : PHP Sessions Starting, Setting and Closing

PHP Tutorial [Beginner’s Basic learning] – Sessions in PHP. Starting, Setting and Closing

Session(s) is a temporary storage of values/data to variables to be used for serving web user in a better and interactive way. The data stored in sessions can be either user provided or is being drawn from a stored database at server end.

Starting Php Session

Starting sessions in php is very easy and it is initiated by simple script code as shown here below

<?php
session_start(); // start up your PHP session!
?>

Displaying Values from PHP Session


<?php
session_start(); // start up your PHP session!
$_SESSION['user'] = "Guest"; // store session data
echo "Your Name is = ". $_SESSION['user']; //retrieve data
?>

above code will output the following line
Your name is = Guest.

Setting Session with isset

<?php
session_start();
if(isset($_SESSION['hits']))
$_SESSION['hits'] = $_SESSION['hits']+ 1;
else
$_SESSION['hits'] = 1;
// Now output page hits
echo "hits = ". $_SESSION['hits'];
?>

Closing Session in PHP coding

We have two options. Either we can remove session values with unset function like shown in PHP code given here below:

<?php
session_start();
if(isset($_SESSION['userID']))
unset($_SESSION['userID']);
?>

OR
We can destroy session with session_destroy function as shown here under :

<?php
session_start();
session_destroy();
?>

Website Tips : How to add animate favicon? HTML code

You might have seen some animated image in address/location bar of browser like one is here an animated website favicon (favicon*new window ) a blinking “M” for web domain Mohyals.info (works better in Mozilla FireFox Browser).

First you need to make an animated gif to be used for your website, the add the following code into head tags just below <TITLE> tags.
Here is the code

<link rel=”shortcut icon” href=”favicon.ico”>
<link rel=”icon” href=”animated_favicon1.gif” type=”image/gif”>

and remember to upload animated gif icon to root folder/directory of your website. I hope you know a little bit of FTP thing. If you do not know about FTP then click here to learn about FTP (File Transfer Protocol). Download freeware FTP client the you can try Core FTP or Filezilla FTP client

Do you need to setup or upload your website? Need help? then contact me I will setup our website/blog for a nominal fee.

Good luck! Go ahead and try it yourself (TIY).

Learn PHP : Lesson 1 – Get it Easy

Ok you wanna learn PHP. Who knows you might be running next big thing. What ever! lets start first lesson. Your first code in PHP scripting language. I would advise you to learn a bit of HTML before you jump into PHP coding thing.

Like HTML, PHP also lives in tags known as open and close tags. <? is the open or start tags and ?> is close tags. Every PHP code stats with open tags and ends with close tags. In PHP 5 version coders are more inclined to use <?php open tags rather than <?

There are some really simple script commands in PHP like PRINT, ECHO, SET, GET, POST, FOR, WHILE etc. Just like ordering someone to do a certain job for you. Yeah! just like that. 🙂

Before you begin coding PHP you need to download a few Freeware softwares from Continue reading “Learn PHP : Lesson 1 – Get it Easy”