Does anyone can idea how to execute mssql query in php :
I am creating a simple php page, which is successfully connected with sql-server but when i try to get data from table using query it's gives me error.
Php code
<?php
$serverName = "serverip"; //serverName\instanceName
$connectionInfo = array( "Database"=>"DBname", "UID"=>"user", "PWD"=>"pass");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT name FROM dbo.test";
$query = sqlsrv_query($conn, $sql);
if ($query === false){
exit("<pre>".print_r(sqlsrv_errors(), true));
}
while ($row = sqlsrv_fetch_array($query))
{
echo "<p>Hello, $row[name]!</p>";
}
sqlsrv_free_stmt($query);
?>
My Output is:
Array
(
[0] => Array
(
[0] => 42S02
[SQLSTATE] => 42S02
[1] => 208
[code] => 208
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid object name 'dbo.test'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid object name 'dbo.test'.
)
)
Please suggest me if you have any idea about it.
Thanks
Your SQL:
SELECT name FROM dbo.test
The error message:
Invalid object name 'dbo.test'.
So, you don't have a table named test
in your database yet. If you just want to do a really simple query without making a table first, do something like...
SELECT 1
Your database name is declared in your $connectionInfo
(it looks like you copied and pasted defaults here) and it does not match the database name you use in your query.
$connectionInfo = array( "Database"=>"aero", "UID"=>"user", "PWD"=>"pass");
Your query uses dbo
as the database name. Change it to aero
(or whatever string you have here):
$sql = "SELECT name FROM aero.test";
Hi you don't need to use database name in the query, since you doing this connection sqlsrv_query() itself. Just remove the "dbo" from the query and try, it will work...
Still if you getting the error, then check the database and table name and ensure you are using the same case-sensitive as you have given name to database and table name