PHP Programming 101

October 10th, 2010

How to Export Data From MySql to CSV

Posted by Edi in Codes, PHP

This is a simple code that exports mysql database data to csv (delimated) data.

//a small class for database connectivity
class database {
private $db_handle;
private $user_name;
private $password;
private $data_base;
private $host_name;
private $sql;
private $results;
function __construct($host = “localhost”, $user, $passwd) {
$this->db_handle = mysql_connect($host, $user, $passwd);
}
function dbSelect($db) {
$this->data_base = $db;
if (!mysql_select_db($this->data_base, $this->db_handle)) {
error_log(mysql_error(), 3, “/phplog.err”);
die(”Error connecting to Database”);
}
}
function executeSql($sql_stmt) {
$this->sql = $sql_stmt;
$this->result = mysql_query($this->sql);
}
function returnResults() {
return $this->result;
}
}
//database variables
$host = “localhost”;
$user = “root”;
$passwd = “”;
$db = “bg_db”;
$sql = “SELECT * FROM table ORDER BY id”; // a query to fetch records from database
$dbObject = new database($host, $user, $passwd);
$dbObject->dbSelect($db);
$dbObject->executeSql($sql);
$res = $dbObject->returnResults(); // result reasource
$newFileName = “emp_names.csv”; //file name that you want to create
$fpWrite = fopen($newFileName, “w”); // open file as writable
$nameStr = “”;
$rows = mysql_fetch_assoc($res); // fetching associate records
$sStr = “”;
//first store the fields name as header of csv in $sStr
foreach($rows as $key=>$val) {
$sStr .= $key.”,”;
}
//then store all records
do {
$sStr .= “– —–\n”; //to seprate every record
foreach($rows as $key=>$row) {
$sStr .= $row.”,”;
}
}while($rows = mysql_fetch_assoc($res));
$sStrExp = explode(”– —–”, $sStr);//separate every record
foreach($sStrExp as $val) {
$sStr2 .= rtrim($val, “,”);
}
echo $sStr2;
fwrite($fpWrite, $sStr2); //now write to csv file
fclose($fpWrite);//close file
?>

February 10th, 2009

Select multiple HTML tag

Posted by editor in Codes, Information
Tips to simplify your life

Tips to simplify your life

Want to select multiple items from a list? Then you need to use the select multiple tag in HTML. The action handler then takes care of the form when the items are passed through it. What happens though, is that all of them are passed with the same widget name.

For example:
<select name=var multiple=yes>
The option you select will register with the action handler as var=option1, var=option2, var=option3. This thereby overwrites the previous $var variable’s contents.

The fix is to utilize the array from form element feature of PHP.

Use it like so:
<select name=var[] multiple=yes>
The first item then becomes $var[0], and then $var[1] for the next, and so on.

Hope this <a href=”http://phpprogrammingtips.net/information/looping-statements-doesnt-loop/”>tip</a> helps.

September 29th, 2008

The numeric status code

Posted by Conrad in Codes


Image Source:yourhtmlsource.com

A reply line indicates whether a request was successful. It includes the protocol being used, a numeric status code, and a short description of the status code.

An example would be:

HTTP/1.1 200 OK

We often see numeric status codes in the browser every now and then. For so many reasons. These numbers may look jumbled to us or we simply cannot understand them but it stands for something.

The numeric status codes fall into the following ranges:

100-199
Information messages on the current status of processing.
200-299
Successful request.
300-399
Request cancelled because document or resource has been moved.
400-499
Client error. The request was incomplete, incorrect, or otherwise unresolvable.
500-599
Server error. Request appears valid, but server could not complete it.

Have you observed? The most common status message we get to see is the “404 Not Found” error, it just means that the document you requested does not exist. Two things though, this is either because it really doesn’t exist or because you entered the URL wrong. When a 404 is returned it is usually displayed on the browser screen in whatever default format is used by that browser. The server may also transmit a detailed error report page along with an error message if the resource call was unsuccessful.

August 10th, 2008

HISTORY OF PHP


Image sOurce:www.foundationphp.com
PHP was created by Rasmus Lerdorf in 1994 and it was written in C programming language. He created PHP so that he can trace the people who visited his online CV. To make things easier for him to debug his program, he made it into an open source. Then, on June 8, 1995, Lerdorf combined PHP and his own Form Interpreter because of the growing needs of web pages. It was now called as PHP/FI, but generally known as PHP 2.0. Then, in 1997, Andi Gutmans and Zeev Suraski rebuilt the main parser. Before Andi Gutmans and Zeec Suraski rewrote the PHP’s core, PHP was widely known as “Personal Homepage”.

PHP was widely known as “Personal Homepage” until Andi Gutsman and Zeev Suraski Rebuit the main parser of PHP. And so, the acronym PHP was formally changed to PERL Hypertext Preprocessor. Then PHP 3 was released in 1998.

PHP 4 was released in May 2000 with a new core known as the Zend Engine 1.0. In July 2004, PHP 5 was released with the updated Zend Engine 2.0 and new features. PHP 6 was released in October, 20006.

May 30th, 2008

PHP BASIC CODING (cont…)

(cont….)

V. Operators in PHP

*Arithmetic Operators*

+ addition
- subtraction
* multiplication
/ division
% modulus (division remainder)
++ increment
– decrement

*Comparison Operators*
== equal to
!= not equal
> greater than
= greater than or equal
<= less than or equal

*Logical Operators*
&& and
|| or
! not

I know its self explanatory…

VI. IF….Else Statements

If (condition)
//*Things to be executed if the answer to the condition is true*//
else
//*Things to be executed if the answer to the condition is false*//

If…else statement is use if the user wants to execute a set of code when the condition is true and another if the condition is not true.

VII. Switch Statement

switch (expression)
{
case 1:
//*code to be executed*//
break;
case 2:
//*code to be executed*//
break;
default:
//*code to be executed*//
break;
}

Switch statement is use if you want to select one of many blocks of code to be executed. this statement is used to avoid long blocks of if…elseif..else.

VIII. Looping Statement

while(condition)
code to be executed;

While statement is used to execute a block of code if and as long as a condition is true

do
{
code to be executed;
}while (condition)

Do—while statement is used to execute a block of code at least once and it will repeat the loop as long as the condition is true.

for each (array as value)
{
code to be executed;
}

For each statement is used to loop through arrays.

I think this is already enough and it has added new knowledge to those who are new in PHP… good luck and happy coding…

php_2.jpg

image source: www.cass.rrhosting.com

May 29th, 2008

PHP BASIC CODING (cont…)

(cont…)
III. PHP Variables

$variablename=value;

Variables are used for storing values such as text string or arrays. And all variables in PHP starts with a ($) dollar sign. There are various rules to be follow before naming a variable. 1) A variable name must start with a letter or an underscore. 2) A variable name can only contain alpha-numeric characters and underscores. Special characters are not allowed. 3) A variable name should not contain spaces.

IV.PHP Strings

$wrd=”Hello World”;
print $wrd;

Strings variables are used for values that contain character strings. The only operator that can be used on string is concatenation denoted by the sign (.) period.

$wrd1=”Hello World”;
$wrd2=”Good day”;
print $wrd1.”".$wrd2

You can count the nos. of string with the function strlen().

Print strlen(“HelloWorld and GoodDay”);

to be continued…

php-designer3php-editorscom.jpg

image source: www.php-editors.com

May 23rd, 2008

PHP BASIC CODING

Here are some of the basic things to be in mind while coding your dynamic web pages using PHP.

I. PHP basic syntax

PHP is a script that is place inside a HTML document that’s why you need to put the basic codes of HTML such as and .
PHP is open with the (?)question mark followed by “PHP” and closed with a (?) question mark.

II. Displaying in PHP

In order to display a text in PHP you need to use the keyword ECHO or PRINT. all code line ends with a semicolon.

To be continued…

firefoxss23hqcom.jpg

image source: www.23hq.com