Tag Archives: PHP

Notice: A session had already been started – ignoring session_start() in .. on line ..

15 Jan

For an error like this

Notice: A session had already been started – ignoring session_start() in .. on line ..

This happens when you try to start session more than once.

The solution for above problem is

1) in php.ini file set session.autostart to 0

session.auto_start = 0

2) In your code use this line

if (!session_id()) session_start();

instead of

session_start(); 

How to import .sql file in mySQL database?

15 Jan

There are 3 ways to import  .sql database file in mySQL.

1) Import the .sql file from phpMyAdmin

Import -> Browse for the .sql file and click on GO.

If you are lucky your databae will be imported if not look for next 2 methods

2) Run the sql query from command prompt if you have access to your webhost either direct or via SSH

The sql query to import .sql database is

mysql -h host -u databaseusername -p databasename < database.sql

Replace

host with your actual host (in most cases it would be localhost)

databaseusername with the user name assigned to access above database

databasename with your actual database name

database.sql with your actual .sql file

PHP Tutorial

15 Jan

PHP stands for PHP Hypertext Preprocessor.

PHP was developed to be a preprocessor language but it lost it track for good and now it has become the de facto language for dynamic websites.

PHP has given tough time to its rival ASP and now it is on top.

PHP is Object Oriented like C++. Its syntax are something similar to C/Perl/C++ and if you are coming from C/C++/Perl background then you will find it easier to learn.

PHP is server side scripting language and requires a database to show its magic. However it doesn’t stop you using it without database.

I prefer PHP over HTML and you may observe that most of my websites are in PHP (without database)

E.g. SwamiWebDesign, SwamiTextLinks, SAIPL

You must learn at least mySQL (Preferred database for PHP) to learn the full potential of PHP/mySQL combination.

And most importantly PHP is free.

PHP is portable meaning you can write the script and execute it on any web server including Apache, IIS etc.

PHP Syntax

PHP is the strangest language I’ve ever known. None of other languages use same syntax as PHP.

A typical PHP code starts with <?php and ends with ?>

And you need to be careful with the spaces. Any space between < and ? means you see nothing in web browser.

Here is our first PHP script

<?php echo “Namaskar”; ?>

Humm!!! where is the typical “Hello World!!” ? Well its my style so live with it. Namaskar is greeting in Hindi and it is used for evrything from Hi/Hello/Good morning/Afternoon/Night to Bye.

And yes did you recognize ; didn’t I say PHP is similar to C/C++.

And the following would be our full code embedded inside html

<html>

<body>

<?php

echo “Namaskar”;

?>

</body>

</html>

Copy & paste above code and save in a file with .php (e.g. file.php ) extension and put it on your server.

Access this file from your browser (http://www.yourdomain.com/file.php or http://localhost/file.php if you are running on WAMP on your own local server) and you will see your first successful php script.

Say Cheers!!!

PHP Comments

PHP comments are borrowed from C/C++ and support both single line C++ style comments and C style block comments

// This is a comment in PHP

/****

This is a comment

in PHP

******/

PHP Variables

As with every other programming languages PHP rely on variables. Variables are those entities which hold data.

In PHP a variable name starts with $ sign.

E.g.

$thisIsAString = “Namaskar”;

$myLuckyNumber = 12345;

Humm!!! excuse me what about the variable type? How does PHP know which variable is going to store what?

Well PHP is loosely typed script meaning that it doesn’t need to know the type of variable in advance. This is really cool isn’t it? you don’t need to worry about C++ type_cast styles any more.

Simple rules for variable names

1) A variable name must start with a letter or underscore _

2) A variable name must contain only alphanumeric characters and underscores.

E.g. a-z, 0-9, and _

Wow! very simple rule can, be remembered easily.

Strings

You can’t think of any language without strings can you?

Think of perl and you know what I mean.

As explained in my previous tutorial a string can be declared as variable. A string can also be used directly instead of assigning it to a variable and using it.

Lets take an example.

$string1 = “Namaskar”;

$string2=”World”;

$string3 = $string1 . ” ” . $string2 . “!”;

echo $string3;

The echo will print

Namaskar World!

We have seen some of string operations in the above code also.

Concatenate Oprator

. is a concatenate operator used to concatenate two or more strings.

You can find rest of string related functions on PHP language home page http://www.php.net

Include and require functions

Include and require functions are used to include files.

E.g.

<?php include(“leftMenu.php”); ?>

<?php require(“leftMenu.php”); ?>

Difference between include and require

With include the rest of code will execute even included files were not found whereas require will not execute any code if it finds any error.