PHP-1.數據庫鏈接與登錄

 1 <?php
 2 //數據庫信息
 3 $servername = "localhost";
 4 $username = "root";
 5 $password = "****";
 6 $dbname="login";
 7 /*
 8  *  mysql_select_db('l');
 9  *  mysql_query('set names utf8');
10  */ 
11 //打開session
12 session_start();
13 //判斷帳號密碼是否被賦值
14 if (isset($_POST['user']) && isset($_POST['pass'])) {
15     $user = $_POST['user'];
16     $pass = $_POST['pass'];
17     //創建一個數據庫鏈接
18     $conn = mysql_connect($servername,$username,$password);
19     /* 測試
20      * if($conn)
21      *  echo "鏈接成功";
22      *  else die("鏈接失敗".mysql_error());
23      */ }
24     //檢查是否啓動並給出提示
25     if (!$conn) {
26         die("connection failed:" . $conn->connect_error);
27     }
28     //指定一個數據庫
29     $db_select=mysql_select_db($dbname,$conn);
30     //驗證內容是否與數據庫記錄吻合
31     $sql_L = "SELECT * FROM l WHERE (username='$user') AND (password='$pass')";
32     //執行上面的sql語句並賦值給$r
33     $r = mysql_query($sql_L,$conn);
34     //將查到的結果數賦值$result
35     $result = mysql_num_rows($r);
36     //判斷是否有對應記錄
37     if ($result>0) {
38         echo "<script type='text/javascript'>alert('歡迎')</script>";
39     } else
40         echo "<script type='text/javascript'>alert('密碼錯誤')</script>";
41     //關閉數據庫鏈接
42     mysql_close($conn);
43 
44 
45 
46 ?>
相關文章
相關標籤/搜索