php使用mysql封装函数删除表数据
|
php删除mysql表中数据: function opendatabase ($host,$user,$pass) { try { if ($db = mysql_connect ($host,$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,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 " } else { echo mysql_error(); } } else { throw new exception (mysql_error()); } } catch (exception $e) { echo $e->getmessage(); } closedatabase ($db); ?> 从上面源码可以看出,我们把mysql_connect封装到opendatabase函数中,如果需要连接mysql服务器,直接调用opendatabase函数即可,同理, mysql_select_db、mysql_close都分别封装到selectdb和closedatabase函数中,如果需要选择数据库操作和关闭数据库操作,只需要分别调用selectdb和closedatabase函数即可。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
