I am trying to connect to MSSQL
using sqlsrv_connect()
however it is not working. I am getting a weird error:
/var/www/html/dir/mssql_conn.php: line 1: ?php: No such file or directory
/var/www/html/dir/mssql_conn.php: line 5: syntax error near unexpected token `('
/var/www/html/dir/mssql_conn.php: line 5: `$conn_array = array ('
My code is:
$conn_array = array (
"UID" => "",
"PWD" => "",
"Database" => "dbName",
);
$conn = sqlsrv_connect('(local)', $conn_array);
if ($conn) {
echo "connected";
if(($result = sqlsrv_query($conn,"SELECT TOP 100 * FROM dbo.tblp")) !== false){
while( $obj = sqlsrv_fetch_object( $result )) {
echo $obj->colName.'<br />';
}
}
} else {
die(print_r(sqlsrv_errors(), true));
}
I've tried localhost\\sqlexpress, 8357
, serv.domain.tld\\sqlexpress, 8357
too and nothing works.
Form the outside there is a port. But I don't think that would apply from the inside would it?
Any help how I can actually connect?
I think it's because you added a comma after "dbName"
, Maybe it should be
$conn_array = array (
"UID" => "",
"PWD" => "",
"Database" => "dbName"
);
You can check the connection by the below code
$serverName = "(local)\sqlexpress";
$conn_array = array("UID" => "",
"PWD" => "",
"Database" => "dbName"
); // Provide the valid details
/* Connect using Windows Authentication. */
$conn = sqlsrv_connect( $serverName, $conn_array);
if( $conn === false )
die( FormatErrors( sqlsrv_errors() ) );
function FormatErrors( $errors )
{
/* Display errors. */
echo "Error information: <br/>";
foreach ( $errors as $error )
{
echo "SQLSTATE: ".$error['SQLSTATE']."<br/>";
echo "Code: ".$error['code']."<br/>";
echo "Message: ".$error['message']."<br/>";
}
}
For more information https://docs.microsoft.com/en-us/sql/connect/php/example-application-sqlsrv-driver
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.