Connecting PHP to MS SQL Server

user3315848
<?php
$serverName = "(local)"; //serverName
$connectionInfo = array( "Database"=>"DabaseNew", "UID"=>"sa", "PWD"=>"*****");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn==true ) {
    echo "Connection established.<br />";
}else{
    echo "Connection could not be established.<br />";
    die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT * FROM Dbo.DATABASE COMPANY SERVICES$Employee";
$stmt = sqlsrv_query( $conn, $sql);
if(!$stmt){
    die( print_r( sqlsrv_errors(), true));
}
$rows = sqlsrv_has_rows($stmt);
while($obj = sqlsrv_fetch_object( $stmt)){
    echo $obj->Description.", ".$obj->lName."<br />";
}
?>

I am trying to connect to php to my sql server using sqlsrv_connect. The above code gives me an error below;

Output: Connection established. Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 102 [code] => 102 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near 'SERVICES'. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near 'SERVICES'. ) )

Daniel

You need to escape the $ character with \$, as php treats it as first character ov a variable. Try this:

$sql = "SELECT * FROM Dbo.[DATABASE COMPANY SERVICES\$Employee]";

EDIT:

To avoid escapingg you could also use single quotes ' instead of double quotes ". Then PHP does not resolve variables within the string. (see this question)

$sql = 'SELECT * FROM Dbo.[DATABASE COMPANY SERVICES$Employee]';

2nd EDIT:

To concatenate two strings use . operator like this:

$foo = "Hello ";
$bar = $foo."world!"; // gives "Hello world!"

As you can read within the answer linked within the first edit " double quotes resolve variables inbetween, while ' single quotes don't. your possible solution could be like this:

$query = 'SELECT [First Name] AS firstName, [Last Name] AS lastName
            FROM  Dbo.[DATABASE COMPANY SERVICES$Employee]
            WHERE [Employee Number] = 15 OR [E-Mail] = \''.mssql_escape($mail).'\'';

But you should NEVER directly send a GET parameter top your sql server. Anybody could infiltrate your database or even delete it. Therefore you should add a escape function like this one or consider using another db-library like PDO and build parameterized queries. It might me sufficient to escape single quotes within the variable with another single quote like this:

function mssql_escape($str) {
    return str_replace("'", "''", $str);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Connecting PHP to MS SQL Server

From Dev

Connecting Quartz to MS Sql Server

From Dev

Connecting MS SQL Server with mule

From Dev

Error connecting to MS SQL server

From Dev

Connecting to ms-sql server using nodejs

From Dev

Connecting to microsoft sql server using php

From Dev

Connecting to sql server data with PHP on ubuntu

From Dev

Could not find driver, connecting php to sql server

From Dev

PDO Error "Adaptive Server is unavailable" when connecting to MS SQL Database

From Dev

Error connecting to MS SQL Server using pyODBC, unixODBC and FreeTDS (on a Mac)

From Dev

Error connecting to MS SQL Server using pyODBC, unixODBC and FreeTDS (on a Mac)

From Dev

Connecting Codeigniter 3 with MS SQL Server using odbc

From Dev

MS SQL Query failed in PHP but not in MS SQL Server Management Studio

From Dev

Configuring PHP 5.3 to work with MS SQL Server

From Dev

Xampp MS SQL server PHP 5.6

From Dev

PHP Connection to MS SQL Server 2012

From Dev

connecting to sql server compact

From Dev

connecting webservice to sql server

From Dev

Issues connecting to SQL Server

From Dev

SQL Server connecting LIKE with or

From Dev

Connecting to a server-side SQL database via php with jquery

From Dev

Connecting to a database in SQL and PHP

From Dev

Connect PHP on Linux server to MS SQL on remote server running Windows

From Dev

ExQuilla alternatives for connecting to MS Exchange Server?

From Dev

Connecting to MS SQL database with PHP: Data source name not found, and no default driver specified

From Dev

Connecting to MS SQL database with PHP: Data source name not found, and no default driver specified

From Dev

Connecting MS-SQL with initial catalog

From Dev

Connecting Android app to MS SQL database

From Dev

Connecting to MS SQL database with Java JDBC

Related Related

  1. 1

    Connecting PHP to MS SQL Server

  2. 2

    Connecting Quartz to MS Sql Server

  3. 3

    Connecting MS SQL Server with mule

  4. 4

    Error connecting to MS SQL server

  5. 5

    Connecting to ms-sql server using nodejs

  6. 6

    Connecting to microsoft sql server using php

  7. 7

    Connecting to sql server data with PHP on ubuntu

  8. 8

    Could not find driver, connecting php to sql server

  9. 9

    PDO Error "Adaptive Server is unavailable" when connecting to MS SQL Database

  10. 10

    Error connecting to MS SQL Server using pyODBC, unixODBC and FreeTDS (on a Mac)

  11. 11

    Error connecting to MS SQL Server using pyODBC, unixODBC and FreeTDS (on a Mac)

  12. 12

    Connecting Codeigniter 3 with MS SQL Server using odbc

  13. 13

    MS SQL Query failed in PHP but not in MS SQL Server Management Studio

  14. 14

    Configuring PHP 5.3 to work with MS SQL Server

  15. 15

    Xampp MS SQL server PHP 5.6

  16. 16

    PHP Connection to MS SQL Server 2012

  17. 17

    connecting to sql server compact

  18. 18

    connecting webservice to sql server

  19. 19

    Issues connecting to SQL Server

  20. 20

    SQL Server connecting LIKE with or

  21. 21

    Connecting to a server-side SQL database via php with jquery

  22. 22

    Connecting to a database in SQL and PHP

  23. 23

    Connect PHP on Linux server to MS SQL on remote server running Windows

  24. 24

    ExQuilla alternatives for connecting to MS Exchange Server?

  25. 25

    Connecting to MS SQL database with PHP: Data source name not found, and no default driver specified

  26. 26

    Connecting to MS SQL database with PHP: Data source name not found, and no default driver specified

  27. 27

    Connecting MS-SQL with initial catalog

  28. 28

    Connecting Android app to MS SQL database

  29. 29

    Connecting to MS SQL database with Java JDBC

HotTag

Archive