php删除mysql表中数据:

<?php 
function opendata  ($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 (); 
} 
} 
/* http://www.manongjc.com/article/1239.html */
function selectdb ($whichdb, $db){ 
try { 

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

selectdb (\"mydata \",$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(); 
} 
closedata  ($db); 
?>

从上面源码可以看出,我们把mysql_connect封装到opendata 函数中,如果需要连接mysql服务器,直接调用opendata 函数即可,同理,

mysql_select_dbmysql_close都分别封装到selectdb和closedata 函数中,如果需要选择数据库操作和关闭数据库操作,只需要分别调用selectdb和closedata 函数即可。

收藏 打印