• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

PHP - Dynamic Websites: A Beginner's Guide

worlditsme

New Member
There are many programming languages that can be used to create dynamic websites, one of the easiest ones being PHP, which is what this article will focus on.


PHP is simple to learn because it closely resembles the English language in terms of most of the programming commands that are available within it.


Here is an example script of a page written in the PHP programming language:

<?php
print("Hello World");
?>

The first and third lines of this script are standard within all PHP scripts, these lines being the opening and closing statements of the program, respectively.


The second line is the program itself, which in this example, is a simple print command which, as the name suggests, prints a message that is displayed on screen when the program is run.


One of the most basic things you will need to know in order to start using PHP is how to read data and use it within a program.


The easiest way to get outside variables into a PHP program is to use the following lines of code:

import_request_variables('p','p_'); - This allows POST variables (e.g. when submitting an HTML form)


import_request_variables('g','g_'); - This allows GET variables (e.g. when passing variables to a script directly)

Once you have this part of the program written, there are many other commands that you will need to know in order to start using PHP to its full potential.


The most common of these is the programming commands used to interact with MYSQL databases, as these are one of the most common ones in use on the Internet, which is most likely due to the fact that MYSQL is both open source and free of charge to all users for all purposes.


Some of the MYSQL commands are as follows:

mysql_connect(hostname, username, password); - Opens a connection to a MYSQL Database
mysql_query("THE QUERY YOU WISH TO RUN"); - Runs a MYSQL query.
mysql_close() - Closes a connection to a MYSQL database.

Although this is only intended as a beginners guide, there are many resources located throughout the Internet which go into more detail regarding some of the more advanced coding that is sometimes used when programming in PHP.


Overall, I would say that this is definitely the best programming language out there both in terms of usage and more importantly one of the easiest to actually learn.
 
Yes, of course, I totally agree with you.
It is really very simple and easiest web programming language that i have ever found. Also, this is the language that most of the websites are running in the web world. Combination of PHP and JavaScript will lead to a great website with high security.
Nowadays, there are so many new languages like json, jquery are out there to offer do a big task in few coding. Syncing php with those scripting language will help us to create a great website.
Thanks.
 
PHP:
<?php
//Connect to a database
mysql_pconnect("localhost","test","test");
mysql_select_db("database");

//Define simple variables
$page = $_GET['p'];

if($page == 'example') {
print "Example content 1 here";
} elseif($page == 'example2') {
print "example content 2 here";
} else {
print "Homepage / Non-existing page";
}

//Example mysql querys
//Select - mysql_query("select * from `table`") or die(mysql_error());
//Select specific - mysql_query("select * from `table` where `what`='this'") or die(mysql_error());
//Update - mysql_query("update `table` set `this`='that'") or die(mysql_error());
//Update specific - mysql_query("update `table` set `this`='that' where `what`='yes'") or die(mysql_error());
//Delete - mysql_query("delete * from `table`") or die(mysql_error());
//Delete specific - mysql_query("delete from `table` where `this`='that'") or die(mysql_error());

mysql_close();
?>
There./
 
here is a simple PHP OOP sample for you that i wrote a while back...

PHP:
<?php #database.incl.php

$ibd_db	=	new ibd_database(
				$config['database']['dbhost'], 
				$config['database']['dbname'], 
				$config['database']['dbuser'], 
				$config['database']['dbpass']
			);

class ibd_database {
    
    // Database Host
    private $db_host;
    // Username
    private $db_user;
    // Password
    private $db_pass;
    // Database
    private $db_name;
    
	function __construct ($dbHost, $dbName, $dbUser, $dbPass) {
		
		$this->db_host = $dbHost;
		$this->db_name = $dbName;
		$this->db_user = $dbUser;
		$this->db_pass = $dbPass;
		
	}
	
    // Checks to see if the connection is active
    private $con = false;
    
    
    private function ibd_connect() {
        
        if($this->con) return true;
            $this->myconn = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name) or die('There was a problem connecting to the database');
        
        if(!$this->myconn) return false;
            $this->con = true;
        
        return $this->myconn;
        
    }
    
    /*
     * Disconnect from the database
     *
     */
    public function ibd_close() {
        
        if($this->con) {
            
            if(mysqli_close($this->myconn)) {
                $this->con = false;
                return true;
            } else {
                return false;
            }
            
        }

    }
    
    
    /*
    * Selects information from the database.
    * Required: ibd_column (the columns requested, separated by commas, or use *)
    *           ibd_table (the name of the table)
    * Optional: ibd_where (column = value as a string)
    *           ibd_group (value to group)
    *           ibd_order (column DIRECTION as a string)
    *           ibd_limit (number or comma seperated numbers as a string)
    */
    public function ibd_select($ibd_column, $ibd_table, $ibd_where=null, $ibd_group=null, $ibd_order=null, $ibd_limit=null) {
        
        $parameters = array();
        $results = array();
        
        $q = "SELECT " . $ibd_column . " FROM " . $ibd_table . "";
        
        if($ibd_where != null)
            $q .= " WHERE " . $ibd_where;
        
        if($ibd_group != null)
            $q .= " GROUP BY " . $ibd_group;

        if($ibd_order != null)
            $q .= " ORDER BY " . $ibd_order;

        if($ibd_limit != null)
            $q .= " LIMIT " . $ibd_limit;

        $mysql = $this->ibd_connect();
        $stmt = $mysql->prepare($q) or die('Problem preparing query');
        $stmt->execute();
        
        $meta = $stmt->result_metadata();
        
        while ( $field = $meta->fetch_field() ) {
            $parameters[] = &$row[$field->name];
        }
        
        call_user_func_array(array($stmt, 'bind_result'), $parameters);
        
        while ( $stmt->fetch() ) {
            $x = array();
            foreach( $row as $key => $val ) {
                $x[$key] = $val;
            }
            $results[] = $x;
        }
        
        $this->ibd_close();
        
        return $results;
        
    }
    

	
}

?>

you can build more parts to the object to handle an insert, delete, update etc.

and to utilize the object, you need something like this...

PHP:
$var = $ibd_db->ibd_select("*", "TABLE NAME", "WHERE CLAUS HERE'", '', "ORDER BY HERE");

look at the object notes to better understand what you can do and what you need.
 
Back
Top