# PHP WITH MYSQL (OBJECT OREITNTED CODE)

# PHP-WITH-MYSQL

(1)create table example:

create table example

(id int,

name varchar(200));

(2)write code for “ connect.php ” :

<?php

class db

{

var $host=’localhost’;

var $username=’root’;

var $password=”;

var $connection;

var $database;

function __construct()

{

$this->connection=mysql_connect($this->host,$this->username,$this->password);

if($this->connection)

{

Echo “connection successful”;

echo “<br>”;

}

else

{

echo”connection error”;

echo”<br>”;

}

}

}

$obj=new db();

?>

(2)write code for form.html:

<form action=”insert.php “ method=”post”>

Id<input type=text name=id>

Name <input type=text name=name>

</form>

(3)write code for “insert.php” :

<?php

Class db

{

var $host=”localhost”;

var $username=”root”;

var $password=””;

var $database=”amrapali”;

var $connection;

function __construct()

{

$this->connection=mysql_connect($this->host,$this->username,$this->password);

if($this->connection)

{

echo”connected successfully”;

}

else

{

echo”connection failed”;

}

mysql_select_db($this->database,$this->connection);

}

function insert()

{

$id=$_POST[‘id’];

$name=$_POST[‘name’];

$sql=”insert into example(id,name) values(‘$id’,’$name’)”;

mysql_query($sql);

}

}

$obj=new db();

$obj->insert();

?>

(4) write code for updateform.html :

<form action=”update.php “ method=”post”>

Id<input type=text name=id>

Name <input type=text name=name>

</form>

(5)write code update.php :

<?php

class db

{

var $host=”localhost”;

var $username=”root”;

var $password=””;

var $database=”amrapali”;

var $connection;

function __construct()

{

$this->connection=mysql_connect($this->host,$this->username,$this->password);

if($this->connection)

{

echo”connected successfully”;

}

else

{

echo”connection failed”;

}

mysql_select_db($this->database,$this->connection);

}

function update()

{

$id=$_POST[‘id’];

$name=$_POST[‘name’];

$sql=”update example set id=’$id’ where name=’$name’ “;

mysql_query($sql);

}

}

$obj=new db();

$obj->update();

?>

(6)Write code for “delete.html” file :

<form action=”delete.php “ method=”post”>

Id<input type=text name=id>

</form>

(7)write code for “delete.php” file :

<?php

class db

{

var $host=”localhost”;

var $username=”root”;

var $password=””;

var $database=”amrapali”;

var $connection;

function __construct()

{

$this->connection=mysql_connect($this->host,$this->username,$this->password);

if($this->connection)

{

echo”connected successfully”;

}

else

{

echo”connection failed”;

}

mysql_select_db($this->database,$this->connection);

}

function delete()

{

$id=$_POST[“id”];

$sql=”delete from example where id=’$id'”;

mysql_query($sql);

}

}

$obj=new db();

$obj->delete();

?>

(7)write code for display form table record:

<?php

class db

{

var $host=”localhost”;

var $username=”root”;

var $password=””;

var $database=”amrapali”;

var $connection;

function __construct()

{

$this->connection=mysql_connect($this->host,$this->username,$this->password);

if($this->connection)

{

echo”connected successfully”;

}

else

{

echo”connection failed”;

}

mysql_select_db($this->database,$this->connection);

}

function display()

{

$sql=”select * from example”;

$result=mysql_query($sql);

while($row=mysql_fetch_array($result))

{

echo $row[‘id’];

echo $row[‘name’];

echo”<br>”;

}

}

}

$obj=new db();

$obj->display();

?>