-1

I'm coding a web service for iOS in Php. I'm trying to retrieve data from server through php but the response is null.The server is MSSql Server 2014.

<?php 
    define ('DB_HOST', '****.net');
    define ('DB_USER', '****');
    define ('DB_PASS', '******');
    define ('DB_NAME', '*******');

$dbc = mysql_connect(DB_HOST, DB_USER, DB_PASS);
mysql_select_db(DB_NAME, $dbc);


if (mysql_select_db){
    echo "Done";
}else{
    echo "Die";
}

    $sql_select = "SELECT * from ClientChk";
    $records = mysql_query($sql_select);
    $count= 1;

    while($result =  mysql_fetch_array($records))
    {
    if ($result == nil){
            echo "Nil!";
    }
    }
    echo json_encode($result['Id']);

?>
  • `$result` is an array, and you're trying to compare an array with a constant named `nil`, which you haven't defined in the first place. – Rajdeep Paul May 09 '17 at 11:13
  • Have you tried using `print_r($result['Id'])` to check if it has the expected data? – Antonios Tsimourtos May 09 '17 at 11:13
  • Don't use `mysql_*` functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`pdo`](http://php.net/manual/en/book.pdo.php) instead. [And this is why you shouldn't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – Rajdeep Paul May 09 '17 at 11:14
  • I don't think that you can connect to an mssql server with the mysql connector. Use PDO or mssql connector depending on your php version (mssql removed as of 7.0) – Derenir May 09 '17 at 11:14
  • what does `$count= 1;` do? what does `if ($result == nil){` do – Rotimi May 09 '17 at 11:15
  • @Derenir so how can i? – AhmedRaza Aedhi May 09 '17 at 11:16
  • @AntonisTsimourtos just tried it,nothing printed nor null – AhmedRaza Aedhi May 09 '17 at 11:17
  • @AhmedRazaAedhi you can start here: http://php.net/manual/en/function.mssql-connect.php – Derenir May 09 '17 at 11:17
  • @AhmedRazaAedhi check examples from this:- http://php.net/manual/en/function.sqlsrv-fetch-array.php – Alive to die - Anant May 09 '17 at 11:18
  • @AhmedRaza Aedhi if you are using microsoft ms sql so, why are your using mysql db connection method, it will not work, you should do ms sql db connection learn here for ms sql db connection http://php.net/manual/en/function.mssql-connect.php – lazyCoder May 09 '17 at 11:32

3 Answers3

0

This is a bad example of doing this.

1) You have to check if the connection was successful.

2) If use MSSQL, then you can't use mysql_ commands.

3) nill doesn't exist in php, should be 'null'

Dezigo
  • 3,220
  • 3
  • 31
  • 39
0
$SERVER = '127.0.0.1';
$DB = 'test';
$USER = 'username';
$PASSWORD = 'password';

$db = new PDO("sqlsrv:server=$SERVER;database=$DB;", $USER, $PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        try {

            $query = "SELECT * FROM ClientChk";  
            $result = $db->query($query)->fetchAll(PDO::FETCH_ASSOC);

        } catch (PDOException $ex) {
            $ex->getMessage();
            echo $ex;
        }

     echo json_encode($result);
stijn nel
  • 51
  • 6
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Akkusativobjekt May 09 '17 at 11:29
  • This an example of connecting to a DB with the PDO class. Then using the fetchall function to retrieve a array of the result set rows. This Tutorial covers the basics: https://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059 – stijn nel May 09 '17 at 11:46
0

So, the problem is, that you try to connect to a MSSQL database, with a MySQL database connector. To connect to a MSSQL database, you can use the following methods:

  • mssql_connect(): It uses a quite similar syntax to mysql, but it is removed as of php 7.0, so I advice against it's usage even if you using an earlier version of php.

  • PDO class: It's differs a lot from the older function style sql connectors, but it is the preferred way by many.

  • sqlsvr_connect(), obdc_connect(): If you prefer a function based connector instead of the PDO class, and you don't want to use the deprecated mssql_connect, I recommend choosing one of these.

I hope, I could be of any help.

Derenir
  • 537
  • 1
  • 12
  • 29