Sunday, October 23, 2016

Working with PHP and MySQL

Working with PHP and MySQL  


With PHP, you can connect to and manipulate databases.
MySQL is the most popular database system used with PHP.

What is MySQL?

  • MySQL is a database system used on the web
  • MySQL is a database system that runs on a server
  • MySQL uses standard SQL
  • MySQL compiles on a number of platforms
  • MySQL is free to download and use
The data in a MySQL database are stored in tables. A table is a collection of related data, and it consists of columns and rows.


if you already Installed WAMP Server MySQL is already Installed and view through PhpMyadmin is a free, open source platform used to administer MySQL with a web browser; 
test PHPMyadmin click this link http://localhost/phpmyadmin/

Creating Database
  1. Create database in PHPMyadmin interface 
  1. 2.Create database in PHPMyadmin SQL Query


                    you can create databse with SQL Query 

                    CREATE DATABASE `user` ;






       3.Create Databse in PHP Script

before you create you need to connect to MySQL with user name and password 


Connecting to Database and Creating Database

<?php
$servername = "localhost";
$username = "username"; // user name 
$password = "password"; // your password

// Create connection$conn = mysqli_connect($servername, $username, $password);
// Check connectionif (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Create database$sql = "CREATE DATABASE myEmployee";
if (mysqli_query($conn, $sql)) {
    echo "Database created successfully";
else {
    echo "Error creating database: " . mysqli_error($conn);
}

mysqli_close($conn);
?>

Creating table in side myEmployee database  
we need to select the carrasponding databse and create table use the following code 
this code create employee Table with 4 field (first name,second name,email, date )


<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myEmployee";

// Create connection$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connectionif (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// sql to create table$sql = "CREATE TABLE employee (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)"
;

if (mysqli_query($conn, $sql)) {
    echo "Table MyGuests created successfully";
else {
    echo "Error creating table: " . mysqli_error($conn);
}

mysqli_close($conn);
?>

Table Employee created 

Insert In to table 

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myEmployee";
// Create connection$conn = new mysqli($servername, $username, $password, $dbname);
// Check connectionif ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')"
;

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

fetch Data fro data base and out put in PHP page 

fetch the data from employee table and out put in PHP page


<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myEmployee";

// Create connection$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connectionif (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id, firstname, lastname FROM employee";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row    while($row = mysqli_fetch_assoc($result)) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
else {
    echo "0 results";
}

mysqli_close($conn);
?>
Result 

Other Operations

DELETE

// sql to delete a record$sql = "DELETE FROM MyGuests WHERE id=3";

if ($conn->query($sql) === TRUE) {
    echo "Record deleted successfully";
else {
    echo "Error deleting record: " . $conn->error;
}

UPDATE

$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
else {
    echo "Error updating record: " . $conn->error;
}

LIMIT 

$sql = "SELECT * FROM Orders LIMIT 30";

Want to become a PHP developer ?

Want to become a PHP developer ?
Here are the following skills required to be a successful PHP developer.
1. Understanding of Web Applications/Services
PHP is primarily used to develop web applications and web services. So it is very much essential for a PHP developer to understand the scope, need and functioning of Web Applications in IT sector.
more about PHP
2. Client-side Scripting
PHP developer must have a good understanding of various client side Scripting languages like HTML, CSS, JavaScript etc. Because it may require to integrate PHP with these languages to build a powerful web applications.(should be aware about latest Client-Side Programming Languages & Frameworks and Latest trends - AngularJS,Node.js,JQuery,Bootstrap,AJAX,HTML5 etc) 
3. Database Concepts
As a PHP developer one should know the basics of database and the methodologies to integrate PHP with different database management tools like MySQL, Oracle, Postgresql, MongoDB etc.(should be aware about SQL Basics, MySQL,PHPMyadmin)
more about SQL
4. Frameworks Understanding
Basicaly  PHP Framework is a collection of classes which help you develop a web application,There are many PHP frameworks out in the market. PHP developer must have a good grip on atleast one framework. Some of the frameworks are Laravel, CakePHP, Zend, CodeIgniter, FatFree,Yii etc.
more abou Framework
5. Understanding of Architecture Pattern
Development will be easy and effective if any of the Architecture pattern is followed in the programming. As a PHP developer, one should have a better understanding of any of the Architecture pattern. MVC, MVVM etc. are few examples.
6. Content Management System
A content management system (CMS) is a software application or set of related programs that are used to create and manage digital content.PHP CMS like wordpress, joomla, drupal, etc.must have a good grip on at least one 
7. OOPS Concepts
PHP follows many OOPS Concepts so as a PHP developer one should know the OOPS Concepts and how to use them
8. Basic Knowledge of Linux, Apache and other FOSS tools and technologies
PHP can work in windows platform too with any other web server but majority of the development and hosting is done on Linux platforms with Apache server running on it. so a PHP developer should have a better understanding of that.
9.E-commerce CMS 
should be aware about E-commerce cms such as magento, prestashop, opencart, etc.(atleast one)
10. Other Things
should be aware about Payment gateway , Php Mailer , Version Controlling SVN ,FTP , Hosting , Graphic Editing , Debugging, Testing 
mut be aware about the following eb sites 
But above all I must say that the key thing required is the Passion to learn.
All the best.


Get started with PHP Programming

Before you can write and test your PHP scripts, there's one thing you'll need - a server that Support PHP ! Fortunately, you don't need to go out and buy one. In fact, you either have to get some web space with a hosting company that supports PHP, or make your computer pretend that it has a server installed.


Thursday, May 24, 2012

Installing and Working With Joomla

Installing and Working With Joomla

What is Joomla

Joomla is a content management system (CMS), which enables you to build Web sites and powerful online applications. Many aspects, including its ease-of-use and extensibility, have made Joomla the most popular Web site software available. Best of all, Joomla is an open source solution that is freely available to everyone.

Joomla is written in PHP, uses object-oriented programming (OOP) techniques  and software design patterns,stores data in a MySQL and includes features such as page caching, RSS feeds, printable versions of pages, news flashes, blogs, polls, search, and support for language internationalization.

http://www.joomla.org/about-joomla.html
http://en.wikipedia.org/wiki/Joomla 

What's a content management system (CMS)?

A content management system is software that keeps track of every piece of content on your Web site, much like your local public library keeps track of books and stores them. Content can be simple text, photos, music, video, documents, or just about anything you can think of. A major advantage of using a CMS is that it requires almost no technical skill or knowledge to manage. Since the CMS manages all your content, you don't have to.

What are some real world examples of what Joomla! can do?


Joomla is used all over the world to power Web sites of all shapes and sizes. For example:
  • Corporate Web sites or portals
  • Corporate intranets and extranets
  • Online magazines, newspapers, and publications
  • E-commerce and online reservations
  • Government applications
  • Small business Web sites
  • Non-profit and organizational Web sites
  • Community-based portals
  • School and church Web sites
  • Personal or family homepages

Download Joomla


Installing Joomla 

here we are installing Joomla in App Server ( link to how to Install Joomla )


all you need is a Web server with PHP and MySQL. Most commercial hosts provide these as part of their basic package of services. Then download a copy of Joomla.  The Joomla Installation Manual (Download PDF) takes you through the installation process step by step, The Joomla Quick Start Guide (Download PDF) should get you running quickly, and the accompanying video is a nice resource.


Minimum System Requirements

To successfully install and use Joomla! you must have a fully operational Web server (Apache is the
optimum), a database (MySQL is the optimum) and the server side scripting language PHP together with specific modules that are activated within PHP for MySQL, XML

To Be Continued...



Wednesday, May 2, 2012

Sending E-mail with PHP (basic mail() & Gmail with phpmailer )


Basic PHP mail()

Send a simple email:

<?php
// the message
$msg = "First line of text\nSecond line of text";
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
// send email
mail("someone@example.com","My subject",$msg);
?>
Definition and Usage
The mail() function allows you to send emails directly from a script.
Syntax
mail(to,subject,message,headers,parameters);

Basic PHP mail() Function code to send emails from a form

Below is the code for the basic email function. We can take the script and actually use a form on our website to set the variables in the script above to send an email.

<?php
//if "email" variable is filled out, send email
  if (isset($_REQUEST['email']))  {
  
  //Email information
  $admin_email = "someone@example.com";
  $email = $_REQUEST['email'];
  $subject = $_REQUEST['subject'];
  $comment = $_REQUEST['comment'];
  
  //send email
  mail($email, "$subject", $comment, "From:" . $email);
  
  //Email response
  echo "Thank you for contacting us!";
  }
  
  //if "email" variable is not filled out, display the form
  else  {
?>

 <form method="post">
  Email: <input name="email" type="text" /><br />
  Subject: <input name="subject" type="text" /><br />
  Message:<br />
  <textarea name="comment" rows="15" cols="40"></textarea><br />
  <input type="submit" value="Submit" />
  </form>
  
<?php
  }
?>

So let’s now review what the form is actually doing. 
 1. The first part checks to make sure the email input field is filled out. If it is not, then it will display the HTML form on the page. If the email is in fact, set (after the visitor fills out the form), it is ready to send.  
2. When the submit button is pressed, after the form is filled out, the page reloads and reads that the email input is set, so it sends the email.
3. if you get following error,  Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25,
then make sure your php.ini configured correctly you have to configure these values on php.ini file
SMTP = smtp.yourdomain.com  
smtp_port = 25 
username = info@yourdomain.com
password = yourmailpassord
sendmail_from = info@yourdomain.com

Sending E-mail with PHP (Gmail SMTP and phpmailer)

 tutorial for sending PHP Mail using Gmail as your SMTP engine (with help of phpmailer functions.

before you start you must have valid gmail account and web server capable of PHP
  • PHP (you know, PHP: Hypertext Preprocessor). Its a lovely language for developing web pages. If you already knew that, then good. If not, you might want to read a good PHP Tutorial.
  • A local apache+PHP webserver running to test with. I am using  AppServ -http://www.appservnetwork.com/  while on Windows 7.
  • Can edit your PHP.ini configuration file. (If you don’t know what I’m talking about, get really familiar with the above two.
-----------------------------------------------------------------------------------------------------------

  1. If you haven’t done so already, get a PHP server running.
  2. Download PHP-Mailer .
  3. Go to your Apache  web server directory C:\AppServ\www\PHP_MAIL folder (in AppSever), and extract the PHP Mailer into it. I’ll call this folder Root from now on.(it contain - 3 PHP files- class.phpmailer.php , class.pop3.php , class.smtp.php , include   class.phpmailer.php in your test_gmail.php file ,source code is given below)
  4. Now go to Root\PHP-Mailer\examples\, and open up test_smtp_gmail_basic.php.(i have created my own test_gmail.php in side example, with the following content )
  5. Now do some minor editing. (Just change the Emails to your send and receiver, and type your password. )
---------------------------------------------------------------------------------------------
<?php

include("../class.phpmailer.php");

$mail             = new PHPMailer();
$body             = $mail->getFile('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP();
$mail->SMTPAuth   = true;  // enable SMTP authentication
$mail->SMTPKeepAlive = true;
$mail->SMTPSecure = "ssl";  // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port       = 465;// set the SMTP port for the GMAIL server
$mail->Username   = "umeshnarayanan1983@gmail.com";  // GMAIL username
$mail->Password   = "********";// your GMAIL password
$mail->AddReplyTo("umeshnarayanan1983@gmail.com","Umesh Narayanan");
$mail->From       = "umeshnarayanan1983@gmail.com";
$mail->FromName   = "Umesh Narayanan";


$mail->Subject    = "PHPMailer Test Subject via gmail";


$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap   = 50; // set word wrap

$mail->MsgHTML($body);

$mail->AddAddress("umeshnarayanan1@gmail.com", "Umeshnarayanan");

$mail->AddAttachment("images/activate.jpg");     // attachment

$mail->IsHTML(true); // send as HTML

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Email has been sent Sucessfuly!";
}

?>


6.Now, pressing save, you have almost done . Ok, the actual step is you need to edit your PHP.ini file settings. You can find that C:\Windows or  C:\AppServ\php5

or you can Go Start > All Programs > AppServ> Configuration server >PHP Edit the php.ini Configuration  as show below




open the PHP.ini file, then search for “openssl”. Go ahead and remove ‘;’ comment symbol from the start of the line.

extension=php_openssl.dll



  1. Restart your servers (optional -  restart your system )
  2. navigate to http://localhost/PHP_MAIL/examples/test_gmail.php in your browser. ( navigate to your file through browser )
  3. And then check your Receivers Inbox. 

Full source code can be downloaded from the following

Google doc link - https://docs.google.com/open?id=0B30amJFTQ4KbRlExVHNlQWM1QkU
(download and extract the whole folder in your root folder , and change email and password and you have done It!)


if you have any query pls contact me - umeshnarayanan1@gmail.com