• 0

from MySQL, MySQLi or PDO and mysqli::$connect_error - the script for errors in the db-connection


Question

hello dear experts -. 

 

good day @all - my name is tarifa - i am pretty new to this forum. 

 

 

well today i need your help in a wordpress issue. 

i thought that during the installation process the file wp-config-sample.php gets to wp-config.php - but this is not happening.  - i have gathered some more infos and inights due to some tests. see the following data: - the outcome of a testscript: 

Warning: mysqli_connect(): (HY000/2002): No such file or directory in /sites/www.mysite.de/tests.php on line 3
Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in /sites/www.mysite.de/tests.php on line 4
could not connect

see the script; 

 

<?php
if(function_exists('mysqli_connect')){
if(!($link = mysqli_connect('localhost','user','passwd','my_db'))){
die('could not connect: ' . mysqli_error($link));
}
} else {
die("don't have mysqli");
}
echo 'connect successfully';
mysqli_close($link);


on the webmin-frontend (that is the area i have to controll and configure all the stuff on the server')

1 USE yourdatabase;
2 SHOW GLOBAL VARIABLES LIKE 'PORT';
3 SHOW GRANTS FOR CURRENT_USER;


i get back the following results: 

check the manual that corresponds to your MySQL server version for the right syntax to use near ''name of my db';
SHOW GLOBAL VARIABLES LIKE 'PORT'; SHOW GRANTS FOR CURRENT_USER' at line 1

and the following

Failed to execute SQL : SQL USE 'name of my db'; SHOW GLOBAL VARIABLES LIKE 'PORT';
SHOW GRANTS FOR CURRENT_USER; failed : You have an error in your SQL syntax;

 

 

well - i am a bit unsure what i can do now.  

 

well - since there is no connection_error property in mysqli. i think that i have to check connection error using 

 

if ($conn->connect_error) {
    die('Connect Error: ' . $conn->connect_error);
}

well i have to read the manpages here http://php.net/manual/en/mysqli.connect-error.php in order to learn what i need to get more insights., 


btw:  I have Linux Server. Apache 2.4.10, PHP Version 5.6.39 and mysqlnd 5.0.11-dev - 20120503 - installed. There are several wordpress website running on server.

do i have MySQL or MySQLi  - i am not sure. 

 

what would you do now!?


by the way: just another question.... regarding the  different systems in DB-terms:  we have got different systems that are available generally... 

MySQL - MySQLI and PDO . if we compare the different systems then we can say. 

 

a. The now extinct mysql library and the mysqli library are two completely different animals.
b.   we may forget about mysqli - since PDO is much much better - so we should probably make use of PDO.

 

see the three systems in a ASCII - Comparison..:

+---------+-------------+
|  mysql  |  dodo       |
|  mysqli |  donkey     |
|  PDO    |  racehorse  |



what do you suggest - should i ask my serveradmin that he will update & upgrade the whole system - in order to have a modern system... And then i run the mysqli_error_(function)

Love to hear from you

regards Tarifa

 

 

6 answers to this question

Recommended Posts

  • 0

Are you sure you set up the MySQL use to beable to access the database via localhost?

 

Try using the following code to connect to the DB

 

<?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();

 

  • 0
  On 20/06/2019 at 13:40, Matthew S. said:

Are you sure you set up the MySQL use to beable to access the database via localhost?

 

Try using the following code to connect to the DB

 

<?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();

 

Expand  

 

got back this  result ... 

 

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

  • 0

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

  • 0

Try using 127.0.0.1 instead of localhost as the server name.

 

Are you sure that there are no firewalls in between the MySQL process and the network stack?

 

If it isn't that, have a look at editing mysqli.default_socket in php.ini and check that the path is correct.

  • 0

Hello dear C;Amie

 

many many thanks for the  quick reply. Thats great to hear from you. 


 

  Quote

 

Try using 127.0.0.1 instead of localhost as the server name.

 

 

 

Expand  

 

that i will do right now...

 

  Quote

 

Are you sure that there are no firewalls in between the MySQL process and the network stack?

 

 

 

Expand  

 

well i am not 100 % sure . i have to re check all this settings. 

 


 

  Quote

 

If it isn't that, have a look at editing mysqli.default_socket in php.ini and check that the path is correct.

 

 

 

Expand  

 

i will talk to my serveradmin - (a friend of me) and i will surely discuss all these ideas. 

 

dear C;Amie - i am very very glad to hear from you - i am so glad. This annoying  error is still keeping me worrying one whole week now.  - i need to get this fixed. 

 

Dear C;Amie many many thanks for your hints.  I am trying to figure out what has happened. 

By the way: 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... 

 

now i am trying to follow all your hints of the previous post - CTAmie i am glad that you want to help me..

 

regards tarifa

Edited by tarifa
  • 0

 

hello dear 😄 Amie and dear 'Matthew, 

 

first of all - many many thanks for the quick reply - i have tried out several things so far.. 

 

 

 

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: ave tr


https://www.php.net/manual/en/mysqli.connect-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.w3schools.com/php/php_mysql_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.w3schools.com/php/php_mysql_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.fatalerrors.org/a/warning-mysqli-mysqli-hy000-2002-no-such-file-or-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.

and report all the findings. 

 

What can i do now!? 

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Get Windows 11 Home or Pro for only $9.97 with this coupon by Steven Parker Today's highlighted deal comes via our Apps + Software section of the Neowin Deals store, where you can save up to 92% off on a Microsoft Windows 11 Home, or Pro license. Upgrade your computing experience with Windows 11 Pro. This cutting-edge operating system boasts a sleek new design and advanced tools to help you work faster and smarter. From creative projects to gaming and beyond, Windows 11 delivers the power and flexibility you need to achieve your goals. With a focus on productivity, the new features are easy to learn and use, enhancing your workflow and efficiency. Whether you're a student, professional, gamer, or creative, Windows 11 Home has everything you need to take your productivity to the next level. New interface. easier on the eyes & easier to use Biometrics login*.Encrypted authentication & advanced antivirus defenses DirectX 12 Ultimate. Play the latest games with graphics that rival reality. DirectX 12 Ultimate comes ready to maximize your hardware* Screen space. Snap layouts, desktops & seamless redocking Widgets. Stay up-to-date with the content you love & the new you care about Microsoft Teams. Stay in touch with friends and family with Microsoft Teams, which can be seamlessly integrated into your taskbar** Wake & lock. Automatically wake up when you approach and lock when you leave Smart App Control. Provides a layer of security by only permitting apps with good reputations to be installed Windows Studio Effects. Designed with Background Blur, Eye Contact, Voice Focus, & Automatic Framing Touchscreen. For a true mouse-less or keyboard-less experience TPM 2.0. Helps prevent unwanted tampering Windows 11 Pro also includes a number of productivity-focused features, such as the ability to snap multiple windows together and create custom layouts, improved voice typing, and a new, more powerful search experience. Personal and professional users will enjoy a modern and secure computing experience, with improved performance and productivity features to help users get more done. Only on Windows 11 Pro If you require enterprise-oriented features for your daily professional tasks, then Windows 11 Pro is a better option. Set up with a local account (only when set up for work or school) Join Active Directory/Azure AD Hyper-V Windows Sandbox Microsoft Remote Desktop BitLocker device encryption Windows Information Protection Mobile device management (MDM) Group Policy Enterprise State Roaming with Azure Assigned Access Dynamic Provisioning Windows Update for Business Kiosk mode Maximum RAM: 2TB Maximum no. of CPUs: 2 Maximum no. of CPU cores: 128 Good to know This license is for Windows 11 only. It is NOT intended to be used for upgrading Microsoft Office (MSO) included in Parallels Pro. However, it will still work with Parallels Pro and allow you to run Windows applications including MSO, but it DOES NOT include an upgrade MSO itself. It is still compatible with Microsoft Office ONLY if you have a separate license for it. Length of access: lifetime Redemption deadline: redeem your code within 30 days of purchase Access options: desktop Max number of device(s): 1 Version: Windows 11 Pro Updates included Click here to verify Microsoft partnership For example, a Microsoft Windows 11 Pro license normally costs $199, but you can pick it up for just $14.97 for a limited time, that represents a saving of $184. For a full description, specs, and license info, click the link below. Get Windows 11 Home or Pro for just $9.97 use MSO5 coupon when checking out Coupon expires on June 29. Although priced in U.S. dollars, this deal is available for digital purchase worldwide. We post these because we earn commission on each sale so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. Other ways to support Neowin Whitelist Neowin by not blocking our ads Create a free member account to see fewer ads Make a donation to support our day to day running costs Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: Neowin benefits from revenue of each sale made through our branded deals site powered by StackCommerce.
    • I hoped it was a bug and will be fixed. Why haven't they made so it asks for a PIN in the dark then? It tries to use the camera for several minutes or I have to manually select PIN and the next day in the morning I'll have to manually select the camera back, because it will just be sitting and waiting for a PIN. It just remembers the last used method regardless of the lighting.
    • I would recommend PowerToys just to remap a Copilot button on modern laptops for something more useful than Microsoft allows in Windows Settings. I also use some other tools like "Always on Top" and "Crop and Lock".
    • Makes sense, we know MS doesn't take dark mode seriously, lol.
    • Why would you think Johny Ives is behind this ? he has not worked at Apple or on a smart Phone or Phone in general for long time On 27 June 2019, Apple announced that Ive would depart Apple after 27 years
  • Recent Achievements

    • Veteran
      1337ish went up a rank
      Veteran
    • Rookie
      john.al went up a rank
      Rookie
    • Week One Done
      patrickft456 earned a badge
      Week One Done
    • One Month Later
      patrickft456 earned a badge
      One Month Later
    • One Month Later
      Jdoe25 earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      640
    2. 2
      ATLien_0
      275
    3. 3
      +FloatingFatMan
      171
    4. 4
      Michael Scrip
      155
    5. 5
      Steven P.
      138
  • Tell a friend

    Love Neowin? Tell a friend!