Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

Wednesday 20 November 2013

PHP MYSQL Login and Registration using Functions

// siddhu vydyabhushana // 139 comments
Today am going to explain you the code for Login and Registration using functions in php,mysql . The best methodology in any language is using Object Oriented Principles.Dealing with classes, objects very easy to manipulate information. Below code for how to develop user registration and login  form in php.

login and registration form using php functions

This tutorial contain only 2 files  functions.php and login_reg.php

Database
simple database with 4 fields id, name, email, password

CREATE TABLE IF NOT EXISTS `javatyro_registration` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` text NOT NULL,
  `email` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

Functions.php
class name profile() -> object $profile

<?php
class profile
{
public function db()
{
mysql_connect("localhost","root","")or die(mysql_error());
mysql_select_db("demos") or die(mysql_error());
}
public function registration($name,$email,$password)
{
$query=mysql_query("SELECT * FROM javatyro_registration where email='$email' ");
if(!$query)
{
$query=mysql_query("INSERT INTO javatyro_registration VALUES ('','$name','$email','$password')");
if($query>0)
echo "<h2> hiee..<font color=green>".$name."</font>
</h2><br/><font color=green> You Registered Successfully</font>";
else
echo "<font color=red>failed</font>";
}
else
echo "<h3><font color=red>Sorry,Already Registered</font></h3>";
}
public function login($email,$password)
{
$query=mysql_query("SELECT name FROM javatyro_registration where 
email='$email' AND password='$password' ");
$query=mysql_fetch_array($query);
if($query>0)
echo "<h2> hiee..<font color=blue>".$query['name'].
"</font></h2><br/><font color=green>Login Success</font>";
else
echo "<font color=red>failed</font>";
}
}
?>
login_reg.php
code for login , registration from including php code
<!Doctype>
<html>
<head>
<?php include_once 'functions.php';?>
<style>
body{font-family:segoe ui}
.registration{background:#eeeeee;padding:20px}
.login{background:pink;padding:20px}
input{padding:5px}
</style>
</head>
<body>
<center>
<h2>JAVATYRO-<a href="http://www.facebook.com/siddhucse"
 target="_blank">SIDDHU VYDYABHUSHANA</a></h2>
<table>
<tr>
<td>
<div class="registration">
<form method="POST" action="login_reg.php">
Full Name:<br/>
<input type="text" name="name" required><br/>
Email:<br/>
<input type="email" name="email" required><br/>
Password:<br/>
<input type="password" name="pass" required><br/><br/>
<input type="submit" value="Register">
</form>
</div>
</td>
<td>
<div class="login">
<form method="POST" action="login_reg.php">
Email:<br/>
<input type="email" name="login_email" required><br/>
Password:<br/>
<input type="password" name="login_pass" required><br/><br/>
<input type="submit" value="Login">
</form>
</div>
</td>
</tr>
</table>
<?php
if($_SERVER['REQUEST_METHOD']=="POST")
{
//object for class
$profile=new profile();
//database calling
$profile->db();

//registration function calling
if(isset($_POST['name']) && isset($_POST['email'])
&& isset($_POST['pass']))
$profile->registration($_POST['name'],$_POST['email'],$_POST['pass']);

//login function calling
if(isset($_POST['login_email']) && isset($_POST['login_pass']))
$profile->login($_POST['login_email'],$_POST['login_pass']);

}
?>
</center>
</body>
</html>
How to add new Function:

public function function_name(argument1,argument2)
{
//query to execute
}
I hope you enjoyed my tutorial..
Read More

Wednesday 3 July 2013

Connect To MySQL With JDBC Driver

// siddhu vydyabhushana // 4 comments
Here’s an example to show you how to connect to MySQL database via a JDBC driver. First, get a MySQL JDBC driver from here -MySQL JDBC Driver Download Here.

1. Java JDBC connection example

Code snippets to use a JDBC driver to connect a MySQL database.
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","username", "password");
conn.close();
See a complete example below :
JDBCExample.java
package com.mkyong.common;
 
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
 
public class JDBCExample {
 
  public static void main(String[] argv) {
 
 System.out.println("-------- MySQL JDBC Connection Testing ------------");
 
 try {
  Class.forName("com.mysql.jdbc.Driver");
 } catch (ClassNotFoundException e) {
  System.out.println("Where is your MySQL JDBC Driver?");
  e.printStackTrace();
  return;
 }
 
 System.out.println("MySQL JDBC Driver Registered!");
 Connection connection = null;
 
 try {
  connection = DriverManager
  .getConnection("jdbc:mysql://localhost:3306/mkyongcom","root", "password");
 
 } catch (SQLException e) {
  System.out.println("Connection Failed! Check output console");
  e.printStackTrace();
  return;
 }
 
 if (connection != null) {
  System.out.println("You made it, take control your database now!");
 } else {
  System.out.println("Failed to make connection!");
 }
  }
}

2. Run it

Assume JDBCExample.java is store in c:\test folder, along with the MySQL JDBC driver
C:\test>java -cp c:\test\mysql-connector-java-5.1.8-bin.jar;c:\test JDBCExample
-------- MySQL JDBC Connection Testing ------------
MySQL JDBC Driver Registered!
You made it, take control your database now!
 
C:\test>
P.S To run this example, your need mysql-connector-java-{version}-bin.jar in your classpath.
Done.
Read More