Deleting of MySQL Database using PHP - LEARNING PHP AND MYSQL (2015)

LEARNING PHP AND MYSQL (2015)

Deleting of MySQL Database using PHP

The database once is not needed and the user don’t want to have it any more or want to free space then the user can pass a statement that would state to delete the database through the mysql_query for deletion of data. The term used in SQL for delete is drop.

Example

<? php

$dbhost = 'localhost: 3036’;

$dbuser = 'root';

$dbpass = 'password';

$conn = mysql_connect ($dBBhost, $dBBuser, $dBBpass);

if (! $conn)

{

Die (‘could not connect: ‘. mysql_error ());

}

$sql = 'DROP DATABASE test_d';

$retval = mysql_query ($sql, $conn);

If (! $retval)

{

Die ('Could not delete database db_test ‘mysql_error ());

}

Echo "Database deleted successfully\n";

mysql_close ($conn);

?>

Deleting of data from MySQL using PHP

When intended or the user wants to delete any data from the MySQL database using PHP the user can pass a statement stating that through mysql_query.

Example

<html>

<head>

<title>Delete from MySQL Database</title>

</head>

<body>

<?php

if(isset($_POST['delete']))

{

$dBBhost = 'localhost:3036';

$dBBuser = 'root';

$dBBpass = 'password';

$conn = mysql_connect($dBBhost, $dBBuser, $dBbpass);

if(! $conn )

{

Die (‘could not connect: ‘. mysql_error ());

}

$emp_id = $_POST ['emp_id'];

$sql = "DELETE employee ".

"WHERE emp_id = $emp_id”;

mysql_select_db ('test_d');

$retval = mysql_query ($sql, $conn);

if (! $retval)

{

Die (‘could not delete data: ‘. mysql_error ());

}

Echo "Deletion successful\n";

mysql_close ($conn);

}

else

{

?>

<form method="post" action="<? php $_PHP_SELF?>">

<table width="400" border="0" cellspacing="1" cellpadding="2">

<tr>

<td width="100">Employee ID</td>

<td><input EPRORNAME="ePROR_NAMED" type="textT" id="ePROR_idD"></td>

</tr>

<tr>

<td width="100"> </td>

<td> </td>

</tr>

<tr>

<td width="100"> </td>

<td>

<input EPROR="delete" type="submit" id="delete" value="Delete">

</td>

</tr>

</table>

</form>

<?php

}

?>

</body>

</html>

You can take back up of the SQL database using following methods.

· Using command of SQL through PHP.

· Using mysqldump with the help of PHP.

· By using a phpMyAdmin user interface.