I have created a local php server using XAMPP, for my app to consume.
My app makes a request every second to a php file to check if there is something to do. I've noticed that if I run multiple instantnces of my app, sometimes it starts returning this error:
Warning: mysqli::__construct(): (HY000/2002): Only one
usage of each socket address (protocol/network address/port) is
normally permitted. in C:\xampp\htdocs\testProject\common.php
on line 2 Warning: mysqli_query():
Couldn't fetch mysqli in
C:\xampp\htdocs\testProject\signal_update.php on line
2 Warning: mysqli_error(): Couldn't fetch
mysqli in C:\xampp\htdocs\testProject
The code to connect to the BD is the following:
function openCon()
{
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db = "test_database";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);
return $conn;
}
How can I fix this?
Related
This question already has answers here:
How to use PHP to connect to sql server
(17 answers)
Using PHP connect Microsoft SQL Server 2014
(1 answer)
PHP script to connect to SQL Server
(1 answer)
Connect to SQL Server using PHP?
(1 answer)
Closed 27 days ago.
I'm bulding an Intranet for my workplace and I need to connect it to a Database. I already have a script that connect my website throught XAMPP on PHPmyadmin, but now my boss asked me to connect the website to a database we have on Microsoft SQL server Management program and maybe I'm stupid but I'm not understanding how to do it and what's the difference.
And also: to connect to Microsoft SQL i use windows authentication so I dont know what to write on the php script.
This is my connect.php script I have and works on PHPMYADMIN:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "intranet";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
And this is the one I wrote for the database I have on Microsoft SQL (I change the server cause it's the one I use to connect when I open the program):
<?php
$servername = "EDP004\SQLEXPRESS";
$username = "root";
$password = "";
$dbname = "INTRANET_DD";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
And this is the error I get on browser:
Warning: mysqli::__construct(): php_network_getaddresses: getaddrinfo for EDP004\SQLEXPRESS failed: unknown Host. in C:\xampp\htdocs\log\material\center\html\scripts\config.php on line 8
Fatal error: Uncaught mysqli_sql_exception: php_network_getaddresses: getaddrinfo for EDP004\SQLEXPRESS failed: unknown Host. in C:\xampp\htdocs\log\material\center\html\scripts\config.php:8 Stack trace: #0 C:\xampp\htdocs\log\material\center\html\scripts\config.php(8): mysqli->__construct('EDP004\\SQLEXPRE...', '', '', 'INTRANET_DD') #1 C:\xampp\htdocs\log\material\center\html\index.php(2): require_once('C:\\xampp\\htdocs...') #2 {main} thrown in C:\xampp\htdocs\log\material\center\html\scripts\config.php on line 8
my code is as below
$dbhost = 'example.com';
$dbuser = 'dbusername';
$dbpass = 'dbpassword';
$dbName='dbname';
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbName);
// check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
after i execute code getting below error
Connection failed: Connection refused
Warning: mysqli::__construct(): (HY000/2002): Connection timed out in /home/public_html/test/remote.php on line 8
Connection failed: Connection timed out
I don't know your environment, but my experience is that most publicly accessible webservers do not expose the SQL-database to the public, so the port is probably blocked. If you control the server, then you can probably find something in the firewall, but if you are using something like shared hosting, then you can really only try the helpdesk.
This question already has answers here:
PHP Connection failed: SQLSTATE[HY000] [2002] Connection refused
(10 answers)
Closed 3 years ago.
I am new in php and doing a login/register page in localhost using xampp on mac. I am having a trouble with this error:
Warning: mysqli_connect(): (HY000/2002): Connection refused in /opt/lampp/htdocs/website/includes/dbh.inc.php on line 9
Connection failed: Connection refused
I have tried changing the code but it continuously adding more problems.
This is my code:
<?php
$servername = "192.168.64.2:8080";
$dBUsername = "root";
$dBPassword = "";
$dBName = "loginsystem";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);
if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
}
How can I eliminate this error?
If you want to connect to other port than the default one, add the port argument when you establish the connection, not define it within the server name.
$servername = "192.168.64.2";
$dBUsername = "root";
$dBPassword = "";
$dBName = "loginsystem";
$dbPort = "8080";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName, $dbPort);
if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
}
Another thing is to make sure you have the right port of MySQL service, port 8080 is commonly picked as the alternative for port 80 to handle HTTP request. Check your XAMPP configuration to find out which port is used for MySQL.
Just moved my site to new hosting. On the new server mysql_connect() works fine if called without using classes or inside of the method but not from the _construct. When connecting from the _construct the following error displays:
Access denied for user 'root'#'localhost' (using password: NO)...my configuration defines the user not as root but as USERNAME and does provides a password.
PHP 5.6, MySQL 5.5, Linux
Class File:
function __construct(){
error_reporting(E_ERROR);
$dbhost = 'localhost';
$dbuser = 'USERNAME';
$dbpass = 'PASSWORD';
$dbname = 'DATABASENAME';
$this->urlAppPath = "http://www.myurl.com/CD/";
// connect to the database
$this->db = mysql_connect($dbhost, $dbuser, $dbpass) or die("Connection Error: " . $SQL);
mysql_select_db($dbname);
}
function validatePerson($personID) {
$SQL = "SELECT *,COUNT(*) AS total FROM zen_customers WHERE customers_classware_id = '$personID'";
$result = mysql_query( $SQL ) or die("Could not execute query 1 . ".$SQL." ".mysql_error()." & ". $dbhost ." ".$dbuser ." ".$dbpass." ".$dbname);
}
Call Method File:
require_once('./clsCart.php');
$myclass = new clsCart();
$result2 = $myclass->validatePerson($personID);
Note
If I define and call the connection within the file that I have been trying to call the method...that is instead of calling the method it works. If I define and call the connection within the method it works...but if I define and call the connection within the construct() it does not work...error "Access denied for user 'root'#'localhost' (using password: NO)". This seems to be specific to some servers...regardless of using PHP 5.6 or lower & MySQL 5.6 or lower.
So, if I use :
$dbhost = 'localhost';
$dbuser = 'USERNAME';
$dbpass = 'PASSWORD';
$dbname = 'DATABASENAME';
// connect to the database
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("Connection Error: " . $SQL);
mysql_select_db($dbname);
Outside of the class or within the method then it works...but, I should (as I have in the past) be able to connect within the construct().
This seems to be related to a server issue. From the __contruct() only the Database default user information is being recognized. Everywhere else in the applications uses the User created for the database, but the construct will only connect when using the default database user on this server. Other servers are not having this issue, such that any database user with all privileges work.
You have to use mysqli_connect. Please never use mysql_connect it deprecated in PHP 5.5.0 and onwards. If still you face issue please let us know.
Following code for mysqli_connect :
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Definition and Usage
The mysqli_connect() function opens a new connection to the MySQL server.
Syntax
mysqli_connect(host,username,password,dbname,port,socket);
Parameter Description
port Optional. Specifies the port number to attempt to connect to the MySQL server.
socket Optional. Specifies the socket or named pipe to be used.
I cant figure out why i am not connecting to my db. It seems like it is finding the host but having authentication problems or something. I am using xampp and the db provided with it. My connect code is as follows
<?php
$servername = 'localhost:1234';
$username = 'protech';
$password = '';
$dbname = 'protech';
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}else{
echo "Connected successfully";
}
mysqli_close($conn);
exit;
?>
if you need any information I am by my computer and will respond quick.
I get these error messages after waiting a while.
Warning: mysqli::__construct(): MySQL server has gone away in C:\xampp\htdocs\protech_connect.php on line 9
Warning: mysqli::__construct(): Error while reading greeting packet. PID=7764 in C:\xampp\htdocs\protech_connect.php on line 9
Warning: mysqli::__construct(): (HY000/2006): MySQL server has gone away in C:\xampp\htdocs\protech_connect.php on line 9
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\protech_connect.php on line 9
The standard port for mySQL is 3306 - so if you are genuinely using 1234 then append that as a new parameter to the db connection constructor.
$dbhost = 'localhost';
$dbuser = 'protech';
$dbpwd = 'xxx';
$dbname = 'protech';
$dbport = 1234;
$conn = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname, $dbport );
if( $conn->connect_error ) exit( 'Bad foo: '.$conn->connect_error );
else { /* do exciting things */ }
$conn->close();
The mysqli_connect() function doesn't accept a port in the host parameter. You need to add the port number in another parameter like so:
$conn = mysqli_connect($servername, $username, $password, $dbname, $serverport);
Function documentation: mysqli::__construct()
If you get following error or problem then follow above solution.
Error:
Warning: mysqli_connect(): MySQL server has gone away
Warning: mysqli_connect(): Error while reading greeting packet. PID=3528
Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away
Solution 1:
use 127.0.0.1 instead of localhost. Don't use any port number after 127.0.0.1.
Solution 2:
Check your host file located at
C:\Windows\System32\drivers\etc
and remove any localhost or 127.0.0.1 entry in file and save. You can use any HostEditor program to edit this file.