6

I want to access the MS SQL Server and have the select query. I have installed the dll files and am using the sqlsrv_query. The connection has been successful but I get:

[Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid object name as error

I am using PHP version 5.3.1

After the connection I have this code

$sql = "SELECT id, latitude, longitude from job ";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
      echo $row['latitude'].", ".$row['longitude']."<br />";
}
sqlsrv_free_stmt( $stmt);
gofr1
  • 15,741
  • 11
  • 42
  • 52
learner1988
  • 141
  • 3
  • 11

3 Answers3

0

"job" is a reserved word in SQL Server. Use this:

$sql = "SELECT id, latitude, longitude from [job] ";
bjnr
  • 3,353
  • 1
  • 18
  • 32
0

Make sure you are connected to to correct database on the server, and that a table (or view or alias) named job exists in this database.

Daniel B
  • 797
  • 4
  • 13
  • I have connected to it and the response is also successfully printed.But while I try to retrieve the value I get the problem.I have the job table and also I have tried with the other tables as well but gives the same result – learner1988 Dec 16 '13 at 03:59
0

Try to execute using the Database and Schema names like

databaseName.schema.table

$sql = "SELECT id, latitude, longitude from [DB_Name].[dbo].[job]";

OR

only with schema name: DBO or any other

$sql = "SELECT id, latitude, longitude from [dbo].[job] ";
CR241
  • 2,293
  • 1
  • 12
  • 30