0

I want to connect PHP with SQL Server 2000. I have search how to connect it and find this code:

$dsn="MY-PC\MSSQLServer";
$username="sa";
$password="admin";
$database = "perpus";
$sqlconnect=odbc_connect("Driver={SQL Server};Server=$dsn;Database=$database;", $username, $password);
$sqlquery="SELECT * FROM TDenda;";
$process=odbc_exec($sqlconnect, $sqlquery);

while(odbc_fetch_row($process)){
$companyName = odbc_result($process,"denda_perhari");
echo "$companyName<br>"; }
odbc_close($sqlconnect);

When I run it on browser, it result this:

Warning: odbc_connect(): SQL error: [Microsoft][ODBC SQL Server Driver][DBMSLPCN]Invalid connection., SQL state 08001 in SQLConnect in C:\xampp\htdocs\perpus\config.php on line 6

Warning: odbc_exec() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\perpus\config.php on line 8

Warning: odbc_fetch_row() expects parameter 1 to be resource, null given in C:\xampp\htdocs\perpus\config.php on line 10

Warning: odbc_close() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\perpus\config.php on line 13

Did anyone know how to connect it correctly? I already tried change MSSQLServer to SQLEXPRESS but still not working.

===UPDATE

I change to sqlsrv, already install the driver and extension. Here's my new code

<?php
$serverName = "MY-PC\MSSQLSERVER";
$connectionInfo = array( "Database"=>"perpus", "UID"=>"sa", "PWD"=>"perpus");
$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));
}
?>

And it resulted:

Connection could not be established. Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 87 [code] => 87 [2] => [Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87]. [message] => [Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87]. ) [1] => Array ( [0] => HYT00 [SQLSTATE] => HYT00 [1] => 0 [code] => 0 [2] => [Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired [message] => [Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired ) [2] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 87 [code] => 87 [2] => [Microsoft][ODBC Driver 11 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. [message] => [Microsoft][ODBC Driver 11 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. ) )

I believe that my instance and server name is correct. So I have no idea why it always failed

Cross Vander
  • 2,077
  • 2
  • 19
  • 33
  • I am really not too familiar with SQL Server, but are you sure the DSN exists and that the username/password are correct? – Mike Sep 23 '15 at 01:28
  • You may also want to read this answer: http://stackoverflow.com/a/13756696/811240 – Mike Sep 23 '15 at 01:34
  • @Mike Yes, already correct. I'm trying use sqlserv but the browser return : sqlserv is not a function – Cross Vander Sep 23 '15 at 01:35
  • That's because `sqlserv` is not a function. See: http://php.net/sqlsrv (note, there is no 'e') for the `sqlsrv_*` functions and documentation. Or give PDO a try if you want. – Mike Sep 23 '15 at 05:34
  • Hello, I already installed sqlsrv, and now my result being more confused: SQL Server Network Interfaces: Connection string is not valid. Does my instance name is wrong? – Cross Vander Sep 23 '15 at 05:36
  • You don't need an odbc interface to connect from PHP to SQL Server. Use sqlsrv driver and have a look at examples in this article http://php.net/manual/en/function.sqlsrv-fetch-array.php – Dzmitry Paliakou Sep 23 '15 at 13:10
  • @CrossVander Then you probably have an invalid connection string. Make sure it follows the format outlined here (assuming you're using `sqlsrv_connect()`): http://php.net/manual/en/function.sqlsrv-connect.php – Mike Sep 23 '15 at 17:32
  • @Mike Check my updated code. Already same with that link. – Cross Vander Sep 25 '15 at 00:46
  • @DmitryPolyakov check my updated code – Cross Vander Sep 25 '15 at 00:47
  • I think the error is pretty self-explanatory. Seems as if you have an incorrect value for `$serverName`. Make sure the server name, instance name and port number (default is 1433) are all correct. Also, I'm not sure if these are case-sensitive, but to be sure, use the same case that they were registered with. I see both your examples use different casing. – Mike Sep 25 '15 at 01:10
  • all correct, until I change my servername to (local) then I got this error: "ODBC Driver 11 for SQL Server does not support connections to SQL Server 2000 or earlier versions". Now I know the problem from beginning is the driver. Do You know what driver that I need? – Cross Vander Sep 25 '15 at 01:13
  • It gives you that error using sqlsrv functions? – Mike Sep 25 '15 at 01:16
  • Yes. Or do You know which ODBC version that I need? – Cross Vander Sep 25 '15 at 01:16
  • Maybe this article might help: http://craigballinger.com/blog/2011/08/usin-php-5-3-with-mssql-pdo-on-windows/ – Mike Sep 25 '15 at 01:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90574/discussion-between-cross-vander-and-mike). – Cross Vander Sep 25 '15 at 01:22

0 Answers0