Connect to Mysql database using php Fail - php

Hi so i am trying to make a login system using mysql and php but i constantly get the error
Warning: mysql_connect(): Access denied for user 'testuser'#'localhost' (using password: YES) in E:\PHP_Runner\php\root\process.php on line 10
The user is created as seen in the Any suggestions/help?
<?php
$username = $_POST['user'];
$password = $_POST['pass'];
$username = stripcslashes($username);
$password = stripcslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
mysql_connect("localhost",$username, $password) or die("Could not connect to database");
mysql_select_db("users");
$result = mysql_query("select * from users where username = '$username' and password = '$password'")
or die("Failed to query database" .mysql_error());
$row = mysql_fetch_array($result);
if($row['username'] == $username && $row['password'] == $password){
echo "Login success !! welcome".$row['username'];
}else{
echo "Failed to login";
}
?>

To connect to mysql db phpmyadmin or any interface you will need to have mysql login credentials which is not a user login credentials this is usually set at the installation of mysql or you will be using the default. Now this set credentials will replace mysql_connect("localhost", $TheSetUserNameForMysqlDb, $TheSetPasswordForMysqlDb) I hope this help
Which I don't recommend using the default

mysql connect should be something like
mysql_connect("localhost","root", ""); // if you use phpmyadmin in localhost. here root is the defualt admin username of mysql and default password is empty.

Related

Which username/password do I use to connect to mySQL

So, I'm SUPER new to mySQL. Having issues connecting to my database with PHP. Hoping someone can point me in the right direction.
I can login to our Cpanel using my username/password. Using the web gui I was able to create a database.
Now when I try to connect to the database (or even just the server for that matter) using PHP I get an error:
Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'username'#'servername' (using password: YES) in /home/ropepart/public_html/techportal/sandbox/mysqltest.php on line 8
Connection failed: Access denied for user 'username'#'servername' (using password: YES)
To me, it seems like this is a username/password error. But I an using the same username/password that I use to login to the web gui. Why can I successfully login there, but can't login with PHP?
Here's my code (taken pretty much directly from W3Schools):
<?php
$servername = "servername";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
(Hopefully it's obvious that I've changed my servername/username/password for the purposes of this post)
Check the details correctly, By deafult
host = localhost
username = root
password = (No password leave it as empty)
database = (your database)
<?php
$connection = new mysqli('host','username','password','your_database');
if($connection->connect_error || $connection->error){
echo "error";
}else{
echo "done";
}
?>
you should add database name.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "demo"
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
//if you are using xampp you shoul locate this file you get information from this
passwords.txt
MySQL database username/password is not necessarily same as cPanel username & password.
Here is what you should do:
Login to your cPanel
From dashboard click "MySQL® Databases"
Add a new user from there (https://prnt.sc/kpiby9). Just fill username
and password (note down the password).
Add that user with your database by selecting both from the dropdown
(https://prnt.sc/kpidqx)
Now, use this username & password to connect with the database.
mysqli_connect(serverhostname,username,password,dbname);

Inserting data into a remote database

I'm trying to make a web/login page, where if a user is in certain location then they can be able to insert data in the database based on their location.
For example, if we have two users bob and max, one in A(bob) and the other in B(max).
If bob logs into the system username... bob password... 123 location.. A, he should be able to to insert his data on the database located on his PC same as max but they should use a single application.
Now my question is how can I achieve this. For example I've a login script below which is communicating with my localhost (A) and i also want to include a remote connection to my other PC on the LAN (B), using Mysql database installed on both computers, I can connect to Mysql database using Mysql workbench but how can I do it using a script in PHP?
login page
<?php
if(isset($_POST['login'])){
include 'includes/config.php';
$uname = $_POST['uname'];
$pass = $_POST['pass'];
$query = "SELECT * FROM admin WHERE uname = '$uname' AND pass = '$pass'";
$rs = $conn->query($query);
$num = $rs->num_rows;
$rows = $rs->fetch_assoc();
if($num > 0){
session_start();
$_SESSION['uname'] = $rows['uname'];
$_SESSION['pass'] = $rows['pass'];
echo "<script type = \"text/javascript\">
alert(\"Login Successful.................\");
window.location = (\"admin/index.php\")
</script>";
} else{
echo "<script type = \"text/javascript\">
alert(\"Login Failed. Try Again................\");
window.location = (\"login.php\")
</script>";
}
}
?>
connection.php
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "cars";
$conn = new mysqli($host, $user, $pass, $db);
if($conn->connect_error){
echo "Failed:" . $conn->connect_error;
}
?>
Configuring two connection scripts is what i have in mind, but i don't
think it will work.
Any suggestions would be appreciated.
Thanks

login function with php mysql error

I am trying to create a login function with php.
Till now this is what I got.
<?php
include("dbconnection.php");
//select a database to work with
$mysqli->select_db("eamunter_opskrift");
Echo ("Selected the eamunter_opskrift database...<BR><BR>");
// get the username and password from the login form
$username=$_POST['username'];
$password=$_POST['password'];
//Protect username and password
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$query ="SELECT * FROM brugerregistration WHERE username='$username' and password='$password'";
$result = $mysqli->query($query);
$count=mysqli_num_rows($result);
// If result matched $username and $password, the count of table rows should equal 1
if($count==1){
// Register session variables and redirect the user to "login_success.php" page
session_start();
$_SESSION['username']= $username;
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
I keep getting the same error
And I can't figure out what the problem is... My dbconnection.php works everywhere else.
Selected the eamunter_opskrift database...
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in /home/www/.dk/reg/post_login.php on line 13
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: No such file or directory in /home/www/.dk/reg/post_login.php on line 13
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/www/.dk/reg/post_login.php on line 13
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in /home/www/.dk/reg/post_login.php on line 14
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: No such file or directory in /home/www/.dk/reg/post_login.php on line 14
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/www/.dk/reg/post_login.php on line 14
Wrong Username or Password
Can anybody help me???
If important I can upload the form too, but didn't think it mattered.
Replace mysql_real_escape_string with $mysqli->real_escape_string
(Also, you don't need the stripslashes calls, real_escape_string should do all that for you)
You are mixing up mysql_* and mysqli_* functions.
It should be
$username = $mysqli->real_escape_string($username);
$password = $mysqli->real_escape_string($password);
Try it:
<?php
include("dbconnection.php");
//select a database to work with
$mysqli->select_db("eamunter_opskrift");
Echo ("Selected the eamunter_opskrift database...<BR><BR>");
// get the username and password from the login form
$username=$_POST['username'];
$password=$_POST['password'];
//You should use your object and current connection
$username = $mysqli->real_escape_string($username);
$password = $mysqli->real_escape_string($password);
$query ="SELECT * FROM brugerregistration WHERE username='$username' and password='$password'";
$result = $mysqli->query($query);
$count=mysqli_num_rows($result);
// If result matched $username and $password, the count of table rows should equal 1
if($count==1){
// Register session variables and redirect the user to "login_success.php" page
session_start();
$_SESSION['username']= $username;
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
see the doc of mysql_real_escape_string
replace :
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
by :
$username = $mysqli->real_escape_string($username);
$password = $mysqli->real_escape_string($password);

Registration script.php does not work

I have a registration script that doesn't work. It used to work but then suddenly it stopped working. I dont know what I've done or what happend. I have tried for like an hour now to find out where the problem is, but I can't seem to find it.
What happens? When I click register I don't get any errors but it doesn't upload to the database:
When I type: $res = mysql_query($query) or die ("ERROR"); it displays ERROR so I think it's related to that code:
//=============Configuring Server and Database=======
$host = 'localhost';
$user = 'root';
$password = '';
//=============Data Base Information=================
$database = 'login';
$conn = mysql_connect($host,$user,$password) or die('Server Information is not Correct'); //Establish Connection with Server
mysql_select_db($database,$conn) or die('Database Information is not correct');
//===============End Server Configuration============
//=============Starting Registration Script==========
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['pass1']);
$email = mysql_real_escape_string($_POST['email']);
$country = mysql_real_escape_string($_POST['country']);
$rights = '0';
$IP = $_SERVER['REMOTE_ADDR'];
//=============To Encrypt Password===================
//============New Variable of Password is Now with an Encrypted Value========
$query = mysql_query("SELECT username FROM users WHERE username = '".$username."'");
if (mysql_num_rows($query) > 0)
{
echo 'Username or email already in use.';
}
else{
if(isset($_POST['btnRegister'])) //===When I will Set the Button to 1 or Press Button to register
{
$query = "insert into users(username,password,email,country,rights,IP,registrated)values('$username','$password','$email','$country',$rights,$IP,NOW())" ;
$res = mysql_query($query);
header("location:load.php");
}
}
I think you are getting the error because you are missing some quotes in your query for two columns wich really looks like not integers
'$email','$country',$rights,$IP,NOW())" //$rights and $ip doesn't have quotes
so your query would look like
"insert into users(username,password,email,country,rights,IP,registrated)values('$username','$password','$email','$country','$rights','$IP',NOW())"
Use mysql_error() in order to see what the error message is. If you haven't changed anything in the script then it's either your database structure has changed or your rights have.
Update:
You don't have quotes around your IP value, which is surprising because you said that query worked until now. Anyway you should also consider saving IPs the RIGHT way. Good luck!

PHP login check flow

Im having trouble with a php login script (below). I want to redirect to nouser.php if someone enters a username that does not exist and to wrongpass.php if the wrong password (but a valid username) is entered. The below code almost works. If I comment out the entire wrong password section then the nouser section works as expected displaying the nouser page , but if I leave the wrong password section in I get the wrongpass.php page for both nouser and wrong password situations. If I put a valid user in but with wrong password then I get wrong password (correct behavior).
Simply put , how can i make sure that I get redirect to nouser.php if there is nouser of this name and not the wrongpass.php page..
<?php
$username = $_POST['username'];
$password = $_POST['password'];
//connect to the database here
require_once 'includes/login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database, $db_server)
or die("Unable to select database: " . mysql_error());
$username = mysql_real_escape_string($username);
$query = "SELECT password, salt
FROM users
WHERE username = '$username';";
$result = mysql_query($query);
//wrong user section
if(mysql_num_rows($result) < 1) //no such user exists
{
header('Location: nouser.php');
}
//wrong password section
$userData = mysql_fetch_array($result, MYSQL_ASSOC);
$hash = hash('sha256', $userData['salt'] . hash('sha256', $password) );
if($hash != $userData['password']) //incorrect password
{
header('Location: wrongpass.php');
}
//login successful
?>
I think you should add die() to stop the script
if(mysql_num_rows($result) < 1) {
header('location:nouser.php');
die();
}
Has not yet test the code.

Categories