Deleting Data : mysql_query « MySQL Database « PHP






Deleting Data

 
<?php 
function opendatabase ($host,$user,$pass) { 
try { 
if ($db = mysql_connect ($host,$user,$pass)){ 
return $db; 
} else { 
throw new exception ("Sorry, could not connect to mysql."); 
} 
} catch (exception $e) { 
echo $e->getmessage (); 
} 
} 

function selectdb ($whichdb, $db){ 
try { 

if (!mysql_select_db ($whichdb,$db)){ 
throw new exception ("Sorry, database could not be opened."); 
} 
} catch (exception $e) { 
echo $e->getmessage(); 
} 
} 
function closedatabase ($db){ 
mysql_close ($db); 
} 
$db = opendatabase ("localhost","root",""); 

selectdb ("mydatabase",$db); 
$updatequery = "DELETE FROM mytable WHERE id=2"; 
try { 

if (mysql_query ($updatequery, $db)){ 

echo "Your record has been removed."; 

if ($aquery = mysql_query ("SELECT * FROM mytable WHERE id=2")){ 

echo "<br />" . mysql_num_rows ($aquery);

} else { 

echo mysql_error(); 

} 
} else { 
throw new exception (mysql_error()); 
} 
} catch (exception $e) { 
echo $e->getmessage(); 
} 
closedatabase ($db); 
?>
  
  








Related examples in the same category

1.Add record to my_database/my_table
2.Adding a Row to a Table
3.Get data from mysql
4.mysql_query.php
5.function mysql_query() queries the database.
6.Storing Information in a Database
7.Using PHP variables wherever you want inside SQL queries
8.Sign the guestbook