本文使用的軟件版本如下:PHP版本 5.5.12;MYSQL版本 5.6.17;Apache 2.4.9 用的wampserver
一、PHPMYSQL實現登陸
一共含有兩個文件:login.php和logincheck.php;
表單代碼:
<form action="logincheck.php"method="post"> Yonghu:<inputtype="text" name="username" /> <br /> Mima:<input type="password" name="password" /> <br /> <input type="submit" name="submit" value="登陸" ahref="logincheck.php" /> <a href="register.php">zhuce:</a> </form>
後台處理代碼:
<?php if(isset($_POST["submit"])&& $_POST["submit"] == "登陸") { $user= $_POST["username"]; $psw= $_POST["password"]; if($user== "" || $psw == "") { echo"<script>alert('請輸入用戶名或密碼!'); history.go(-1);</script>"; } else { $link= mysqli_connect('localhost', 'sa', '123456','account');//鏈接數據庫 mysqli_select_db($link,"account"); mysqli_query($link,'setname utf8'); $sql= "selectuser from zhanghu where user = '$_POST[username]'"; $result=mysqli_query($link,$sql)or die("Failed".mysql_error()); if($num=mysqli_num_rows($result)) { $row= mysqli_fetch_array($result); //將數據以索引方式儲存在數組中 echo"welcome "; echo$row[0]; } else { echo"<script>alert('用戶名或密碼不正確!');history.go(-1);</script>"; } } } else { echo"<script>alert('Submit Failed!');history.go(-1);</script>"; } ?>
Account數據庫內容:
二、PHPMYSQL實現模糊查詢
查詢數據庫(只給PHP的代碼):
<?php $mysqli=newmysqli(); $mysqli->connect("localhost","sa", "123456"); if(mysqli_connect_errno()) { printf("Failllllll:%s\n", mysqli_connect_error()); exit(); } $mysqli->select_db("booklib"); $mysqli->query("SETNAMES utf8"); $rsbooks= $mysqli->query("select * from books where Name like'%$_POST[bookname]%'"); $row_rsbooks = $rsbooks->fetch_assoc(); $totalRows_rsbooks = $rsbooks->num_rows; ?> Totel <?php echo $totalRows_rsbooks ?>books; <table width="600"border="1"> <tr> <td bgcolor="#99CCFF"align="center">Name</td> <td bgcolor="#99CCFF" align="center">ISBN</td> <td bgcolor="#99CCFF"align="center">Store</td> <td bgcolor="#99CCFF"align="center">Do</td> </tr> <?php do { ?> <tr> <td><?php echo $row_rsbooks['Name']; ?></td> <td><?php echo $row_rsbooks['ISBN']; ?></td> <td><?php echo $row_rsbooks['Store']; ?></td> <td><fontcolor="#110BAA">rent</font></td> </tr> <?php } while ($row_rsbooks = $rsbooks->fetch_assoc()); ?> </table> <?php $rsbooks->close(); $mysqli->close(); ?>
數據庫表的內容如下:
查詢:
結果:
以上就是本文的全部內容,希望對大家學習php程序設計有所幫助。