Connecting to MySQL database

Status
Not open for further replies.

NaK

New Member
Messages
9
Reaction score
0
Points
1
I uploaded my website but it doesn't work correctly. It uses a database, which I created, but my pages can't connect to the database. I have the database name, the user name and password and am using localhost as the host. Is there a different host definition?
 

NaK

New Member
Messages
9
Reaction score
0
Points
1
I checked everything, including the database password and it seems to be OK.
Which username do you need? for the account or the database?
The account is yolk100@gmx.com
The database is gsnevada_admin
 

lylex10h

Active Member
Messages
982
Reaction score
71
Points
28
What is the user name in your database connection string and what is the connection error?
 

NaK

New Member
Messages
9
Reaction score
0
Points
1
The database connection user name is gsnevada_admin. The password is !qazxsw@
I get a simple message (generated by the sql connector): Unable to connect.
 

lylex10h

Active Member
Messages
982
Reaction score
71
Points
28
Does the database user have all the permissions?
cPanel --> MySQL Databases --> Click on "gsnevada_admin" below Privileged Users.
 

lylex10h

Active Member
Messages
982
Reaction score
71
Points
28
Is the user assigned to the database and can you access the database with phpMyAdmin?
 

NaK

New Member
Messages
9
Reaction score
0
Points
1
Yes to both. I used NetObjects Fusion 12 to create the website and am now suspicious that the problem is with their database connector. When I try connecting with my own php mysql statement it seems to connect fine. I will look into this later and report.
 

NaK

New Member
Messages
9
Reaction score
0
Points
1
What is the host for the database? I used "localhost" but if it's different it would explain the trouble I'm having connecting.
 

lylex10h

Active Member
Messages
982
Reaction score
71
Points
28
This is the code in my wp-config.php file (database, user and password changed)

PHP:
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'my_database');

/** MySQL database username */
define('DB_USER', 'my_user');

/** MySQL database password */
define('DB_PASSWORD', 'my_p@$$W0rd');

/** MySQL hostname */
define('DB_HOST', 'localhost');
 

NaK

New Member
Messages
9
Reaction score
0
Points
1
It still can't connect. I checked the PHP MySql syntax and everything seems to be OK. The same script works on three other hosts. There muct be something strange in the PHP configuration.
 

lylex10h

Active Member
Messages
982
Reaction score
71
Points
28
I used the following test script and it worked:

PHP:
<?php
# Fill our vars and run on cli
# $ php -f db-connect-test.php
$dbname = 'name';
$dbuser = 'user';
$dbpass = 'pass';
$dbhost = 'localhost';
$link = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysqli_select_db($link, $dbname) or die("Could not open the db '$dbname'");
$test_query = "SHOW TABLES FROM $dbname";
$result = mysqli_query($link, $test_query);
$tblCnt = 0;
while($tbl = mysqli_fetch_array($result)) {
  $tblCnt++;
  #echo $tbl[0]."<br />\n";
}
if (!$tblCnt) {
  echo "There are no tables<br />\n";
} else {
  echo "There are $tblCnt tables<br />\n";
}
From https://gist.github.com/M165437/421cd2d23e53a111541a483971f7368b
 

NaK

New Member
Messages
9
Reaction score
0
Points
1
I'm using php 5.6:
mysql_connect("localhost", "admin", "password") or die('Error connecting to mysql');
mysql_select_db("databse_name") or die(mysql_error());
 

Anna

I am just me
Staff member
Messages
11,751
Reaction score
581
Points
113
You might want to switch over to mysqli or PDO for your connection, don't know by heart when it was removed but using mysql_xxx has been depreceted for a while. Simplest is to switch to mysqli_xxx, for most statements it is as easy as just changing mysql_connect to mysqli_connect etc. Some have minor syntax changes, such as mysql_connect() now takes four parameters, where the last one lets you select the database right away instead of using mysql_select_db() after the connection has been set up (you can still use that with mysqli as well though). I know there's a few more syntax changes too, but I haven't really had much reason to look them up.
 

NaK

New Member
Messages
9
Reaction score
0
Points
1
Problem solved. The website uses cookies and some of the database functions were set to refer to the cookies, which created a mess.
 
Status
Not open for further replies.
Top