Hi guys I'm trying to write some code for a login, I've got it all but whenever I try login it says Could not select database. I can't figure it out, I've got hosting with One.com and when I go into the PHP & MySQL settings it says the database name is "c343_co_uk", which is what I've used in the code below:
UPDATE: It's detecting the database but whenever I try and log in with the exact same username and password that's on MySQL it says invalid login
Here is my 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>
try to replace
$selected = mysql_select_db("c343_co_uk",$dbhandle)
or die("Could not select Database");
with
$selected = mysql_select_db("c343_co_uk",$connection)
or die("Could not select Database");
Use this for connection :-
$username = "c343_co_uk";
$password = "abc";
$hostname = "c343.co.uk.mysql";
$connection = mysql_connect($hostname, $username, $password);
@mysql_select_db("c343_co_uk",$connection);
you can try by this way
<?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 );
?>
try these:
$dbname = "c343_co_uk"
$username = "c343_co_uk";
$password = "abc";
$hostname = "c343.co.uk.mysql";
and replace
$selected = mysql_select_db("c343_co_uk",$connection)
or die("Could not select Database");
to this:
$selected = mysql_select_db($dbname,$connect)
or die("Could not select Database");
i hope that work! have a nice day!
you haven't do the host with mysql, just do 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");
?>
I hope this work! other i'll take a look again