Could not connect: Access denied for user 'root'#'localhost' - php

<?php
$name= $_POST["dbname"];
$pass= $_POST["dbpass"];
$rname= $_POST["rootname"];
$rpass=$_POST["rootpass"];
if ($rname='leave blank for default root name' or $name='')
{
$rname="root";
}
if ($rpass='leave blank if no password assigned')
{
$rpass='';
}
$con = mysql_connect("localhost", $rname, $rpass);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_query("CREATE DATABASE {$name}",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
(mysql_query("DATABASEPASSWORD {$pass}",$con));
mysql_close($con);
?>
this is the php script i am using to create database,i am passing rootname=root and rootpass=toor
but i am getting an error
Warning: mysql_connect(): Access denied for user 'root'#'localhost'
(using password: NO) in /var/www/webdefender/script/dbcreate.php on
line 17 Could not connect: Access denied for user 'root'#'localhost'
(using password: NO)
but when i use
$con = mysql_connect("localhost", 'root','toor' );
it works fine
Please help

You're assigning values in your first 2 checks instead of checking them for equality.
$a = 1; // assigning
$a == 1; // this is a check if 1 is $a is equal to 1
So you're basically resetting your $rpass value to '' (empty string) in:
if ($rpass='leave blank if no password assigned')
{
$rpass='';
}
Change your checks to use == and it will work.
Please to be aware that using root access from scripts is a HUGE security hazard.

Don't use or use below code near if ($rname='')
if ($rname='')
{
$rname="root";
}

it's general when U use if like this
if($a=b)
always return true and $a have the value b
the right Syntax is
if($a==b)
so if and only if $a value = b it return true

Related

MySQL Database not connecting with name

Whenever I try to connect to my database with my credentials, it doesn't connect with the username, for example when I try to connect to my local database with
mysqli_connect("localhost","dbuser","","database")
It returns
Warning: mysqli_connect(): (HY000/1044): Access denied for user ''#'localhost' to database 'database'
You can't connect using all four parameters on mysqli_connect()
From the above code i can tell your
host : "localhost"
user: "dbuser"
password: ""
database name: "database"
Do this,
$conn = mysqli_connect("localhost", "dbuser", "");
mysqli_select_db($conn, "database");
// to check if you're connected, after tested and see you can take off the code below...
if ($conn) {
echo "Connected";
} else {
echo "Connection failed";
}
if you seriously want to connect using all four parameter above?
$conn = new mysqli("localhost","dbuser","","database");
// to check if you're connected, after tested and see you can take off the code below...
if ($conn) {
echo "Connected";
} else {
echo "Failed";
}
Hope this was helpful?
Just put a space between the quotes like this " " instead of "" to show as blank in the password value in your query because in programming a value either 0(false) or 1(true) has to be there you cannot have nothing and expect something
This is when you have not set password for the user in your local phpmyadmin like root user and also check for spelling mistakes

how to access my database in cpanel server?

The following error occur for my website:
Warning: mysql_connect(): Access denied for user 'mydb_db'#'localhost'
(using password: YES) in
/home/fiveghr/public_html/don/dbconnection/connection.php on line 8
Could not connect: Access denied for user 'mydb_db'#'localhost' (using
password: YES)
<?php
$rpath = $_SERVER["DOCUMENT_ROOT"];
require $rpath."/don/dbconnection/config.php";
class getdbconnection {
public function getConnection(){
// $con = mysql_connect("localhost","*****","******");
$con = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
if (!$con)
{
return die('Could not connect: ' . mysql_error());
} else {
// mysql_select_db("real_state", $con);
mysql_select_db(DB_NAME, $con);
//return $con;
}
}
}
?>
Value of DB_HOST
must be the server address as "http://www.myserver.com" not localhost

PHP - Access Denied while passing parameters through URL

I made a php script which'll echo an image if the correct password is entered; so that, nobody can access the images stored on my server directly, thus, making my server more secure. Now, for the php script I used GET method to generate a mysql_query to my database in order to check if the email and password entered by the user are associated with a relevant account and then echo the image from a folder on my server. Now, in order to pass the parameters while runtime, I'm adding them in the URL like this:
http://<mywebserver>/get_image.php/?email=<email>&password=<password>&file_name=<image-file-name>
But, something's wrong with this whole setup, and I'm getting the following error:
Warning: mysql_query() [function.mysql-query]: Access denied for user
'uXXXXXXXXX'#'XX.XX.XX.XX' (using password: NO) in
/home/uXXXXXXXXX/public_html/get_image.php on line 11
Warning: mysql_query() [function.mysql-query]: A link to the server
could not be established in /home/uXXXXXXXXX/public_html/get_image.php
on line 11 Error getting data: Access denied for user
'uXXXXXXXXX'#'XX.XX.XX.XX' (using password: NO)
Here is my php script, get_image.php:
<?php
$file_path = "/ProfilePics/";
if(isset($_GET['email']) && isset($_GET['password']) && isset($_GET['file_name'])) {
$id = "\"" . $_GET['email'] . "\"";
$typed_password = "\"" . $_GET['password'] . "\"";
$file = $_GET['file_name'];
$result = mysql_query("SELECT * FROM students WHERE email = $id AND password = $typed_password") or die ("Error getting data: " . mysql_error()); //line 11
if(!empty($result)) {
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$user = array();
$user["email"] = $result["email"];
$user["password"] = $result["password"];
$pass = "\"" . $user["password"] . "\"";
if($pass == $typed_password) {
$img_path = $file_path . $file;
echo '<img src="' . $img_path . '" name = "cover" />';
} else {
echo "Incorrect password";
}
} else {
echo "Unable to find user";
}
} else {
echo "Unable to find user";
}
} else {
echo "Required field(s) is missing";
}
?>
I agree, that there are lots of other questions already on stackoverflow stating similar problems. But, I didn't find the solution(s) to those questions applicable for my code. So, any help on this will be highly appreciated. Thank you for your time!
This is because you have not connected your file get_image.php to your MySQL database. Do so as follows:
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="database"; // Database name
$tbl_name="students"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
Simply replace the information above with the correct information.
You don't seem to be connecting to the database before you try to send queries to it.
It should like something like this:
$conn = new mysqli($servername, $username, $password, $dbname);
Taken from the example here:
http://www.w3schools.com/php/php_mysql_select.asp
Furthermore, if you care, you should consider using PDO or some other method of preventing SQL injection attacks.
SQL Injection:
https://en.wikipedia.org/wiki/SQL_injection
Information on PDO:
http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059
setup a database connection first:
$con = mysql_connect('server','username','password');
$rst = mysql_query('select...', $con);
remember that kind of library to access mysql is outdated.
imagine someone logging in with this password:
password = '"; DROP TABLE students;'
or something crafted better.
There are different libraries like PDO or MYSQLI that take cares of this for you.
in newer releases of php standard mysql libis deprecated and removed

mysql_real_escape_string(): Access denied for user 'root'#'localhost' (using password: NO)

I found this errors repeated in my error log and I believe its causing memory leaks leading to downtimes on my site.
PHP Warning: mysql_query(): Access denied for user 'root'#'localhost' (using password: NO) in /mysql.class.php on line 74
PHP Warning: mysql_query(): A link to the server could not be established in /mysql.class.php on line 74
PHP Warning: mysql_real_escape_string(): Access denied for user 'root'#'localhost' (using password: NO) in /functions.php on line 380
PHP Warning: mysql_real_escape_string(): A link to the server could not be established in /functions.php on line 380
PHP Warning: mysql_query(): Access denied for user 'root'#'localhost' (using password: NO) in /mysql.class.php on line 100
PHP Warning: mysql_query(): A link to the server could not be established in /home/glo/mysql.class.php on line 100
In mysql.class.php, here is the code from line 72 to 104
function &execute () {
$query = $this->read();
**$res = mysql_query($query);** // line 74
if ($res || mysql_errno() == 1062) {
return $res;
}
$mysql_error = mysql_error();
$mysql_errno = mysql_errno();
// If debug_backtrace() is available, we can find exactly where the query was called from
if (function_exists("debug_backtrace")) {
$bt = debug_backtrace();
$i = 1;
if ($bt[$i]["function"] == "SQL_Query_exec_cached" || $bt[$i]["function"] == "get_row_count_cached" || $bt[$i]["function"] == "get_row_count")
$i++;
$line = $bt[$i]["line"];
$file = str_replace(getcwd().DIRECTORY_SEPARATOR, "", $bt[$i]["file"]);
$msg = "Database Error in $file on line $line: $mysql_error. Query was: $query.";
} else {
$file = str_replace(getcwd().DIRECTORY_SEPARATOR, "", $_SERVER["SCRIPT_FILENAME"]);
$msg = "Database Error in $file: $mysql_error. Query was: $query";
}
mysql_query("INSERT INTO `sqlerr` (`txt`, `time`)
**VALUES (".sqlesc($msg).", '".get_date_time()."')");** // line 100
if ( function_exists('show_error_msg') )
show_error_msg("Database Error", "Database Error. Please report this to an Admin.", 1);
}
In functions.php here is the code
// MySQL escaping
function sqlesc($x) {
if (!is_numeric($x)) {
$x = "'".mysql_real_escape_string($x)."'"; // Line 380
}
return $x;
}
The code for my connection is
function_exists("mysql_connect") or die("MySQL support not available.");
#mysql_connect($mysql_host, $mysql_user, $mysql_pass) or die('DATABASE: mysql_connect: ' . mysql_error());
#mysql_select_db($mysql_db) or die('DATABASE: mysql_select_db: ' . mysql_error());
Any help to debug these errors is much appreciated....
It looks like the issue here is that the connection is outside of the function's scope and so the mysql functions are attempting to create a new connection without any credentials.
Try assigning your connection to a variable and then calling it as a global within your functions.
$conn = #mysql_connect($mysql_host, $mysql_user, $mysql_pass);
function &execute () {
global $conn;
$query = $this->read();
$res = mysql_query($query, $conn);
....
You can read more about scope in the PHP manual here http://www.php.net/manual/en/language.variables.scope.php
Note: Using global variables isn't advised - it would be better practice to use a more OO approach and create the connection in your MySQL class eg:
public function connect ($host, $user, $pass) {
$this->connection = mysql_connect($host, $user, $pass);
}
function &execute () {
$query = $this->read();
$res = mysql_query($query, $this->connection);
....
and then using something like this to connect
$db = new MysqlClassName();
$db->connect($mysql_host, $mysql_user, $mysql_pass);
Display errors could be turned off in the php.ini or your apache config file.
You can turn it on in the script:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
write above script in top of page and u will see error message.
You should see the same messages in the php error log.

2 MySQL connections across 2 differents host

I have 2 connection database (MySQL) in different host like this:
//database 1
$dbhost='localhost';
$dbuser='user1';
$dbpass='pass1';
$dbname='dbname1';
//database 2
$dbhostams='192.168.16.3';
$dbuserams='user2';
$dbpassams='pass2';
$dbnameams='dbname2';
function dbconnection($dbhost,$dbuser,$dbpass,$dbname){
if(!mysql_connect($dbhost,$dbuser,$dbpass)){
echo "Error occure on opening connection or database. Period.";
}else{
if(!mysql_select_db($dbname)){
echo "Error occure on select databases !";
}
}
}
function dbconnectionams($dbhostams,$dbuserams,$dbpassams,$dbnameams){
$cxn = mysql_connect($dbhostams,$dbuserams,$dbpassams,$dbnameams);
if( $cxn === FALSE ) {
die('mysql connection error: '.mysql_error());
}else{
if( !mysql_select_db($dbnameams) ){
echo "Error occure on select databases !";
}
}
}
when i use:
dbconnection($dbhost,$dbuser,$dbpass,$dbname);
at my page code, and use:
dbconnectionams($dbhostams,$dbuserams,$dbpassams,$dbnameams);
at another line of code in same page, error occured, like this:
Warning: Access denied for user: 'apache#localhost' (Using password: NO) in
/home/firman/html/fdrsimpeg/sdm-aam/include/dbclass.php on line 17
Warning: MySQL Connection Failed: Access denied for user: 'apache#localhost'
(Using password: NO) in /home/firman/html/fdrsimpeg/sdm-aam/include/dbclass.php
on line 17
mysql connection error:
what must i do to solve this problem?
Your global variables ($dbhost, $dbuser, etc.) are not within the scope of your function calls; for example, if those function calls take place within another function, you will need to declare the variables within the function with the global keyword in order to access them (otherwise PHP thinks you're referring to different variables of the same name that are local to the function):
function foo() {
global $dbhost, $dbuser, $dbpass, $dbname;
dbconnection($dbhost,$dbuser,$dbpass,$dbname);
}
Read more on PHP variable scope.
First of all, change the variable names, it will be easier for you to read the code ... Second, the problem seems to be with the connection, as it is detecting that the username is "apache". Check the variables names in the functions ... change them and try again.
//database 1
$dbhost='localhost';
$dbuser='user1';
$dbpass='pass1';
$dbname='dbname1';
//database 2
$dbhostams='192.168.16.3';
$dbuserams='user2';
$dbpassams='pass2';
$dbnameams='dbname2';
function dbconnection($_dbhost,$_dbuser,$_dbpass,$_dbname){
if(!mysql_connect($_dbhost,$_dbuser,$_dbpass)){
echo "Error occure on opening connection or database. Period.";
}else{
if(!mysql_select_db($_dbname)){
echo "Error occure on select databases !";
}
}
}
function dbconnectionams($_dbhostams,$_dbuserams,$_dbpassams,$_dbnameams){
$cxn = mysql_connect($_dbhostams,$_dbuserams,$_dbpassams,$_dbnameams);
if( $cxn === FALSE ) {
die('mysql connection error: '.mysql_error());
}else{
if( !mysql_select_db($dbnameams) ){
echo "Error occure on select databases !";
}
}
}
You need to pass true as fourth parameter to the mysql_connect function.
$database1 = mysql_connect($host1, $user1, $pass1);
$database2 = mysql_connect($host2, $user2, $pass2, true);
mysql_select_db('database1', $database1);
mysql_select_db('database2', $database2);
And to query your database use this:
mysql_query('SELECT * FROM table', $database1);
mysql_query('SELECT * FROM table', $database2);
Check this answer here on Stackoverflow....link
I didn't see anything obviously wrong, I tested your code as provided and both connections worked.
Check to see if mysql.default_user, mysql.default_host etc. is set within your php.ini

Categories