Access denied for user ''#'localhost' (using password: NO) [duplicate] - php

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 4 years ago.
Attempting to connect to localhost sql db using the following code (Not doing anything with query at this point just want the query to execute):
<?php
$host = "localhost";
$port = 3306;
$socket = "";
$user = "root";
$password = "Password1";
$dbname = "CIIP_WIKI";
$con = new mysqli($host, $user, $password, $dbname, $port);
if(!$con)
{
echo ("db connection failed!");
die ('Could not connect to the database server' . mysqli_connect_error());
}
else {
echo ("db connection established.");
echo ("<br/>");
}
$query = sprintf("SELECT first_name FROM actor WHERE actor_id='1'");
$result = mysql_query($query);
$con->close();
?>
I keep getting the following...
Welcome
db connection established.
Warning: mysql_query(): Access denied for user ''#'localhost' (using password: NO) in C:\Program Files (x86)\EasyPHP-12.1\www\Cisco Wiki\index.php on line 31
Warning: mysql_query(): A link to the server could not be established in C:\Program Files (x86)\EasyPHP-12.1\www\Cisco Wiki\index.php on line 31
Why does this not work?

This is because you create a connection using mysqli_ and then use mysql_ to try to fetch your result. They are different API's.
<?php
/* You should enable error reporting for mysqli before attempting to make a connection */
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
/* Set the desired charset after establishing a connection */
mysqli_set_charset($mysqli, 'utf8mb4');
printf("Success... %s\n", mysqli_get_host_info($mysqli));
Example taken from the PHP manual

This warning will be logged anytime you try to execute a query without a valid connection object being available.
In your case you thought you had a valid connection object available, but you created it using mysqli while your query is being executed using mysql. Those are two different APIs and so the mysql_query function was looking for a mysql connection object and didn't find it.

Related

Database Selection Failed Unknown database 'login' [duplicate]

This question already has answers here:
How can i solve this "Warning: mysqli_connect(): (HY000/1049): Unknown database" problem?
(6 answers)
Database exists but returns an error saying "Unknown Database"
(1 answer)
Closed 2 years ago.
I was trying to create a login using PHP and MySQL for the database, I did everything but when I try to run the site it gives me a connection error with the database even though I created the database and gave it the corresponding name (login)
This is the code
db_connect.php
<?php
$connection = mysqli_connect('localhost', 'root', '');
if (!$connection){
die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'login');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($connection));
}
authen_login.php
<?php
require('db_connect.php');
if (isset($_POST['user_id']) and isset($_POST['user_pass'])){
// Assigning POST values to variables.
$username = $_POST['user_id'];
$password = $_POST['user_pass'];
// CHECK FOR THE RECORD FROM TABLE
$query = "SELECT * FROM `user_login` WHERE username='$username' and Password='$password'";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
$count = mysqli_num_rows($result);
if ($count == 1){
//echo "Login Credentials verified";
echo "<script type='text/javascript'>alert('Login Credentials verified')</script>";
}else{
echo "<script type='text/javascript'>alert('Invalid Login Credentials')</script>";
//echo "Invalid Login Credentials";
}
}
?>
And the error is this
Database Selection Failed Unknown database 'login'
(I add to the question a photo of the database that maybe the error is there)
You dont have a database called shoolbrk, your database appears to be called login.
Also you you should use mysqli_connect_error() to see connection errors and not mysqli_error($connection). Thats for all other errors other than connection errors.
You can also simplify the connection script as follows
<?php
$connection = mysqli_connect('localhost', 'root', '', 'login');
// database name ^^^^^
if (!$connection){
echo $connection ->connect_error;
}
A better solution would also be to not die() or show errors to the world, instead add mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); to the connection script.
Port number issue
See this answer https://stackoverflow.com/a/62831626/2310830 which describes how WAMPServer manages MySQL and mariaDB port number usage. And how it will allow you to switch the default to MySQL so it uses port 3306 which will allow you to write your code in a way that will run anywhere without having to change the port number. As most hosting companies will have either MySQL or mariaDB listening on 3306

Connect to XAMPP MySQL Database from Document Root on another Drive

would like to ask about configuration of XAMPP mySQL database.
I have set my xampp document root to drive D, and now i unable to connect to SQL database and always get error.
Fatal error: Uncaught Error: Call to undefined function mysql_connect() in D:\localserver\connection.php:3 Stack trace: #0 D:\localserver\login.php(4): include() #1 {main} thrown in D:\localserver\connection.php on line 3
the file that handle the connection look like this
<?php
$connect = mysql_connect("localhost","root","");
if(!$connect) {
die ('connection fail!!!');
} else {
print ('connection okay!!!');
}
$connectdb = mysql_select_db('admin_login');
if(!$connectdb) {
die ('connection fail!!!');
} else {
print ('connection okay!!!');
}
?>
The mysql_connect() function is from a library that is deprecated since a couple of years and has been removed in PHP 7.
Use mysqli_connect() or PDO.
UPDATE
You can pass the name of the database into mysqli_connect() and get rid of the extra mysqli_select_db(). If you want to user mysqli_select_db() in procedural style instead of object oriented it expects the link that is returned by mysqli_connect() as the first parameter and the database name as the second one like this:
$link = mysqli_connect("localhost", $user, $password);
$db = mysqli_select_db($link, $dbname);

Database cant connect [duplicate]

This question already has answers here:
Warning: mysqli_select_db() expects exactly 2 parameters, 1 given
(2 answers)
Closed 1 year ago.
I am using an Ajax Search form which upon connecting to the database, will show search terms upon typing.
But I am getting database connection issue on my site.
This is the code I have in my connection file (db.inc):
<?php
$username = "xxxx";
$password = "xxxx";
$hostname = "xxxx";
$database = "xxxx";
mysqli_connect($hostname, $username, $password) or die(mysql_error());
mysqli_select_db($database) or die(mysqli_error());
?>
But on the frontend, I am getting errors as below:
Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in xxxx/db.inc.php on line 9
Warning: mysqli_error() expects exactly 1 parameter, 0 given in xxxx/db.inc.php on line 9
Can anyone please check what I am doing wrong? I have pasted the complete db.inc file above.
Thank you in advance for helping.
As the error is saying, mysqli_select_db() expects 2 parameters.
The first one is a link returned by mysqli_connect() and the second parameter should be the database name.
The second error means that you have to add the link to the connection as a parameter to mysqli_error.
So you'll have to write it like this:
$link = mysqli_connect($hostname, $username, $password);
if (!$link) {
die('Connection error: ' . mysqli_connect_error());
}
mysqli_select_db($link, $database));
$username = "xxxx";
$password = "xxxx";
$hostname = "xxxx";
$database = "xxxx";
$conn=mysqli_connect($hostname, $username, $password) or die(mysql_error());
mysqli_select_db($conn, $database) or die(mysqli_error());
Here, the second parameter is missing. So, the correct syntax will be
mysqli_select_db($link, $database) or die(mysqli_error());
You are using Procedural Style and in this style, two parameters will be passed in mysqli_select_db().So, the correct syntax will be
bool mysqli_select_db ( mysqli $link , string $dbname )
link
Procedural style only: A link identifier returned by mysqli_connect() or
mysqli_init()
dbname
The database name.
For more details, you can find all the details https://dev.mysql.com/doc/apis-php/en/apis-php-mysqli.select-db.html
You can also add the database name in the single line which will be
mysqli_connect($hostname, $username, $password, $database);

Warning: mysqli_connect(): (28000/1045): Access denied for user [duplicate]

This question already has answers here:
MySQLI 28000/1045 Access denied for user 'root'#'localhost'
(2 answers)
Closed 6 years ago.
i uploaded my php config code to my server befirstw on cpanel ...under the public html ..
my php file is
<?php
$dbhost = "befirstwin.com";
$dbname = "befirstw_intlgent";
$dbuser = "befirstw_intelli";
$dbpass = "Ngcp1988$ ";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(mysqli_connect_errno())
{
die("database connection failed: " .
mysql_connect_error() . "(" . mysql_connect_errno() .")"
);
}
$username = isset($_GET['username']) ? $_GET['username'] : '';
$password = isset($_GET['password']) ? $_GET['password'] : '';
$query = "INSERT INTO users
VALUES ('', '$username', '$password')";
$result = mysqli_query($connection,$query);
if(!$result)
{
die("we have problem");
}
mysqli_close($connection);
?>
i am getting this error
Warning: mysqli_connect(): (28000/1045): Access denied for user 'befirstw_intelli'#'s147.adk-media.com' (using password: YES) in /home/befirstw/public_html/connectimagedb.php on line 8
if you have any idea or any thing more required for php connectivity plz suggest
I check that $ sign in the password is creating problem in my case.Please Do not use $ or any other special Character that may create problem. In my case $ sign in password was creating problem
This could simply be caused by an incorrect password. In your $dbpass, is the space at the end supposed to be there? That extra space before the close " would add a space to the end of the password, causing it to be a different password.
I am wondering if you are opening a connection. I notice the line, where it is closing a connection but not sure if you have opened a connection? Where is is your execute statement for the Insert Query?
Thanks

connecting to phpMyAdmin database with PHP/MySQL

I've made a database using phpMyAdmin , now I want to make a register form for my site where peaple can register .I know how to work with input tags in HTML and I know how to insert data into a database but my problem is that I don't know how I can connect to the database that is already made in phpMyAdmin.
The database is a MySQL database, not a phpMyAdmin database. phpMyAdmin is only PHP code that connects to the DB.
mysql_connect('localhost', 'username', 'password') or die (mysql_error());
mysql_select_database('db_name') or die (mysql_error());
// now you are connected
Connect to MySQL
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'username';
/*** mysql password ***/
$password = 'password';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);
/*** echo a message saying we have connected ***/
echo 'Connected to database';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
Also mysqli_connect() function to open a new connection to the MySQL server.
<?php
// Create connection
$con=mysqli_connect(host,username,password,dbname);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Set up a user, a host the user is allowed to talk to MySQL by using (e.g. localhost), grant that user adequate permissions to do what they need with the database .. and presto.
The user will need basic CRUD privileges to start, that's sufficient to store data received from a form. The rest of the permissions are self explanatory, i.e. permission to alter tables, etc. Give the user no more, no less power than it needs to do its work.
This (mysql_connect, mysql_...) extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. (ref: http://php.net/manual/en/function.mysql-connect.php)
Object Oriented:
$mysqli = new mysqli("host", "user", "password");
$mysqli->select_db("db");
Procedural:
$link = mysqli_connect("host","user","password") or die(mysqli_error($link));
mysqli_select_db($link, "db");
$db = new mysqli('Server_Name', 'Name', 'password', 'database_name');

Categories