I have been developing my php mysql application locally and it worked fine with this connection parameters:
// Database connection params
define('HOST', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'some_db');
at some point I wanted to put application online with connection parameters:
define('HOST', 'somedomain.com');
define('USERNAME', 'some_db_admin');
define('PASSWORD', 'Password1!');
define('DATABASE', 'some_db');
When I try to connect to remote database like this:
function db_connect()
{
$connection = #mysql_connect(HOST, USERNAME, PASSWORD);
if(!$connection)
{
return false;
}
if(!mysql_select_db(DATABASE))
{
return false;
}
return $connection;
}
I get error:
Access denied for user 'some_db'#'localhost' (using password: NO)
How can it possibly be localhost?
I have regularly created database and user that database using using database wizard in cpanel.
What can be the problem?
Thanks a lot!
If your PHP code is at the same host where the MySQL database is then setting HOST to localhost should solve your problem
define('HOST', 'localhost');
It also seems that you swapped your constants, because some_db is not likely to be a user but rather a database. Try debugging and output the constants' values just before the 'mysql_connect' call.
If this does not work, try debug_backtrace().
Related
I have been building a website with PHP functionality in XAMPP, and everything works perfectly within the localhost. Although, I know that in order to have the same functionality on a live hosted server I will need to change the server info in my config.php file that is used:
<?php
define('ROOT_URL', 'http://localhost/newkellumws/');
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', 'austink01');
define('DB_NAME', 'kellumws');
I've tried changing the DB_HOST to my hosting profile's nameserver, but that did not work. Any help is much appreciated and thank you for your time. I apologize if the is a newbie question...
Assuming you are also moving the site as well the hostname will remain as localhost (as is the case with most shared hosting)
Assuming it's cPanel hosting you will first need to create the database.
You can then create an SQL account and grant it access to the DB (recommended) or use your cPanel credentials (Not Recommended)
So your config would look something like this:
define('ROOT_URL', 'http://example.com/newkellumws/');
define('DB_HOST', 'localhost'); // Website and SQL ruinning on the same server
define('DB_USER', 'exampl_kellumws');
define('DB_PASS', 'aBc*63oie8wfq');
define('DB_NAME', 'exampl_kellumws');
See https://documentation.cpanel.net/display/68Docs/MySQL+Databases
if the site is still going to be run via XAMPP (For whatever reason) you would also need to allow Remote MySQL
See https://documentation.cpanel.net/display/68Docs/Remote+MySQL
So your config would instead look something like this:
define('ROOT_URL', 'http://example.com/newkellumws/');
define('DB_HOST', 'c01.example.host'); //Address the SQL Server
define('DB_USER', 'exampl_kellumws');
define('DB_PASS', 'aBc*63oie8wfq');
define('DB_NAME', 'exampl_kellumws');
If you still have issues, contact your hosting provider as they will know the server setup and requirements.
You can also create testConnection.php with the following to help diagnose errors
<?php
require_once('path/to/file/with/config.php');
//Step-1 : Create a database connection
$connection=mysql_connect(DB_HOST,DB_USER,DB_PASS);
if(!$connection) {
die(“Database Connection error: ” . mysql_error());
}
//Step-2 : Select a database to use
$db=mysql_select_db(DB_NAME,$connection);
if(!$db) {
die(“Database Selection error” . mysql_error());
}
echo('Connected to Database');
Your server infos and local XAMP are different localhost works like following.
You dont need url part as its localhost/your_folder
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', ''); //Xamp doesnt have a password for root as default leave it empty if you didnt set one.
define('DB_NAME', 'kellumws');
define('PORT', '3306');
latest version of XAMP request port for MySql port, add it into your connection. mostly its 3306 as default but sometimes it changes to 3307 or 3308 because of mariaDB comes as default you can see real path on XAMP tool.
Than just call http://localhost/newkellumws/ if you created a folder.
I am using windows and trying to develop an app in php where I connect a database mySQL created on mySQL workbench. I connect with root and a saved pass. But it seems I cannot connect to the page afterwards.
I use Windows 10.
<?php
define('db_user', 'what is the user?');
define('db_pass', 'mypass');
define('db_host', 'localhost');
define('db_name', 'movies');
&db_conn = mysqli_connect(db_host, db_user, db_pass, db_name);
if(!&db_conn){
die('error connecting to database');
}
echo 'you have connected succesfully';
?>
My database is called movies. I don't know what is the user?
phpMyAdmin is not a database; it is just one of the tools you use to manage what is inside. Your database is MySQL, which should be started and listening on a particular port. The default is usually 3306 or for MariaDB 3307. When MySQL server is located on the same machine as your web server, you can connect via localhost or 127.0.0.1.
If you haven't set up any users in you MySQL server, then the default usually is root with no password. Check your users using this command:
SELECT * FROM mysql.user;
The user must also be authorized for an access via either localhost or IP, whichever you use.
Then to open a connection to MySQL in PHP using the mysqli class, you would usually use this code:
<?php
// your connection code
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'root', '', 'movies');
$mysqli->set_charset('utf8mb4');
I am trying to configure my Wordpress site in the Google App Engine standard environment. I have configured a Cloud SQL for MySQL Second Generation instance and can access it using Cloud SQL Proxy.
The problem I have is connected to the Cloud SQL instance after the app has been deployed to the Google App Engine (GAE) environment. Here are two different connection strings; one for the GAE environment and another for the local environment.
if (isset($_SERVER['GAE_ENV'])) {
$dbConn = mysqli_connect (null, DB_USER, DB_PASSWORD, DB_NAME, 3306, DB_SOCK);
} else { // local environment
$dbConn = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
}
Local environment
The connection string for the local environment works perfectly when I use these 4 parameters: DB_HOST, DB_USER, DB_PASSWORD, DB_NAME. When I try to use the same connection string with four parameters in the GAE environment, it throws an error:
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known
GAE Environment
In this question, I was told to call mysqli_connect using all 6 of the optional parameters, with null for parameter 1 and 3306 for parameter 5, being the port. When I try to make the connection using the GAE connection string with these 6 parameters: null, DB_USER, DB_PASSWORD, DB_NAME, 3306, DB_SOCK, I get a slightly different error:
Fatal error: Uncaught mysqli_sql_exception: No such file or directory
Here is the code from wp-config.php which sets the connection string variables depending on the environment:
if ($onGae) {
/** GAE Environment */
define('DB_NAME', 'database');
define('DB_HOST', ':/cloudsql/project_id:region:instance_id');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
} else {
/** Local environment */
define('DB_NAME', 'database');
define('DB_HOST', '127.0.0.1');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
}
No matter what I try, I can't seem to get mysqli_connect to connect to CloudSQL from GAE. Can someone please tell me what I am doing wrong? Thank you.
I suspect the problem is the colon in the path of your socket is preventing it from recognizing it is an absolute path. Try define('DB_HOST', '/cloudsql/<project_id:region:instance_id>'); instead.
A couple of other tips: You can use the Cloud SQL proxy to create a local unix socket at /cloudsql/<CONNECTION_NAME>, which means you can test the same code deployed and locally.
Also as general programming advice, I would try to avoid using DB_HOST for different types of values (because they really aren't the same). This will help you from inadvertently using $DB_HOST somewhere else and expecting an IP address. Try something like this instead:
define('DB_NAME', 'database');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
if ($onGae) {
/** GAE Environment */
define('DB_HOST', null);
define('DB_SOCKET', '/cloudsql/project_id:region:instance_id');
} else {
/** Local environment */
define('DB_HOST', '127.0.0.1');
define('DB_SOCKET', null);
}
While trying install wordpress i recieve following error
I use wampserver i have changed wp-config.php file and now it looks like this:
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'root');
/** MySQL database password */
define('DB_PASSWORD', 'root');
/** MySQL hostname */
define('DB_HOST', 'localhost');//i used 127.0.0.1 too but no matter
define('WP_DEBUG', true);
wp-db.php file:
1379 line:
mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
1409 line:
$this->dbh = mysqli_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
but still no result I do not know what to do can anyone help me?
if you are using default configuration of xampp/wamp try with
define('DB_PASSWORD', ''); // for localhost
generally localhost db config:-
host = localhost
user = root
password = '' (blank)
Is root really the MySQL user you want to connect as? Is root its password?
It's usually not necessary and from a security standpoint potentially very dangerous to let web applications use your MySQL root user. Create a new user for your WordPress instance, give it its own database:
CREATE DATABASE wordpress;
GRANT ALL PRIVILEGES ON wordpress.* TO blog#localhost IDENTIFIED BY '3AmAbsKCsrKDw':
Then use that user
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'blog');
/** MySQL database password */
define('DB_PASSWORD', '3AmAbsKCsrKDw');
/** MySQL hostname */
define('DB_HOST', 'localhost');
define('WP_DEBUG', true);
That should let you connect. Verify that you can connect with those login credentials before continuing to your WordPress setup. WAMP includes a phpMyAdmin interface as far as I know, so try those credentials there.
Update your wp-config.php file like this
define your database name when you create on your localhost or server
define('DB_NAME', 'Your database name');
Ex:- You create your database name like "wordpress" then put database name like
define('DB_NAME', 'wordpress');
If you run your project in local always put root and when you upload database in your server you put your database user name when you create database
define('DB_USER', 'root');
Put your password when you create database on server other wise you can put ''
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
define('WP_DEBUG', true);
I tried to connect to my DB server using the hostname and the port in the file config.php but still give me error, I won't use the localhost server, the database is in other server.
I get the hostname using : show variables like '%hostname%'
and get the port using : SHOW VARIABLES WHERE Variable_name = 'port'
define('DB_USER', "test");
define('DB_PASSWORD', "test");
define('DB_DATABASE', "test");
define('DB_SERVER', "My problem is here");
someone know what I did wrong?
thank you
Some hosts, you can't connect to their database from your localhost, but only from their webserver.
Maybe thats your problem ?