-2

I am trying to connect PHP with mssql and facing this error. I have downloaded the drivers and installed and configured my PHP.ini file as well. I am receiving connection established but its not working for mssql_query can any body please help me on this.

<?php
$serverName = "server details";
$connectionInfo = array("Database"=> "mydbname", "UID" => "id", "PWD" => "password");
$conn = sqlsrv_connect($serverName , $connectionInfo);

if($conn){
    echo "connection established <br />";
}
else{
    echo "connection could not established <br />";
    die(print_r(sqlsrv_errors(),true));
}

$query = "SELECT * FROM AgeNames";

$result = mssql_query( $query );

for ($i = 0; $i < mssql_num_rows( $result ); ++$i)
     {
         $line = mssql_fetch_row($result);
         print( "$line[0] - $line[1]\n");
     }

I have added these fields in PHP ini PHP ini image

and this is what i am getting in phpinfo()

phpinfo

phpinfo screenshot 2

1 Answers1

1

Seems you're using incorrect functions. You need to use sqlsrv_query function instead of mssql_query i.e.

$result = sqlsrv_query( $conn, $query );

P.S. mssql_query has been removed since PHP 7.0. Read more here.

gaganshera
  • 2,629
  • 1
  • 14
  • 21
  • Thanks a lot but I am facing another issue by using sqlsrv_query I am not able to fetch data from tables. `$query = "SELECT * FROM AgeNames"; $result = sqlsrv_query( $conn , $query); while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC)) { echo "Minimum Age: ".$row['0']."\n"; echo "Maximum Age: ".$row['MaxAge']."\n"; } ` and I am getting this error `Notice: Undefined index: MinAge in C:\xampp\htdocs\aljawaizi\connection.php on line 23 Minimum Age: Notice: Undefined index: MaxAge in C:\xampp\htdocs\aljawaizi\connection.php on line 24` – Khalil Ur Rehman Jun 07 '20 at 08:36
  • Hi @KhalilUrRehman, I suggest you create a different question for this error. Since this doesn't pertain to the original question as you're no longer getting `Call to undefined function`, this means your code was able to process that funciton. – gaganshera Jun 07 '20 at 08:48
  • Why in the world would you specify NUMERIC keys in the result set, then try to access a key called `MaxAge`? – mickmackusa Jun 07 '20 at 09:36