您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)使用php怎么對(duì)mysql進(jìn)行增刪改查操作,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
列表代碼
<?php $con = mysql_connect("localhost:3306","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $result = mysql_query("SELECT * FROM user"); echo "<table border='1'> <tr> <th>Username</th> <th>Password</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['username'] . "</td>"; echo "<td>" . $row['password'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?>
刪除
<?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); mysql_query("DELETE FROM user WHERE username = '$_POST[username]'"); mysql_close($con); ?>
添加
<?php $con = mysql_connect("localhost:3306","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $sql = "INSERT INTO user (username,password) VALUES ('$_POST[username]','$_POST[password]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con); ?>
修改
<?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); mysql_query("UPDATE user SET password = '$_POST[password]' WHERE username = '$_POST[username]'"); mysql_close($con); ?>
<html> <head> <title>用PHP向數(shù)據(jù)庫(kù)中實(shí)現(xiàn)簡(jiǎn)單的增刪改查</title> </head> <body> <br /> <h2>Insert:</h2> <form action="insert.php" method="post"> username:<input type="name" name="username"/> <br /> password:<input type="password" name="password"/> <input type="submit" value="submit"/> </form> <br /><hr /><br /> <h2>Delete</h2> <form action="delete.php" method="post"> username:<input type="name" name="username" /> <br /> Are you sure?<input type="submit" value="sure" /> </form> <br /><hr /><br /> <h2>Update</h2> <form action="update.php" method="post"> username:<input type="name" name="username"/> <br /> You want to change your password into:<input type="password" name="password"/> <input type="submit" value="submit"/> </form> <br /><hr /><br /> </body> </html>
上述就是小編為大家分享的使用php怎么對(duì)mysql進(jìn)行增刪改查操作了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。