1

I'm working with PHP Version 5.4.45, my website and a SQL Server database hosted on Microsoft Azure.

At this moment I just have 2 tables:

  • users (ID [int PK], fname [vchar], lname [vchar]);
  • user_login (ID [int PK], userID [int], login [datetime]);

I had inserted data on SSMS just for tests.

On my website I run a query to give me a list of registered users and it returns all relevant information (2 rows).

When I run a query to return a list of users login that throws a 500 Internal server error.

  1. When my 1st "echo" is the ID or userID, it returns ONE value and nothing more.

  2. When my 1st "echo" is the login (datetime), it doesn't shows anything, and the next "echo" in code too.

With this I conclude that the error's on login column.

I've read that 'bog queries' could do this error (I only have 30 rows for each userID, 60 for my 2 users) but, for test, I limited my query to only 5 records.

The query that I'm trying at this moment works on SSMS.

I can't access to the PHP error log file.

<?php

    $userID = 1;
    $uid = "xxx";
    $pwd = "xxx";
    $serverName = "xxx";
    $connectionInfo = array( "Database"=>"Development", "UID"=>$uid, "PWD"=>$pwd);
    $conn = sqlsrv_connect( $serverName, $connectionInfo);

    if( $conn )
    {
        // echo "Connection established.\n";
    }
    else
    {
        echo "Connection could not be established.\n";
        die( print_r( sqlsrv_errors(), true));
    }

    $sql = "SELECT * FROM dbo.userLogin WHERE userID=1 AND ID BETWEEN 5 AND 10 ORDER BY ID ASC ";
    $stmt = sqlsrv_query( $conn, $sql);

    if( $stmt === false ) {
        die( print_r( sqlsrv_errors(), true));
    }

    while($sql = sqlsrv_fetch_array($stmt)) {
        $id         = $sql["ID"];
        $login      = $sql["login"];
        $num_row    = count($login);

        for ($i = 0; $i < $num_row; $i++) {
            echo "
                <p>$id</p>
                <p>$login</p>
                <p>$userID</p>
            ";
        }
    }
?>

Query working in SSMS

enter image description here

TiA!

2
  • 1
    "I can't acess to the PHP error log file.", then contact someone who can. The only way to debug a 500 error is to read the error log. Commented Apr 14, 2016 at 11:26
  • @Oldskool Thanks for the reply! My big problem was exactly that: can't acess the log file to know where was the problem. I made a little search and now I'm able to see the log file. 'Catchable fatal error: Object of class DateTime could not be converted to string in D:\home\site\wwwroot\teste.php on line 44' Commented Apr 14, 2016 at 11:43

1 Answer 1

1

Try this $sql = "SELECT ID, userID, CONVERT(nvarchar(50),[Login],120) as [Login] FROM dbo.userLogin WHERE userID=1 AND ID BETWEEN 5 AND 10 ORDER BY ID ASC ";

Sign up to request clarification or add additional context in comments.

4 Comments

500 internal server error... such a random error. Thanks!
Once I started with PHP + MS SQL this was the second issue after installing PDO drivers :)
@DenisV gofr1 is right. Also, you can view this link if you want to understand more what was the problem: af-design.com/2010/03/13/…
@muhammedv thanks for valuable reply! Also you can use this stackoverflow.com/questions/26470041/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.