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