This article is about very basic of PHP coding, creating functions and database connection using PHP. Later posts may contain detail about other features.
Target viewers are assumed as familiar with C/JAVA or other similar programming language. So I didn’t bother to clarify basic syntax.
Writing PHP code:
All your PHP content should be inside <? ?> tags. That is the file format should be:
<?
Your content here.
?>
Most of the functions in PHP are similar to C. So if you can code in C, you can code in PHP. There are simple exceptions as:
Variables:
PHP variables has no data type. All are same. A PHP variable starts with $ sign. An example is:
$var1 = "Something";
Above variable contains a string. So you can do any string operation over it, such as, strcat, strlen etc. Have you noticed? The string functions are similar to C.
Now if another 3 variables are like this:
$var2 = 3;
$var3 = 2;
And
$var 4 = $var2 + $var3;
Now $var4 contains 5 by adding above two variables’ contents.
You can even do:
$var4 = $var1 + $var2;
This would provide no error, but no result either as there is a string and a number you are adding.
Print and Handling String:
If you want to print something on your browser you have to write:
echo "String content what you wish to print";
echo 'And something more';
Notice that you can use either double quote (“) or single quote (‘) to define a string.
Now to attach a variable with a string, we use the dot (.).
Example:
echo 'A number is ' . $var2 . ' and is an odd number';
This will print:
A number is 3 and is an odd number
You can also do:
echo $var1 . ' is a string and ' . $var2 .' is a number';
Which will print:
Something is a string and 3 is a number
If you do:
$var5 = $var1 . $var2;
Then both the $var1 and $var2 will be treated as strings and $var5 will contain:
Something3
Writing Functions:
Its very easy. The syntax is:
function function_name(params)
{
Do something useful.
return result (optional).
}
Now some DB connection:
The My SQL database connection and manipulation is done using mysql_ functions.
Such as:
mysql_connect();
mysql_select_db();
mysql_query();
mysql_fetch_row();
A database connection we’ve done earlier in another project is like this:
$host= 'localhost';
$user = 'root';
$auth = 'password';
$db = 'mydb';
$msg = 'Database error occurred. Please inform the administrator.';
mysql_connect($host, $user, $auth);
@mysql_select_db($db) or die($msg);
Running a Query:
An example from previous project:
function db_select_by_id($table_name, $id)
{
$sql = 'SELECT * FROM '.$table_name.' WHERE '.$table_name.'_id = '.$id;
$result = @mysql_query($sql);
if($result)
{
return @mysql_fetch_object($result);
}
else
{
if($debug)
{
echo mysql_error();
}
return NULL;
}
}
So if we call the above function like this:
$result = db_select_by_id('user', 4);
The function will run the the query:
SELECT * FROM user WHERE user_id = 4;
And will return the result as object. So the $result will contain an object with the fetched row elements as object elements. We can access elements of $result as:
echo $result -> id;
echo $result -> name;
etc.
Notice that, here we are fetching only one row. This is because, there should be only one row as we are searching using the primary key (id). If you need multiple rows you can use:
function db_select_all($table_name)
{
$sql = 'SELECT * FROM '.$table_name; $result = @mysql_query($sql);
if($result)
{
return $result;
}
else
{
if($debug)
{
echo mysql_error();
}
return NULL;
}
}
Where we select all the rows from the table. Now if $result has the return value from above function, we can retrieve using following process:
while($row = @mysql_fetch_object($result))
{
echo $row -> id;
echo $row -> name;
}
This will fetch every row and will print them until end.
Have you noticed the @ sing before mysql functions? This is used so that any error message is not printed on the browser. You can remove them now to see error messages during development period.
Now you can make your own functions with adding some restrictions to the query…
A very good resource which helps me every time is: http://php.net
Leave a Reply