将PHP连接到MYSQL时出错

时间:2017-09-11 05:54:35

标签: php mysql

美好的一天!我试图将我的代码连接到数据库localhost / phpmyadmin。

这是我的代码:

在register.php文件中:

<?php
    session_start();
    require_once('dbconfig/config.php');
    //phpinfo();
?>

config.php是数据库名称所在的文件

<!DOCTYPE html>
<html>
<head>
<title>Registration Page</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body style="background-color:#bdc3c7">
    <div id="main-wrapper">
    <center><h2>Registration Form</h2></center>
        <form action="register.php" method="post">
            <div class="imgcontainer">
                <img src="imgs/avatar.png" alt="Avatar" class="avatar">
            </div>
            <div class="inner_container">
                <label><b>Username</b></label>
                <input type="text" placeholder="Enter Username" name="username" required>
                <label><b>Password</b></label>
                <input type="password" placeholder="Enter Password" name="password" required>
                <label><b>Confirm Password</b></label>
                <input type="password" placeholder="Enter Password" name="cpassword" required>
                <button name="register" class="sign_up_btn" type="submit">Sign Up</button>

                <a href="index.php"><button type="button" class="back_btn"><< Back to Login</button></a>
            </div>
        </form>

        <?php
            if(isset($_POST['register']))
            {
                @$username=$_POST['username'];
                @$password=$_POST['password'];
                @$cpassword=$_POST['cpassword'];

                if($password==$cpassword)
                {
                    $query = "select * from userinfotbl where username='$username'";
                    //echo $query;
                $query_run = mysqli_query($con,$query);
                //echo mysql_num_rows($query_run);
                if($query_run)
                    {
                        if(mysqli_num_rows($query_run)>0)
                        {
                            echo '<script type="text/javascript">alert("This Username Already exists.. Please try another username!")</script>';
                        }
                        else
                        {
                            $query = "insert into userinfotbl values('$username','$password')";
                            $query_run = mysqli_query($con,$query);
                            if($query_run)
                            {
                                echo '<script type="text/javascript">alert("User Registered.. Welcome")</script>';
                                $_SESSION['username'] = $username;
                                $_SESSION['password'] = $password;
                                header( "Location: homepage.php");
                            }
                            else
                            {
                                echo '<p class="bg-danger msg-block">Registration Unsuccessful due to server error. Please try later</p>';
                            }
                        }
                    }
                    else
                    {
                        echo '<script type="text/javascript">alert("DB error")</script>';
                    }
                }

此行发生错误。但我并不确切知道发生了什么。

                else
                {
                    echo '<script type="text/javascript">alert("Password and Confirm Password do not match")</script>';
                }

            }
            else
            {
            }
        ?>
    </div>
</body>
</html>

然而,在localhost页面中,我收到错误消息&#34; Localhost说:DB Error&#34; 我不知道该怎么回事。

2 个答案:

答案 0 :(得分:0)

这意味着"select * from userinfotbl where username='$username'"查询找不到任何符合条件或出错的实体。

  

失败时返回FALSE。成功的SELECT,SHOW,DESCRIBE或   EXPLAIN查询mysqli_query()将返回一个mysqli_result对象。对于   其他成功的查询mysqli_query()将返回TRUE。

http://php.net/manual/en/mysqli.query.php

使用mysqli_error查看您遇到的错误。

示例:

if($password==$cpassword)
{
    $query = "select * from userinfotbl where username='$username'";
    //echo $query;
    $query_run = mysqli_query($con,$query);
    //echo mysql_num_rows($query_run);
    if(!empty($query_run)) {
        ...
    } elseif ($query_run === false)
        echo mysqli_error($con);
    else
        echo '<script type="text/javascript">alert("DB error")</script>';
}

答案 1 :(得分:0)

您必须使用mysqli_error检查连接错误或查询错误