无法选择数据库

时间:2015-10-18 12:52:54

标签: php html mysql connection

大家好我尝试为登录编写一些代码,我已经完成了所有操作,但每当我尝试登录时,无法选择数据库。我无法弄明白,我已经在One.com上托管了,当我进入PHP& MySQL设置它说数据库名称是" c343_co_uk",这是我在下面的代码中使用的:

更新:它正在检测数据库,但每当我尝试使用与MySQL完全相同的用户名和密码登录时,它表示无效登录

这是我的 Connection.php

<?php
$username = "c343_co_uk";
$password = "abc";
$hostname = "c343.co.uk.mysql"; 

//connection to the database
$connection = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");


$selected = mysql_select_db("c343_co_uk",$dbhandle) 
  or die("Could not select Database");

?>

Loginform.php

<!DOCTYPE html>

<html>

<a href="index.html" title="back to home">Home</a>
</font>
    <head>

        <style>
            body {
                font-size: 14px;
            }

        </style>
        <link rel="stylesheet" type="text/css" href="website.css" />
    </head>
    <body> 


    <div id="loginform" style="font-family: 'ClearSans-Thin'; color: Black">


        Please enter your login details<br /><br />
        Username:<br />
        <form method="post" action="loginsubmit.php">
            <input type="text" name="username" />
            <br />
            Password:<br />
            <input type="password" name="password" />
            <br />
            <input type="submit" name="submit" value="Submit" />
        </form>
        </center>
</div>
</body>
</html>

Loginsubmit.php

<?php
session_start();
?>
<font face="ClearSans-Thin">
<font color="lightgray">

<?php

include 'connection.php';
include 'loginform.php';
?>
<center>
<?php
if (isset($_POST['submit'])) 
    {
    $user = $_POST['username'];

    $pass = $_POST['password'];
    //Counts up how many matches there are in the database
    $query = "SELECT COUNT(*) AS cnt FROM users WHERE Username='" . mysqli_real_escape_string($connection, $user) . "' && Password='" . mysqli_real_escape_string($connection, $pass). "'";
    $result = mysqli_query($connection, $query);
    $row = mysqli_fetch_assoc($result);

    $queryadmin = "SELECT COUNT(*) AS cnt FROM admin WHERE Username='" . mysqli_real_escape_string($connection, $user) . "' && Password='" . mysqli_real_escape_string($connection, $pass). "'";
    $resultadmin = mysqli_query($connection, $queryadmin);
    $rowadmin = mysqli_fetch_assoc($resultadmin);
    //If count is more than 0, log user in.
    if ($row["cnt"] > 0) 
    {
        $_SESSION["userlogged"] =  $user;
        echo "Logged in - Press the home button to return to the homepage";
    } 
    //count for user table is 0, if there are more than 0 matches in the admin database, start admin session
    else if ($rowadmin["cnt"] > 0 )
        {
            $_SESSION["adminlogged"] = $user;
            echo "Logged in - Press the home button to return to the homepage";
        }
    else 
    {
        echo 'Not a valid login';
    }
}
?>
</center>

5 个答案:

答案 0 :(得分:1)

试试这些:

 $dbname = "c343_co_uk"
 $username = "c343_co_uk";
 $password = "abc";
 $hostname = "c343.co.uk.mysql"; 

并替换

    $selected = mysql_select_db("c343_co_uk",$connection) 
  or die("Could not select Database");

到此:

$selected = mysql_select_db($dbname,$connect) 
  or die("Could not select Database");

我希望工作!祝你有愉快的一天!

答案 1 :(得分:0)

尝试替换

$selected = mysql_select_db("c343_co_uk",$dbhandle) 
  or die("Could not select Database");

$selected = mysql_select_db("c343_co_uk",$connection) 
  or die("Could not select Database");

答案 2 :(得分:0)

用于连接: -

$username = "c343_co_uk";

$password = "abc";

$hostname = "c343.co.uk.mysql";

$connection = mysql_connect($hostname, $username, $password);

@mysql_select_db("c343_co_uk",$connection);

答案 3 :(得分:0)

你可以通过这种方式尝试

<?php
define("DB_HOST", "c343.co.uk.mysql");
define("DB_USER", "c343_co_uk");
define("DB_PASSWORD", "abc");
define("DB_DATABASE", "c343_co_uk");
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,DB_DATABASE );

?>

答案 4 :(得分:0)

你没有用mysql做主机,只做c343.co.uk:

 <?php
 $username = "c343_co_uk";
 $password = "abc";
 $hostname = "c343.co.uk"; 

 //connection to the database
 $connection = mysql_connect($hostname, $username, $password) 
   or die("Unable to connect to MySQL");


 $selected = mysql_select_db("c343_co_uk",$dbhandle) 
   or die("Could not select Database");

 ?>

我希望这项工作!其他我再来看看

相关问题