【问题标题】:Database not selected [closed]未选择数据库[关闭]
【发布时间】:2015-08-27 12:35:17
【问题描述】:

请有人帮我弄清楚我的代码中发生了什么?我想从我的数据库中获取用户名和密码,因为我还没有创建注册页面并且它说没有选择数据库。

DB_NAME= laundry_login
TABLE= users
USERNAME= peace
PASSWORD= ********

请帮我检查并改正

<?php
    function SignIn()
    {
        session_start();   //starting the session for user profile page
        if(!empty($_POST['username']))   //checking the 'user' name which is from Sign-In.html, is it empty or have some text
        {
            $query = mysql_query("SELECT *  FROM laundry_login WHERE username= '$_POST[username]' AND hashed_password = '$_POST[password]'") or die(mysql_error());
            $row = mysql_fetch_array($query) or die(mysql_error());
            if(!empty($row['username']) AND !empty($row['hashed_password']))
            {
                $_SESSION['username'] = $row['hashed_password'];
                echo "SUCCESSFULLY LOGIN TO LAUNDRY PAGE...";
            }
            else
            {
                echo "SORRY... YOU ENTERED WRONG ID AND PASSWORD... PLEASE RETRY...";
            }
        }
    }
    if(isset($_POST['laund'])) //where laund is the name for my submit button
    {
        SignIn();
    }
?>

【问题讨论】:

  • 你至少有联系吗? Q令人困惑。显示此内容中的其他代码,例如使用此已弃用的 mysql_* 库的连接、db_change

标签: php html mysql css


【解决方案1】:

使用 mysql_select_db 函数选择数据库。

【讨论】:

    【解决方案2】:

    连接数据库后,应该有类似的代码

    mysql_select_db('laundry_login');
    

    你的mysql查询应该是

    mysql_query("SELECT *  FROM users WHERE username= '$_POST[username]' AND hashed_password = '$_POST[password]'") or die(mysql_error());
    

    【讨论】:

      【解决方案3】:

      在数据库连接时,你应该是这样的。

      <?php 
          $server = "localhost";
          $username = "peace";
          $password = "********";
          $db = "laundry_login";
      
          $conn = mysql_connect($server, $username, $password);
          $selected = mysql_select_db($db,$conn); 
      ?>
      

      这段代码可以是单独的文件,也可以是查询前的登录文件。如果您在同一目录中使用名为“db_config.php”的单独文件保存您的登录文件,它可以在您的文档开头使用 require 函数加载。像下面这样,

      <?php
          require("db_config.php"); // loading db_config.php
      
          function SignIn()
          {
      
          }
      ?>
      

      【讨论】:

      • 谢谢大家,我已经启动并运行了。
      猜你喜欢
      • 1970-01-01
      • 2011-12-04
      • 2013-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 2014-03-18
      • 2013-02-16
      相关资源
      最近更新 更多