PHP Best Practices for Beginners

PHP is the most widely-used language for programming on the web.

Connect
	
 //=== Create connection    Name file: dbconfig.php ===		
 $server   = "localhost";
 $user     = "root";
 $password = "123";
 $dname    = "php_practice_db";
 $sqlconnection = @mysql_connect($server, $user, $password);
 
 //=== Check connection ===
 mysql_select_db($dname, $sqlconnection) or 
 die ("Unable to connect to the database");





MySQL DB

//=== Create database ===
Database: `php_practice_db`

//=== Create table ===
CREATE TABLE IF NOT EXISTS `members` (
  `id` int(15) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(50) NOT NULL,
  `lastname` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `phone` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;



Insert

 //=== Create connection
 include_once("dbconfig.php");
 
 //=== Capturing data from login form ===
 $firstname = $_POST['firstname'];	
 $lastname  = $_POST['lastname'];
 $email     = $_POST['email'];
 $phone     = $_POST['phone'];

 //=== Insert data in database
 $doSQL = mysql_query("INSERT INTO members 
                     (firstname, lastname,email, phone) VALUES
		     ('$firstname','$lastname','$email','$phone')");
 if($doSQL){ echo "Inserted"; } else { echo "Not inserted"; }

View

 //=== Create connection ===
 include_once("dbconfig.php");
 
 //=== Get data from table ===
 $get_members = mysql_query("SELECT * FROM members");
 while($show_members = mysql_fetch_array($get_members)) {
 	
	echo $show_members['firstname'];
	echo $show_members['lastname'];
	echo $show_members['email'];
	echo $show_members['phone'];
	
	}
	
Edit
	
 include_once("dbconfig.php");
 
 //=== Capturing data from login form ===
 $id        = $_POST['id'];	
 $firstname = $_POST['firstname'];	
 $lastname  = $_POST['lastname'];
 $email     = $_POST['email'];
 $phone     = $_POST['phone'];

 //=== Save updated information in database ===
 $doSQL = mysql_query("UPDATE members SET firstname='$firstname',
 lastname='$lastname', email='$email', phone='$phone' WHERE id = '$id'");

	
Delete

 //=== Create connection ===
 include_once("dbconfig.php");
 
 //=== Get member id ===
 $id=$_GET['id'];

 //=== Delete member from database ===
 $delete = mysql_query("DELETE FROM members WHERE id='$id'");
 
 
 
 
	
	

PHP Basics Practices

We provide you all neccessary didactic tools to help you get 100% hands-on programming and work on industrial standard.
  • Connect to database
  • Insert
  • View
  • Edit
  • Delete
  • Upload file
  • Send email

PHP Mini Projects

Welcome to our series of tutorials covering the absolute basics of programming with PHP. We will guide you through the first steps and some basic techniques and strategies.

We will study best practices in PHP applications' development and online deployment.

Get personal training and assistance

PHP programming support and coaching available 24/7

Get in touch

T: (+250) 788 802 332 | E: support@code.rw