• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

RESOLVED Cannot Connect to mysql database

Messages
6
Likes
0
Points
1
#1
<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_SERVER', 'server name');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'database name');

/* Attempt to connect to MySQL database */
$con = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

// Check connection
if($con === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>



I have the above code which works on the server if I put the php files separately. However, I when I try to put this on a woody snippet its gives an error as below.

Don't Panic
The code snippet you are trying to save produced a fatal error on line 9:
mysqli_connect(): (28000/1045): Access denied for user 'nanosoft_Sam'@'mocha3027.mochahost.com' (using password: YES)
The previous version of the snippet is unchanged, and the rest of this site should be functioning normally as before.
Please use the back button in your browser to return to the previous page and try to fix the code error. If you prefer, you can close this page and discard the changes you just made. No changes will be made to this site.

I contacted hosting company and they asked me to talk to the plugin guys. Because server is working.


CAN SOMEONE PLEASE HELP ME I AM SO STUCK.
 

Temyk

Developer & Support
Messages
1,129
Likes
42
Points
48
#2
Hello.
If you want to connect to the database of your Wordpress, you have incorrectly specified global variables, and you do not need to declare them, they are already declared in the wp-config.php. Here is the correct code:
PHP:
<?php

/* Attempt to connect to MySQL database */
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

// Check connection
if($con === false){
 die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>
 
Messages
6
Likes
0
Points
1
#3
Hi Temyk,

Thank you so much for the reply, now I understand the system. One more thing, What is I want to connect to a database created by myself other than the database which is on the wp-config.php file by wordpress. Is there any way to add my database credentials to the wp-config.php file also?
 

Temyk

Developer & Support
Messages
1,129
Likes
42
Points
48
#4
Just specify the data to connect to your database. It is not necessary to use parameters from wp-config.php
PHP:
mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
 
Messages
6
Likes
0
Points
1
#5
Yes thank you very much and one last question.... in my php pages i have included the file which has database connection details,
like below.

// Include config file
require_once "config.php";

So in the Config.php has the connection string.
I just need the connection string at one place.
How do I achieve this in this plugging.

I tried to used "Run Everywhere" option in the plugin and its not working.

But as you said first, I tried putting connection string in normal pages and it works.. Please could you tell me how to put the connection string at a global place so i dont have to write it in all my php pages. Thanksss
 

Temyk

Developer & Support
Messages
1,129
Likes
42
Points
48
#6
Snippet with declaration of database connection parameters (Run everywhere)
Constant names must be as unique as possible to avoid conflicts with other plugins
PHP:
define('SAMINDA_DB_HOST', 'server name');
define('SAMINDA_DB_USER', 'username');
define('SAMINDA_DB_PASSWORD', 'password');
define('SAMINDA_DB_NAME', 'database name');
At the beginning of each snippet:
PHP:
$con = mysqli_connect(SAMINDA_DB_HOST, SAMINDA_DB_USER, SAMINDA_DB_PASSWORD, SAMINDA_DB_NAME);
 
Messages
6
Likes
0
Points
1
#7
Oh thank you so very much I was struggling to achieve this for weeks now since i'm new to WordPress. Thank you so much for your highly professional help.