This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 5 years ago.
I am getting this error:
Access denied for user ''#'localhost' (using password: NO)
The code passes the mysqli_connect but fails on mysql_select_db($db);
below is my code:
$conn = mysqli_connect($host,'segokaku_user','SegokakuDB',$db);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$db_selected = mysql_select_db($db);
if (!$db_selected) {
echo 'Can\'t use database : ' . mysql_error();
}
Use
mysqli_select_db($conn, $db)
instead
mysql_select_db($db)
I think it should solve your problem
Related
This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 3 years ago.
I have been trying to connect my registrationform with my sql database for a few days now. I've tried to find the issues in my code but eventually I went for a simple check if a connection was possible with the following code:
<?php
define('DB_HOST', '`............');
define('DB_NAME', '.............');
define('DB_USER', '..............');
define('DB_PASSWORD', '..........');
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>
Which always returns blank. This was equally the issue when I tried to connect my registrationform to the server.
Can someone maybe explain what could be wrong.
I checked the DB host, name,username and password, so that can't be the problem.
thanks
these days you want to use mysqli like the following (I don't define variables [usr/pass/db/etc] just to then use them so I just put the info directly in the command as below):
$db = mysqli_connect("localhost", "username", "password", "database");
if (!$db) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
exit;
} else {
echo "Connected to MySQL<br>";
}
Warning: mysqli_connect(): (HY000/1045): Access denied for user
'usr'#'localhost' (using password: YES) in
/storage/emulated/0/htdocs/includes/condb.php on line 9 Failed to
connect to MySql: Access denied for user 'usr'#'localhost' (using
password: YES) Warning: mysqli_query() expects parameter 1 to be
mysqli, boolean given in /storage/emulated/0/htdocs/includes/condb.php
on line 19
Warning: mysqli_error() expects parameter 1 to be mysqli, boolean
given in /storage/emulated/0/htdocs/includes/condb.php on line 20
Unable to connect
Im trying to connect to the database using this code but im an unable to. I dont know what i actually did wrong. Ive tried googling and reading some articles but none of those work.
UPDATED CODE still getting the error message above.
$server = "127.0.0.1"; $username = "usr"; $password = "123"; $database
= "contents";
$conn = mysqli_connect($server,$username,$password,$database);
if(mysqli_connect_errno($conn)) {
echo "Failed to connect to MySql: ".mysqli_connect_error(); }
$sql = "INSERT INTO contents('subj','article','day') VALUES
('".$_POST['This is the subj']."', '".$_POST['This is the
content']."', '".$_POST['2019-02-01 10:15:59']."')";
if(!mysqli_query($conn,$sql)) {
die("Unable to connect".mysqli_error($conn)); }else {
echo "Connected"; }
?>
Still getting the same error above unfortunately it was not a typo error. Still couldn't get the solution to this problem it has been days. Can someone with problem solving skills help me.
You don't have to pass $conn inside mysqli_connect_errno()
remove $conn
$conn = mysqli_connect($server,$username,$password,$database);
if(mysqli_connect_errno()) {
echo "Failed to connect to MySql: ".mysqli_connect_error();
}
You can fix this error by changing your mysql username and password. There are many tutorials for changing mysql user and password with a little search
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
I am trying to make a registration page where it adds the information to a SQL Table. Here is my PHP code to do this... Can anyone tell me what the error with this is?
<?php
$con=mysqli_connect("Correct Website is here","Correct Username","Correct Pass","Correct DB");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$DBsql = mysql_query("SELECT username, email from Profiles");
$sql = mysql_fetch_array($DBsql);
if(!($_POST['username'] == $sql['username']) && ($_POST['email'] == $sql['email'])) {
if ($_POST['password'] == $_POST['password2']){
$sql="INSERT INTO Profiles (firstName, lastName, username, email, password, region, group, verified)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[username]','$_POST[email]','$_POST[password]','$_POST[region]','member','no')";
}else{
echo '<script type="text/javascript">'
, 'window.alert("The passwords do not match.")'
, '</script>';
}
} else {
echo '<script type="text/javascript">'
, 'window.alert("That username or email is already in use.")'
, '</script>';
}
mysqli_close($con);
?>
It's giving these errors:
Warning: mysqli_connect() [function.mysqli-connect]: (HY000/1130): Host '31.170.161.96' is not allowed to connect to this MySQL server in /home/a5349216/public_html/register.php on line 2
Failed to connect to MySQL: Host 'ip' is not allowed to connect to this MySQL server
Warning: mysql_query() [function.mysql-query]: Access denied for user 'username'#'localhost' (using password: NO) in /home/username/public_html/register.php on line 8
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/username/public_html/register.php on line 8
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/username/public_html/register.php on line 9
Warning: mysqli_close() expects parameter 1 to be mysqli, boolean given in /home/username/public_html/register.php on line 27
If your mysql database is not on the same server(localhost) you need to give it access from a remote server for that mysql user/database for your server. See this article for some steps that might help you. It sounds like it might be remote.
https://support.rackspace.com/how-to/mysql-connect-to-your-database-remotely/
Create new php file, just test with below syntax. let me know your error.
$con=mysqli_connect("Correct Website is here","Correct Username","Correct Pass","Correct DB");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
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.