0

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

Thom A
  • 88,727
  • 11
  • 45
  • 75
tappo
  • 1
  • 1
  • 4
    `mysqli` is for connecting to MySQL databases. to connect to an MS SQL db, you'll want to use something like: https://www.php.net/manual/en/function.sqlsrv-connect.php (that man page also includes an example of using Windows authentication) – TZHX Jan 24 '23 at 08:49
  • 1
    Terminology note: PhpMyAdmin is not a database. It's just a web based administration tool for managing MySQL databases. (It is one of many such administration clients). Your code connects directly to the MySQL database server and doesn't have anything to do with PhpMyAdmin. It's the same with SQL Server... SSMS (SQL Server Management Studio) is also a front-end, it's the equivalent of phpMyAdmin...again, your PHP code does not connect to that application, it connects to a SQL Server (backend) instance directly. – ADyson Jan 24 '23 at 09:27
  • 1
    `what's the difference`...the difference is that MySQL and MS SQL Server are two entirely different products with entirely different connection protocols. You can't use the same code to connect to both of them. If you simply google "php connect to sql server" you'd find plenty of examples showing the correct approach. – ADyson Jan 24 '23 at 09:29
  • "You can't use the same code to connect to both of them" I know that; i was just posting what I tried cause sometime when u dont do that ppl flag your question – tappo Jan 24 '23 at 09:58
  • **PhpMyAdmin is not a database** Okay thanks, didnt know. Anyway it was to make myself clear – tappo Jan 24 '23 at 09:59
  • @TZHX thanks you; I tried but now it's giving me the error "Failed connection for **USER**"; so i'm tring to undestand how to resolve it – tappo Jan 24 '23 at 10:00
  • @ADyson **If you simply google "php connect to sql server"** Oh my god, u know i didn't think about that! Thank you so much! Do you thinks i came here without tried that? I just didnt found the link that TZHX was kindly enough to share – tappo Jan 24 '23 at 10:02
  • `Do you thinks i came here without tried that`...well it looked like it, given the nature of the question. And a lot of people do seem to skip the "doing some research" step of investigating their problem. Given the complete wrong track you seemed to have gone down, and given the easy availability of the right info online, it seemed a reasonable assumption. TBH if you _had_ typed that into google, it's unclear to me how you could possibly have got into the mess you showed in your question (trying to use mysqli for the task), because there are lots of clear examples of the correct method. – ADyson Jan 24 '23 at 10:23
  • `I tried but now`...you should probably delete this (nonsensical) question and start a new one showing your current code and the exact error you're getting, so you can get useful answers about that specific issue (assuming it can't be resolved by googling the error and/or consulting the various links in the blue box above). – ADyson Jan 24 '23 at 10:28

0 Answers0