简体   繁体   English

PHP连接到mysql数据库

[英]PHP connect to mysql database

I'm trying to make a connection between my database and the login form that I'm using in my website, but I'm facing a problem with that, so every time I click on login, this message appears "No database selected" 我试图在数据库和我在网站上使用的登录表单之间建立联系,但是我遇到了问题,因此,每次单击登录时,此消息都会显示“未选择数据库”

Here is my code: 这是我的代码:

<?php
session_start();

if(isset($_POST['Email']) && isset($_POST['Username']) && isset($_POST['Password']))
{
    $mysql_hostname = "localhost";
    $mysql_user = "root";
    $mysql_password = "";
    $mysql_database = "db_3300";

    $con=mysql_connect($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);

    // Check connection
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }

    $result = mysql_query("SELECT * FROM users where
    Username='".$_POST['Username']."'
    AND Email ='".$_POST['Email']."'
    AND Password ='".$_POST['Password']."'")
    or die (mysql_error());


    while($row = mysql_fetch_array($result))
    { echo $result; 
        echo "You are successfully connected";
        $_SESSION["authenticated"] = true;
        exit;
    }

    mysql_close($con);
}
?>

I hope that someone could help me with this. 我希望有人可以帮助我。

You haven't selected your database yet. 您尚未选择数据库。

Do this after your $con $con之后执行此操作

$con=mysql_connect($mysql_hostname, $mysql_user, $mysql_password); //Removed the $mysql_database from here
$db_selected = mysql_select_db($mysql_database, $con); //Added this

Also, You are using mysql_* functions which will be deprecated in the coming versions of PHP. 另外,您正在使用mysql_ *函数,这些函数将在以后的PHP版本中弃用。 Switch to mysqli_* or PDO Instead. 切换到mysqli_ *或PDO。

Switch over to mysqli while you can. 可以时切换​​到mysqli。 It is really easy, in fact your code is styled for it too. 这确实很容易,实际上您的代码也已为此设置样式。

$con = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);

Now, whenever you do a mysqli_query, you just do 现在,只要您执行mysqli_query,就可以

$query = mysqli_query($con, "SELECT * FROM users WHERE...");

so you have to include the connection in the mysqli_query 所以你必须在mysqli_query中包括连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM