Home > headlines > body text

PHP small practical recommendation: PHP+mysql implementation of login and query function code examples

伊谢尔伦
Release: 2019-02-22 16:51:56
Original
6984 people have browsed it

This article mainly introduces the methods of PHP MYSQL to implement login and fuzzy query functions. The login function and query function are very practical functions and are necessary in the website. Below, our PHP Chinese website will bring practical implementation cases for beginners. We hope it will be helpful to everyone's understanding and learning.

Related mysql video tutorial recommendations: "mysql tutorial"                                            

There are also related practical tutorial recommendations on our PHP Chinese website: PHP login registration page tutorial

Related links: http://www.php.cn/course/120.html

Related video tutorials: PHP user registration login system video Teaching

Related links: http://www.php.cn/course/192.html

1. PHPMYSQL implementation Login

contains a total of two files: login.php and logincheck.php;
Form code:

<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>
Copy after login

Background processing code:

<?php

     if(isset($_POST["submit"])&& $_POST["submit"] == "登陆")

     {

          $user= $_POST["username"];

          $psw= $_POST["password"];

          if($user== "" || $psw == "")

          {

              echo"<script>alert(&#39;请输入用户名或密码!&#39;); history.go(-1);</script>";

          }

          else

          {

              $link= mysqli_connect(&#39;localhost&#39;, &#39;sa&#39;, &#39;123456&#39;,&#39;account&#39;);//链接数据库

              mysqli_select_db($link,"account");

              mysqli_query($link,&#39;setname utf8&#39;);

              $sql= "selectuser from zhanghu where user = &#39;$_POST[username]&#39;";

              $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(&#39;用户名或密码不正确!&#39;);history.go(-1);</script>";

              }

          }

     }

     else

     {

          echo"<script>alert(&#39;Submit Failed!&#39;);history.go(-1);</script>";

     }

 

?>
Copy after login

Account database content:

2. PHPMYSQL implements fuzzy query

Query database (only for PHP code):

<?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&#39;%$_POST[bookname]%&#39;");

  $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[&#39;Name&#39;]; ?></td>

   <td><?php echo $row_rsbooks[&#39;ISBN&#39;]; ?></td>

   <td><?php echo $row_rsbooks[&#39;Store&#39;]; ?></td>

   <td><fontcolor="#110BAA">rent</font></td>

  </tr>

  <?php } while ($row_rsbooks = $rsbooks->fetch_assoc()); ?>

</table>

<?php

 $rsbooks->close(); 

 $mysqli->close();

?>
Copy after login

The contents of the database table are as follows:

Query:

Result:

The above is the entire content of this article, I hope it will be helpful to everyone learning PHP programming.

Related recommendations:

1. php uses object-oriented to implement user login authentication function

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template