I am quite new to the PHP world. I have installed MAMP on my Mac. This is the code I have written to connect to MySQL:
<?php
// Connect to the database server
$dbcnx = #mysql_connect("localhost", "root", “root”);
if (!$dbcnx) {
echo( "<P>Unable to connect to the " .
"database server at this time.</P>" );
exit();
}
?>
MySQL has already started and is running. I am not sure why am I not able to connect.
Try this
<?php
// Connect to the database server
$dbcnx = mysql_connect("localhost", "root", "");
if (!$dbcnx) {
echo( "Cannot connect to database due to ". mysql_error());
exit();
}
?>
Since you are getting Access denied for user 'root'#'localhost' (using password: YES) there is no need of giving password to connect with the database.So try with password ''.
Note:- mysql_* functions are deprecated as of PHP 5.5.0,and will be removed in the future.Instead, the MySQLi or PDO_MySQL extension should be used
Your code has smart quotes around the password. Try replacing them with regular quotes. If you have not changed the default MAMP password, this should work:
<?php
// Connect to the database server
$dbcnx = mysql_connect("localhost", "root", "root");
if (!$dbcnx) {
echo( "<P>Unable to connect to the " .
"database server at this time.</P>" );
exit();
}
?>
Related
I have a mysql database running on my local machine and I use wamp. I can login with phpmyadmin. Everything looks fine, create tables and users.
When I try to connect with php I get the message connection refused. The same when I try 127.0.0.1/connect.php in my browser. If I have a wrong user in my php file I get the message that user ist wrong.
Here my connect.php
<?php
$conn = mysqli_connect("127.0.0.1", "root", "", "users");
if ($conn) {
die('no connection: '. mysqli_connect_error());
}
echo 'connected';
mysqli_close($conn);
?
I tried everything, localhost, IP, 10.0.2.2, but nothing works. I also checked mysql settings and I read most of the posts here.
I solved the problem.
With this code it works perfect
<?php
$conn = new mysqli("127.0.0.1", "root", "", "users");
if (mysqli_connect_errno()) {
echo "no connection " . mysqli_connect_error();
}
echo 'conneted';
mysqli_close($conn);
?>
I am trying to write php to connect data from myqsl (i use free host 000webhost.com)
file connect:
<?php
$connect = mysqli_connect("files.000webhost.com:21", "nguyendang", "password", "foodcourt_db") or die ("could not connect to sever " . mysqli_connect_error()) ;
mysqli_query($connect, "SET NAMES 'utf8'");
?>
I had these errors when i ran
Try hostname with localhost. And also check the MySQL server is currently running.
This may happen in hosting for less duration due to overload of MySQL Server
Try to use this connection:
$link = mysqli_connect("localhost", "username", "password", "database_name");
//check connection
if($link === false) {
die("ERROR could not connect. " .mysqli_connect_error());
}
This question already has answers here:
SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES) (SQL: select * from `table`)
(2 answers)
Closed 2 years ago.
I tried connecting my PHP file to MySQli database but when i ran the code it displays
But when i logon to phpmyadmin it works fine no errors i can login to my account with no problems
Credentials in the database:
Connection Code:
<?php
$con = mysqli_connect('localhost','sotomango', '1234', 'mysql');
if(!$con) {
echo 'noooo';
}
?>
I double checked the database usernames, passwords, and privileges. Everything seems in place and correct, but the php file wont allow me to connect to the database.
#Dharman had a nice post but I couldnt find it.
Try this for debug :
$con = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$con) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is
great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($con) . PHP_EOL;
mysqli_close($con);
mysqli_connect
if its a new instalation or new upgrade, You need to include your port into connection, mariadb comes as default port so, when you try to connect mysql its redirecting to mariadb.
you need to include your correct port into connection it might be 3306, 3307 or 3308, and use ip instead of localhost.
your final codes should look like this :
$host = '127.0.0.1';
$db = 'test';
$user = 'root';
$pass = '';
$port = '3308';
$charset = 'utf8mb4';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
$mysqli = mysqli_connect($host, $user, $pass, $port, $db);
mysqli_set_charset($mysqli, $charset);
} catch (\mysqli_sql_exception $e) {
throw new \mysqli_sql_exception($e->getMessage(), $e->getCode());
}
For The People Wondering
I solved this by just adding a port to the host name like localhost:3308
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
I am trying to build a dynamic website for connecting pages with database I used the code as follows. connection with server is Ok but unable to select database. data base name, user id, password, ip of host all gave but not working. please help....
define('DB_NAME', '');
define('DB_USER', '');
define('DB_PASSWORD', '');
define('DB_HOST', '');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
else {
echo 'connected to server..................'; }
$db_selected = mysql_select_db($DB_NAME, $link);
if ($db_selected) {
print "Database Found";
}
else {
print "Database NOT Found";
}
Mysql has been deprecated and will eventually be removed. Consider using PDO or MySqli.
Here is a link to the PHP documentation page for making connections to a database using MySqli.
http://php.net/manual/en/mysqli.quickstart.connections.php
If you follow the instructions carefully you 'should' be able to connect. If not, perhaps you can post the error message you receive.
Try to give proper credential to connect to mysql server. mysqli_connect("localhost","root","password","db_name");