Jump to content
dil_bert

from MySQL to MySQLi - the script for errors in the db-connection

Recommended Posts

Posted (edited)

Hello dear experts and user of PHPFreaks, 

 

first of all: i have plenty of issues - a whole bunch of issues on a Server - i need to digg deeper into all things regarding creating a healthy db-connection.  Therefore i need to learn all about the script - mysql_connect_error... see below. 

 

i have gathered some information - in order to think about it - and to share the ideas ... so i hopefully will learn and solve my issues.

well the mysql_connect_error-script is a great help: It returns a string description of the last connect error ( see more infos here: https://www.php.net/manual/en/mysqli.connect-error.php ).  The mysqli_connect_error() function returns the error description from the last connection error, if there is  any error-note. 


the return value are the following ones:

a. A string that describes the error.
b. an empty string if no error occurred.

at least this goes for the Version: PHP 5, PHP 7


well - if we run the code below we can get the info bout the option to connect to the db.  What if we run this as a mysql-test-script, and what if we will want to convert it to use mysqli? Can this be done by changing mysql _query($sql); to mysqli _query($sql); ?

<?PHP

// the test-script that we are running.
$DB["dbName"] = "emails";
$DB["host"] = "localhost";
$DB["user"] = "root";
$DB["pass"] = "";
$link = mysql_connect($DB['host'], $DB['user'], $DB['pass']) or die("<center>Howdy - be aware; There a thing happenede - 
An Internal Error has Occured. Please report following error to the webmaster shot him a mail now.<br><br>".mysql_error()."'</center>");
mysql_select_db($DB['dbName']);
// end header connection part

// function from a functions file that I run a mysql query through in any page.
function executeQuery($sql) {
    $result = mysql_query($sql);
    if (mysql_error()) {
        $error = '<BR><center><font size="+1" face="arial" color="red">An Internal Error has Occured.<BR> The error has been recorded for review</font></center><br>';
        if ($_SESSION['auto_id'] == 1) {
            $sql_formatted = highlight_string(stripslashes($sql), true);
            $error .= '<b>The MySQL Syntax Used</b><br>' . $sql_formatted . '<br><br><b>The MySQL Error Returned</b><br>' . mysql_error();
        }
        die($error);
    }
    return $result;
}

// example query ran on anypage of the site using executeQuery function
$sql='SELECT auto_id FROM friend_reg_user WHERE auto_id=' .$info['auto_id'];
$result_member=executequery($sql);
if($line_member=mysql_fetch_array($result_member)){
    extract($line_member);
} else {
    header("location: index.php");
    exit;
}
?> 

 

 

If we do replace mysql_* with mysqli_* then we will have to bear in mind that a whole load of mysqli_* functions need  the database link to be passed.

E.g.: the following ones. 

mysql_query($query)
becomes

mysqli_query($link, $query)
I.e., lots of checking required.


on the other hand side: 

is it suffice if we replace every mysql_* function call with its equivalent mysqli_*, when we will use the procedural API 
 (note: there is some code based on the MySQL API, which is a procedural one - at least afaik), To help with that, the The MySQLi Extension Function Summary-manual is definitely something that will prove helpful. We can do the following: 

 

we have the following options to do that: 

 

- mysql_connect will be replaced by mysqli_connect
- mysql_error will be replaced by mysqli_error and/or mysqli_connect_error, depending on the context
- mysql_query will be replaced by mysqli_query ,,,, and so on and so forth. 

 

Note: For some functions, we may need to check the parameters very very carefully: Maybe there are 
some differences here and there -- but not that many differences. Belive me. Both mysql and mysqli-codes are based on the same library ( the great and powerful libmysql ; at least for PHP-version <= 5.2)


Usage - for instance:
with mysql, we have to use the mysql_select_db once connected, to indicate on which database we want to do our queries mysqli, on the other side, allows us to specify that database name as the fourth parameter to mysqli_connect.

 

what do you think bout this.. 

 

love to hear from you

Edited by dil_bert

Share this post


Link to post
Share on other sites

1. The now extinct mysql library and the mysqli library are two completely different animals.

2. Forget about mysqli and use PDO.

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

 

  • Like 1

Share this post


Link to post
Share on other sites
14 hours ago, dil_bert said:

Hello dear experts and user of PHPFreaks, 

 

love to hear from you

Hello dear Barand ,

first of all many thanks for the reply and your ideas and the sharing of ideas and insights. 

 

well i am so thankful that you gave me the hint several days.  

note: at the moment i am not at home - i am just traveling - and i have no access on the server - but as soon as i am back home i will do the tests on the server and i wll check 

 

some words regarding the installation:   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. I have  checked this all one week ago, In the phpinfo(); 

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)!? 

 

I love to hear from you 

 


mysqli_error() function / mysqli::$error
The mysqli_error() function / mysqli::$error returns the last error description for the most recent function call, if any.

Syntax:

Object oriented style

string $mysqli->error;
Procedural style


 
string mysqli_error ( mysqli $link )

 

Parameter:

Name    Description    Required/Optional
link    A link identifier returned by mysqli_connect() or mysqli_init()    Required for procedural style only and Optional for Object oriented style
Usage: Procedural style

mysqli_error(connection);

the Parameter:

Name    Description    Required/Optional
connection    Specifies the MySQL connection to use.    Required
Return value:

A string that describes the error. An empty string if no error occurred.

Version: PHP 5, PHP 7

Example of object oriented style:

 
<?php
$mysqli = new mysqli("localhost", "user1", "datasoft123", "hr");
	/* 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();
?>
Copy
Output:
	Errormessage: Unknown system variable 'a'
Example of procedural style:
	<?php
	$link = mysqli_connect("localhost", "my_user", "my_password", "world");
	/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
	if (!mysqli_query($link, "SET a=1")) {
    printf("Errormessage: %s\n", mysqli_error($link));
}
	/* close connection */
mysqli_close($link);
?>
Copy
Output:
	Errormessage: Unknown system variable 'a'
Example:
	<?php
$con=mysqli_connect("localhost","user1","datasoft123","hr");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
	// Perform a query, check for error
if (!mysqli_query($con,"INSERT INTO employees (First_Name) VALUES ('David')"))
  {
  echo("Errorcode: " . mysqli_errno($con));
  }
	mysqli_close($con);
?>
Copy
	

 

Sample Output:

Errorcode: 1146

 

and dear Barand - i am happy bout your ideas regading the comparison - and for the sharing of the ideas: 

Quote


1. The now extinct mysql library and the mysqli library are two completely different animals.
2. Forget about mysqli and use PDO.


some words regarding the installation:   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. I have  checked this all one week ago, In the phpinfo(); 

 

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)!? 

 

I love to hear from you 

Share this post


Link to post
Share on other sites
23 hours ago, dil_bert said:

Hello dear experts and user of PHPFreaks, 

 

love to hear from you

 

Hello dear Barand ,

i am trying to make sure that i have set up the MySQL use to be able to access the database via localhost? - this is pretty important!

Aferward i have to 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 wait - the $connect_error was broken until PHP 5.2.9 and 5.3.0. - so i have to make sure if i am the one that must be careful here
 */
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}

$mysqli->close();

 

well i have to do some more test. 

 

i keep you posted - and i come back and report all the findings. 

 

Share this post


Link to post
Share on other sites

 

 

hello +


<?php
$mysqli = new mysqli('localhost', 'jo', 'passwd', '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();


got back this  result ... 

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

On 6/19/2019 at 9:59 PM, dil_bert said:

Hello dear experts and user of PHPFreaks, 

 

love to hear from you

 

Hello dear Barand ,

i am trying to make sure that i have set up the MySQL use to be able to access the database via localhost? - this is pretty important!

Aferward i have to 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 wait - the $connect_error was broken until PHP 5.2.9 and 5.3.0. - so i have to make sure if i am the one that must be careful here
 */
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}

$mysqli->close();

 

well i have to do some more test. 

 

i keep you posted - and i come back and report all the findings. 

 

Share this post


Link to post
Share on other sites

hello dear all 

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

Share this post


Link to post
Share on other sites
Posted (edited)

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

Edited by dil_bert

Share this post


Link to post
Share on other sites

Hello dear all


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... 

Share this post


Link to post
Share on other sites

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"
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.
[/quote]

what i need to do. 
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.

the next things i will have to do: 
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!? 

Share this post


Link to post
Share on other sites

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

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/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!? 

Share this post


Link to post
Share on other sites
Posted (edited)
3 hours ago, dil_bert said:

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.

Not true. Connections are made to a server, not a database.

This works just fine.

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)

Edited by Barand
  • Like 1

Share this post


Link to post
Share on other sites
9 hours ago, dil_bert said:

 

hello dear Barand 

 

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

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 !? 

 

 

Share this post


Link to post
Share on other sites
Posted (edited)

Good day dear Barand  hello dear all. 

 

this error is quite very hard to fix .  i re-checked some tests and the behavior of the server while running the following scripts 

 
define("HOST",'localhost');
define("USERNAME",'jc');
define("PASSWORD",'my_passwd');
	    $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

 

...throws back the following: 

 
define("HOST",'localhost'); define("USERNAME",'jc'); define("PASSWORD",'my_secret_passwd'); $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

 

and the following script 

 
	<?php
$servername = "localhost";
$username = "jc";
$password = "my_passwd";
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();
    }
?>

 

...throws back the following:

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

 

I spend some time in trying to find out what goes wrong here: - see what i have  found out: .... 

 

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

  ..... what means according this thread:  https://stackoverflow.com/questions/29695450/pdoexception-sqlstatehy000-2002-no-such-file-or-directory

Quick test (run in shell):

 
	php -r "new PDO('mysql:hostname=localhost;dbname=test', 'username', 'password');"
	

 

 see the following:

 
	SQLSTATE[HY000] [2002] No such file or directory means php cannot find the mysql.default_socket file. Fix it by modifying php.ini file. On Mac it is mysql.default_socket = /tmp/mysql.sock (See PHP - MySQL connection not working: 2002 No such file or directory)  SQLSTATE[HY000] [1044] Access denied for user 'username'@'localhost' CONGRATULATION! You have the correct mysql.default_socket setting now. Fix your dbname/username/password. 

 

Also see Error on creating connection to PDO in PHP ( cf: https://stackoverflow.com/questions/1435445/error-on-creating-connection-to-pdo-in-php )

Today, I removed and reinstalled the latest version of lampp in order to move to php 5.30, and suddenly a very simple app is failing to connect to the mysql database. I'm using PDO to connect, and receiving the following error:

 Warning: PDO::__construct() [pdo.--construct]: [2002] Invalid argument (trying to connect via unix://) in /home/raistlin/www/todoapp/home.php on line 9 Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002]

 

[.. and so forth and so forth..] 

 

..... see some answers that i have found for the cumbersome issues: 

answer1: 
Usually means that you need to specify TCP/IP (1), or tell MySQL where your Unix socket is (2):

 
"mysql:host=127.0.0.1" or "mysql:host=localhost;port=3306"
"mysql:unix_socket=/var/run/mysqld/mysqld.sock"

 

answer2:  

Quote

You can also use 127.0.0.1, rather than specifying "localhost", in your db connection string to avoid this issue altogether.

answer3:
You might want to modify php.ini so PDO can find mysql.sock by specifying the pdo_mysql.default_socket = /opt/lampp/var/mysql/mysql.sock (in the case of xampp). Don't forget to restart Apache after changing php.ini.


cf: https://stackoverflow.com/questions/1435445/error-on-creating-connection-to-pdo-in-php
 

well i think i have some issues on my server... 

 

Edited by dil_bert

Share this post


Link to post
Share on other sites

- i  guess that it is time to discuss all that with the serveradmin - since he is the guy that can change the configs... 

Share this post


Link to post
Share on other sites
On 6/23/2019 at 4:07 PM, dil_bert said:

 

hello dear Barand - and dear requinix - and all the supporter here!! ;)

 

first of all - many many thanks for the reply. i am very glad to be here on this great place. - many many thanks for the continued help and the your supprt. This is just great. 
And i am very very happy that you never have gave up the support and the continued help. 

Now it woks - i have talked to my serveradmin and he did some corrections.  And now all runs like a chame.  Note: i will talik to him and will ask him what exactly he did - in order to get to know what was the missing piece.  I come back later the week and will let you know!!! 

Above all:  Again - many many thanks for all you did.  Barand  -you deserve many many kudos. Keep up the great work it rocks.

Note: you supprtet my php-questions for years now.  probably you remember that you gave great hints as i was worin on a db-solution for open-streetmap-data. 

Keep up your great work 

regards Dilbertone ;)

Share this post


Link to post
Share on other sites
Posted (edited)

hello and  good evening dear Barand, 

the admin renewed the PHP-Build two days ago - on Jun 25 2019 16:34:32

the new php was not able to find the mysql path with the data derived from php.ini. The Server-admin had to recompilie it and had to set a configure argument regarding the correct paths. he talked about a "glitch" ... 

note: as far as i can see we still have the following 

Quote


mysql.default_socket    /var/run/mysql/mysql.sock    /var/run/mysql/mysql.sock
mysqli.default_socket    /var/run/mysql/mysql.sock    /var/run/mysql/mysql.sock


and furthermore 

Directive    Local Value    Master Value

and so forth

pdo_mysql.default_socket    /home/vhost/WWW/var/run/mysql/mysql.sock    /home/vhost/WWW/var/run/mysql/mysql.sock

note : it runs now very well - but we still not have the newest version of PHP - guess that we do this upgrade in the near future... 

BTW. we still have to upgrade the hardware too... (i will keep you informed;)

 

dear Barand -  you and requinix you have helped me so much. You gave answers and encouraged me to go on. You lend me a helping hand. This was just very very important. This is AWESOME, that's all exactly what I need. Thanx a million for what you did!


to sum up: this two-week-troubleshooting can be a great chance of digging deeper into some server-internals and the setting up of DB on Linux-systems. I will read some manuals and will want to understand all the things that happened here.  It will take some days to do that.  But above i wrote what the Backend-admin said to me so far. 


above all: i am so glad to be here - and to receive so much support and encouraging. keep up the great work

greetings

Edited by dil_bert

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.