Connecting to Microsoft SQL server with PHP

Viewed 59

I'm trying to connect to my SQL server with PHP. I'm very stuck on this. I have downloaded the drivers for my PHP version (7.2). I've added the extensions to the .ini file. I've restarted the webserver in intellij. I've restarted the SQL server. Nothing I've done can get rid of this error.

Uncaught Error: Call to undefined function sqlsrv_connect() in C:\Users\Administrator\WebstormProjects\Skittle-Dew\BackEnd\addVendor.php:7 Stack trace: #0.

<div
            id="createVendor" class="tabcontent">
            <h2>Add New Vendor</h2>
            <!--Should functionally add a new vendor to the vendor table-->

            <form method = "post" action = "../../BackEnd/addVendor.php">
            <label for="vendName">Vendor Name: </label>
            <input id="vendName" type="text" name="vname">
            <br>
            <label for="vendEmail">Email Address: </label>
            <input id="vendEmail" type="text" name="vmail">
            <br>
            <label for="vendPhone">Phone Number: </label>
            <input id="vendPhone" type="text" name="vphone">
            <br>
            <label for="vendAddress">Address: </label>
            <input id="vendAddress" type="text" name="vaddress">
            <br>
            <label for="vendCity">City: </label>
            <input id="vendCity" type="text" name="vcity">
            <label for="vendState">State: </label>
            <input id="vendState" type="text" name="vstate">
            <label for="vendZip">Zip Code: </label>
            <input id="vendZip" type="text" name="vzip">
            <br>
            <input type="submit" name="submit" value="Submit">
            <p id="avWarn" style="color: #ff0000; visibility: hidden"></p>
            </form>

This is the relevant part of my HTML. I'm running the server using intellij

<?php

    if(isset($_POST['submit']))
    {
        $serverName = "SERVER-NAME\SQLEXPRESS"; //serverName\instanceName
        $connectionInfo = array( "Database"=>"DB Name", "UID"=>"user", "PWD"=>"password");
        $conn = sqlsrv_connect( $serverName, $connectionInfo);

        if( $conn )
         {
            echo "Connection established.<br />";
         }else
         {
            echo "Connection could not be established.<br />";
            die( print_r( sqlsrv_errors(), true));
         }

        $tsql1 = "SET IDENTITY_INSERT Vendor_Table ON Insert INTO Vendor_Table(Vendor_ID, Vendor_Name, Vendor_Address, Vendor_City, Vendor_State, Vendor_Zip_Code, Vendor_Phone, Vendor_Email) VALUES ('NEWID()','$_POST[vname]', '$_POST[vaddress]','$_POST[vcity]','$_POST[vstate]','$_POST[vzip]','$_POST[vphone]','$_POST[vmail]')";


        $query = sqlsvr_query( $conn, $tsql1);

        if( $query )
        {
            echo "Row successfully inserted.\n";
        }
        else
        {
            echo "Row insertion failed.\n";
            die( print_r( sqlsrv_errors(), true));
        }

        sqlsvr_close($conn);

    }

Any help would be hugely appreciated. I've spent a lot of time pouring over the documentation for PHP and Microsoft SQL and can't find anything.

1 Answers

First of all - never-ever use weird markings in database names, table names, file names, variables (including spaces, dots, dashes, (back)slashes, asterix, etc.). It is very bad programming!

Secondly - you're error appears to show you the way - something is missing. In big part, it COULD BE the database name or any other weird s*** about that connection. But, it is always possibility, that wrong driver version is installed.

Related