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
Related
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.
This question already has answers here:
PHP 7 cannot find MySQLi
(2 answers)
Closed 3 years ago.
I have php7 installed and working
I'm using the Built-in HTTP server
Uncommented "extension_dir="C:\php\ext" and extension="php_mysqli.dll" in the php.ini files development and production (did production too just to be safe)
When I used the phpinfo() method, I found that it says "Loaded Configuration file: none"
I've installed php7 therefore mysqli is supposed to already be installed. Looking online I found that you have to edit the php.ini file (mentioned how I did so in bullet points above) however when I run:
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
I get the error:
Fatal error: Uncaught Error: Class 'mysqli' not found in C:\Users\Calvin\try\learningMySQL.php:17 Stack trace: #0 {main} thrown in C:\Users\Calvin\try\learningMySQL.php on line 17
Thank you, any help is appreciated.
$servername = "localhost";
$username = "username";
$password = "password";
$database = "database";
$con = mysqli_connect("$servername","$username","$password","$database");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
I get this error when I try to connect to the mysql database using php mysqli class.
I am using
OS :- windows 10
webserver :- xampp
php :- 7.3.3
it got a warning error and error message is junk.
mysqli::__construct(): (HY000/2002):
�ڑ��ς݂̌Ăяo���悪���̎��Ԃ�߂��Ă������������Ȃ��������߁A�ڑ��ł��܂���ł����B�܂��͐ڑ��ς݂̃z�X�g���������Ȃ��������߁A�m�����ꂽ�ڑ��͎��s���܂����
Using following code:
$dbServerName= "*******ap-northeast-1.rds.amazonaws.com";
$dbUsername= "example";
$dbPassword= "*******";
$dbName= "example";
// Create connection
$conn = new mysqli($dbServerName, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
dbServerName is amazon.com for example,
password is * for security.
This question already has answers here:
Message: mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known
(2 answers)
Closed 1 year ago.
I tried to connect to phpMyAdmin database but encounter an error Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: No such host is known. Below coding is the one that I currently use.
$servername = "http://localhost:8080";
$username = "root";
$password = "";
$db = "dbLogin";
$conn = new mysqli($servername, $username, $password, $db);
if ($conn->connect_error) {
die("Connection failed: ".$conn->connect_error);
}
else {
echo "Connection success!";
}
When I run my system, It will still show the message connection success! but together with the error I mentioned.
replace server name from
$servername = "http://localhost:8080";
to
$servername = "localhost";
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.