sql server - Show all database in sqlsrv by php
I want to display all databases in microsoft sql srv for admin panel, but i have problem with get value. I'm try with query:
EXEC sp_databases
This query is executed successfully in sql srv, but when i'm try make this same by PHP, im not see value, no any errors or warnings, return null
My PHP Code:
<?php
$serverName = $_POST['hostname'];
$uid = $_POST['username'];
$pwd = $_POST['password'];
$connectionInfo = array( "UID"=>$uid,
"PWD"=>$pwd);
$connsrv = sqlsrv_connect( $serverName, $connectionInfo);
if($connsrv == TRUE ){
echo "connected";
$tsql = "EXEC sp_databases";
$stmt = sqlsrv_query( $connsrv, $tsql);
while( $row = sqlsrv_fetch_array($stmt)){
echo $row['DATABASE_NAME'];
}
}else{
echo "no connect";
}
?>
I don't know why I not see any result - any suggestions ? :(
Answer
Solution:
One possible explanation for this unexpected behaviour is that
CREATE DATABASE
orALTER ANY DATABASE
orVIEW ANY DEFINITION
permissions are requied to run thesp_databases
stored procedure (I can reproduce this with a server login, which has onlypublic
role). Set the needed permissions for the connection user, or as another option, try to use thesys.databases
system view.