Error establishing a database connection - wordpress-issues

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

40 Replies - 1294 Views - Last Post: 28 June 2019 - 01:32 PM Rate Topic: -----

#16 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3822
  • View blog
  • Posts: 13,909
  • Joined: 08-August 08

Re: Error establishing a database connection - wordpress-issues

Posted 11 June 2019 - 07:54 PM

Hopefully, mysql_connect does not exist. It has been removed from modern PHP. If it does exist then you need to upgrade your PHP version and learn to use PDO or MySQLi.
Was This Post Helpful? 0
  • +
  • -

#17 sayhello   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 12-November 17

Re: Error establishing a database connection - wordpress-issues

Posted 15 June 2019 - 06:06 AM

many many thanks for the quick reply - 


well - i guess that i need to run the following command: mysqli_connect_error (PHP 5, PHP 7)

https://www.php.net/...nnect-error.php
mysqli::$connect_error -- mysqli_connect_error — Returns a string description of the last connect error


Object oriented style

string $mysqli->connect_error;
Procedural style

mysqli_connect_error ( void ) : string
Returns the last error message string from the last call to mysqli_connect().

Return Values ¶
A string that describes the error. NULL is returned if no error occurred.

Examples ¶
Example #1 $mysqli->connect_error example

Object oriented style

Quote

<?php

$mysqli = @new mysqli('localhost', 'fake_user', 'my_password', 'my_db');

// Works as of PHP 5.2.9 and 5.3.0.
if ($mysqli->connect_error) {
    die('Connect Error: ' . $mysqli->connect_error);
}
?>


Procedural style

Quote

<?php
$link = @mysqli_connect('localhost', 'fake_user', 'my_password', 'my_db');

if (!$link) {
    die('Connect Error: ' . mysqli_connect_error());
}
?>


The above examples will output:

Connect Error: Access denied for user 'fake_user'@'localhost' (using password: YES)
Was This Post Helpful? 0
  • +
  • -

#18 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2331
  • View blog
  • Posts: 7,107
  • Joined: 15-January 14

Re: Error establishing a database connection - wordpress-issues

Posted 17 June 2019 - 10:34 AM

Well, there's your answer. See, it's much better to read and understand the error messages than trying to guess.
Was This Post Helpful? 1
  • +
  • -

#19 sayhello   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 12-November 17

Re: Error establishing a database connection - wordpress-issues

Posted 17 June 2019 - 09:52 PM

Hello dear ArtificalSoldier, hello dear no2pencil, hi modi123_1, hello DTphpwb


first of all many many thanks for the continued help and support. I am trying to figure out what has happened.
a big big sorry - for the bit of confusing thread-posting with the issues. I am in need of help and i try to sort out things here.


View PostArtificialSoldier, on 17 June 2019 - 10:34 AM, said:

Well, there's your answer. See, it's much better to read and understand the error messages than trying to guess.




i am tryin to understand what goes on...
in order to do that i recapitulate what i have done..:
- i runned two differnt versions of scripts to test the connection

a. one with mysql and
b. one with mysqli


i had several results - and now i have to interpret these results correcly



Quote

The "no such file or directory" message is also related to trying to connect to MySQL, that is related to how
the mysql functions try to connect.I also just noticed that for some reason you've switched to the mysql extension from the mysqli extension. Don't do that, either use mysqli or PDO. The regular mysql extension is no longer part of PHP.
Just trust the error messages. This entire time the one, single problem you have is that PHP cannot
connect to MySQL, but you're assuming it's 10 different things wrong and trying to solve other things
that aren't a problem. So, why can't you connect? Did you change the default port for MySQL?



i understand that you want me to run the testscript that is written below.

Quote

Well, there's your answer. See, it's much better to read and understand the error messages than trying to guess.



https://www.php.net/...nnect-error.php


well - i guess that i need to run the following command: mysqli_connect_error (PHP 5, PHP 7)

https://www.php.net/...nnect-error.php
mysqli::$connect_error -- mysqli_connect_error — Returns a string description of the last connect error


Object oriented style

string $mysqli->connect_error;
Procedural style

mysqli_connect_error ( void ) : string
Returns the last error message string from the last call to mysqli_connect().

Return Values:

A string that describes the error. NULL is returned if no error occurred.

Examples:
Example #1 $mysqli->connect_error example

Object oriented style


<?php

$mysqli = @new mysqli('localhost', 'fake_user', 'my_password', 'my_db');

// Works as of PHP 5.2.9 and 5.3.0.
if ($mysqli->connect_error) {
    die('Connect Error: ' . $mysqli->connect_error);
}
?>





Procedural style


<?php
$link = @mysqli_connect('localhost', 'fake_user', 'my_password', 'my_db');

if (!$link) {
    die('Connect Error: ' . mysqli_connect_error());
}
?>




The above examples will output:


Connect Error: Access denied for user 'fake_user'@'localhost' (using password: YES) 





i do that...

... dear ArtificalSoldier, dear no2pencil, modi123_1, DTphpwb - i am happy that you help me to figure out the issues.

many many thanks

i will run the script and come back and report all the findings.


untill later...

This post has been edited by sayhello: 18 June 2019 - 04:43 PM

Was This Post Helpful? 0
  • +
  • -

#20 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2331
  • View blog
  • Posts: 7,107
  • Joined: 15-January 14

Re: Error establishing a database connection - wordpress-issues

Posted 18 June 2019 - 10:39 AM

You really, really need to pay attention to the error message. It's telling you exactly what the problem is:

Quote

Connect Error: Access denied for user 'fake_user'@'localhost' (using password: YES)

What do you think that means when it tells you access denied for a particular user using a password?
Was This Post Helpful? 0
  • +
  • -

#21 sayhello   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 12-November 17

Re: Error establishing a database connection - wordpress-issues

Posted 22 June 2019 - 04:15 AM

hello dear all hello Artificalsoldier, Modi123.. and dear all friends,


runned this code 

   
    <?php
    $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
    
    /*
     * This is the "official" OO way to do it,
     * BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
     */
    if ($mysqli->connect_error) {
        die('Connect Error (' . $mysqli->connect_errno . ') '
                . $mysqli->connect_error);
    }
    
    $mysqli->close();
    

    

and got back this 

   
Parse error: syntax error, unexpected end of file in /sites/www.my-site.de/new_test.php on line 13
    



well what happened? i tryto figure it out
Was This Post Helpful? 0
  • +
  • -

#22 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3822
  • View blog
  • Posts: 13,909
  • Joined: 08-August 08

Re: Error establishing a database connection - wordpress-issues

Posted 22 June 2019 - 05:15 AM

There is a hidden character after the semicolon you posted here
    $mysqli->close();


Was This Post Helpful? 0
  • +
  • -

#23 sayhello   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 12-November 17

Re: Error establishing a database connection - wordpress-issues

Posted 22 June 2019 - 06:14 AM

and besides the php-configuration 



if i use code with a closing tag -  like so... .




<?php
$mysqli = new mysqli('localhost', 'jo', 'susi19', 'jo');

/*
 * This is the "official" OO way to do it,
 * BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
 */
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error)
}

$mysqli->close();

?>






then i get back 



Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /sites/www.my_page.org/new_test.php on line 2
Connect Error (2002) No such file or directory






still wonder what is going on here . and what i can do and test now
Was This Post Helpful? 0
  • +
  • -

#24 sayhello   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 12-November 17

Re: Error establishing a database connection - wordpress-issues

Posted 22 June 2019 - 07:31 AM

Hello dear ArtificalSoldier, hello dear no2pencil, hi modi123_1, hello DTphpwb


many many thanks for the continued help and support. I am trying to figure out what has happened.

see even more - i have runned several code examples - also this one....:

<?php
$mysqli = new mysqli("localhost", "db-user", "passwd", "db-name");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

if (!$mysqli->query("SET a=1")) {
    printf("Errormessage: %s\n", $mysqli->error);
}

/* close connection */
$mysqli->close();
?>






Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /sites/www.job-starter.de/tt.php on line 2
Connect failed: No such file or directory




well this is really funny...
Was This Post Helpful? 0
  • +
  • -

#25 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3822
  • View blog
  • Posts: 13,909
  • Joined: 08-August 08

Re: Error establishing a database connection - wordpress-issues

Posted 22 June 2019 - 07:54 AM

Try changing "localhost" to "127.0.0.1"
https://www.fatalerr...-directory.html

I find PDO to be much easier to deal with than MySQLi.
Was This Post Helpful? 0
  • +
  • -

#26 sayhello   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 12-November 17

Re: Error establishing a database connection - wordpress-issues

Posted 22 June 2019 - 09:34 AM

dear ArtificalSoldier, hello dear no2pencil, hi modi123_1, hello CTphpnwb

@CTphpnwb: many many thanks for the qick reply.

View PostCTphpnwb, on 22 June 2019 - 07:54 AM, said:

Try changing "localhost" to "127.0.0.1"
https://www.fatalerr...-directory.html
I find PDO to be much easier to deal with than MySQLi.


good idea: foods for thought.

a. i will try the changing "localhost" to "127.0.0.1"
b. i have no experience with PDO but i will dig deeper into all that. I am going to read the docs.


above all: well - this is so crazy and i think that there some kind of magic things are happening.

btw: the wordpress-experts told me that i should not use 127.0.0.1 instead of localhost for the servername.
But while is constantly going wrong and does not work at all i am musing about using 127.0.0.1 instead of localhost as the server name.

Besides that i think that there might be some more issues:


i have to do some more checks

- is there probably some firewalls in between the MySQL process and the network-stack and if it isn't that,
- i might have a closer look at the editing of mysqli.default_socket in php.ini and i need to check that the path is set correct.


@CTphpnwb - many many thanks for the qick reply.
i do the change of "localhost" to "127.0.0.1" and report all the findings.

Do you have and additional ideas what i can do and test!?
Was This Post Helpful? 0
  • +
  • -

#27 sayhello   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 12-November 17

Re: Error establishing a database connection - wordpress-issues

Posted 23 June 2019 - 07:36 AM

Hello dear ArtificalSoldier, hello dear no2pencil, hi modi123_1, hello DTphpwb
hello dear all,


well - first of all many many thanks for the continued help and support. I am trying to figure out what has happened. i am very glad to be here on this great place. 
a big big sorry - for the bit of confusing thread-posting with the issues. I am in need of help and i try to sort out things here.


Well after futher tests i am pretty clueless.

some first statements: 

i am on

PHP Version 5.6.39

btw: I have Linux Server - based on opensuse: 

Apache 2.4.10, PHP Version 5.6.39 and
mysqlnd 5.0.11-dev - 20120503 
the newest version of Webadmin - Webmin 1.910
see http://www.webmin.com/


i have testesd varios versions of the mysqli.connect-error-scripts...


see the results: 


https://www.php.net/...nnect-error.php

mysqli::$connect_error
mysqli_connect_error
(PHP 5, PHP 7)


Example #1 $mysqli->connect_error example

Object oriented style

<?php
$mysqli = @new mysqli('localhost', 'fake_user', 'my_password', 'my_db');

// Works as of PHP 5.2.9 and 5.3.0.
if ($mysqli->connect_error) {
    die('Connect Error: ' . $mysqli->connect_error);
}
?>





i also runned the prozedural-style 

<?php
$link = @mysqli_connect('localhost', 'fake_user', 'my_password', 'my_db');


if (!$link) {
    die('Connect Error: ' . mysqli_connect_error());
}
?>





and got back the following:
Connect Error: No such file or directory 


note: i also runned this with the adviced replacement of localhost with 127.0.0.1  ... see below: 



<?php
$link = @mysqli_connect(''127.0.0.1'', 'user', 'db-passwd', 'db-name');

if (!$link) {
 die('Connect Error: ' . mysqli_connect_error());
}
?>






and i got back here:
Connect Error: No such file or directory 


...and here we have more insights  - i also runned the PDO-version: 

see: https://www.w3school...sql_connect.asp




<?php
$servername = "localhost";
$username = "username";
$password = "password";



try {
 $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    
// set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  

  echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
 echo "Connection failed: " . $e->getMessage();

    }
?>




see what i have got back:

Connection failed: SQLSTATE[HY000] [2002] No such file or directory





Note: In the PDO example above we have also specified a database (myDB). PDO require a valid database to connect to.
If no database is specified, an exception is thrown.
Tip: A great benefit of PDO is that it has an exception class
to handle any problems that may occur in our database queries. If an exception is thrown within the try{ } block,
the script stops executing and flows directly to the first catch(){ } block.

cf. https://www.w3school...sql_connect.asp

conclusio: all attemts to set up a mysql-db that
is accessible from a Wordpress-installation failed.

some more ideas that i need to think about. 



i should do some more tests and i will try to change "localhost" to "127.0.0.1


see https://www.fatalerr...-directory.html


i further have to test  PDO it is
told to be much easier to deal with than MySQLi.

what i need to do:

a.
i also have tried the changing "localhost" to "127.0.0.1"
b. i have no experience with PDO but
i will dig deeper into all that. I am going to read the docs. 
But see - i allready have done a first test with PDO 



above all: well - this is so crazy and i think that
there some kind of magic things are happening.

i have to do some more checks:
-
is there probably some firewalls in between the MySQL process and the network-stack and if it isn't that,
-
i might have a closer look at the editing of mysqli.default_socket in php.ini
and i need to check that the path is set correct.



i need to do more tests and report all the findings.

but at the moment i am a bit clueless...
 

What can i do now!? 
Was This Post Helpful? 0
  • +
  • -

#28 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3822
  • View blog
  • Posts: 13,909
  • Joined: 08-August 08

Re: Error establishing a database connection - wordpress-issues

Posted 23 June 2019 - 07:57 AM

$link = @mysqli_connect(''127.0.0.1'', 'user', 'db-passwd', 'db-name');

What's with the extra quotes?

This post has been edited by CTphpnwb: 23 June 2019 - 07:59 AM

Was This Post Helpful? 0
  • +
  • -

#29 sayhello   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 12-November 17

Re: Error establishing a database connection - wordpress-issues

Posted 23 June 2019 - 05:16 PM

good evening dear CTphpnwb


first of all - many many thanks for the reply. i am very glad to be here on this great place. 


regarding your Question: the extra quotes are not in the code - in other words they are not effective ( in the code) ...


here some data statements: i am on  
SuSe-Linux 12.3
Webmin Authentic Theme 19.33

PHP Version 5.6.39 - [ note - this is just very old - i should tell my admin that he can update this]
Build Date    Dec 26 2018 22:47:34 

mysql.default_socket    /var/lib/mysql/mysql.sock    /var/lib/mysql/mysql.sock
mysqli.default_socket    /var/lib/mysql/mysql.sock    /var/lib/mysql/mysql.sock


PDO - settings
PDO support    enabled
PDO drivers    mysql, sqlite
pdo_mysql


pdo_mysql
PDO Driver for MySQL    enabled
Client API version    mysqlnd 5.0.11-dev - 20120503 - $Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $
Directive    Local Value    Master Value
pdo_mysql.default_socket    /var/lib/mysql/mysql.sock    /var/lib/mysql/mysql.sock
pdo_sqlite
PDO Driver for SQLite 3.x    enabled
SQLite Library    3.8.10.2



settings and tests https://www.w3schools.com/php/php_mysql_connect.asp

PDO support    enabled
PDO drivers    mysql, sqlite
pdo_mysql
PDO Driver for MySQL    enabled
Client API version    mysqlnd 5.0.11-dev - 20120503 - $Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $
Directive    Local Value    Master Value
pdo_mysql.default_socket    /var/lib/mysql/mysql.sock    /var/lib/mysql/mysql.sock
pdo_sqlite
PDO Driver for SQLite 3.x    enabled
SQLite Library    3.8.10.2



see more 
<?php
$servername = "localhost";
$username = "jc";
$password = "my password";

try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?>





result: Connection failed: SQLSTATE[HY000] [2002] No such file or directory


btw;: Chrome-brwoser tells this page is not secure... 
#hmm - should i do some settings... - it is quite a bit intersting that the chrome-browser telling us that this page is not secure... 

define("HOST",'localhost');
define("USERNAME",'?');
define("PASSWORD",'?');

    $db = new PDO("mysql:host=".HOST,USERNAME,PASSWORD);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$res = $db->query("SELECT 7 * 6 ");
echo $res ->fetchColumn();          //--> 42
Of course it would fail if I tried to access columns from a table (in an unspecified DB)



see the result: 
define("HOST",'localhost'); define("USERNAME",'jo'); define("PASSWORD",'my password'); $db = new PDO("mysql:host=".HOST,USERNAME,PASSWORD); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $res = $db->query("SELECT 7 * 6 "); echo $res ->fetchColumn(); //--> 42




hmmm - what can i  do now !? 



love to hear from you

This post has been edited by sayhello: 24 June 2019 - 01:43 AM

Was This Post Helpful? 0
  • +
  • -

#30 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2331
  • View blog
  • Posts: 7,107
  • Joined: 15-January 14

Re: Error establishing a database connection - wordpress-issues

Posted 24 June 2019 - 11:00 AM

The "no such file or directory" error means that PHP cannot find the default mysql socket file.

https://stackoverflo...le-or-directory

That's kind of a weird error because before this you were getting the access denied error, which means that it could connect, but now it can't find the socket file to connect. You need to check your mysql configuration file to see where it's putting the socket file, and make sure that PHP is looking for it in the same place.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3