0

I have such a statement:

$user = $_POST['username'];
$pass = $_POST['password'];
$fetch = sqlsrv_query($conn, "SELECT ID FROM login WHERE 
                         username='$user' AND password='$pass'");
$count = sqlsrv_num_rows($fetch);

However, $count value returns null. I don't know what am I doing wrong.

Can you help me, please?

JohnEye
  • 6,436
  • 4
  • 41
  • 67
  • Does it give an error? – Daan May 21 '14 at 13:27
  • Perhaps you should add a `die()` statement that tells you what the error is. We don't have enough to guess what it might be here. – Jay Blanchard May 21 '14 at 13:27
  • It doesn't give an error Daan. Okay, I will put a die() operation to determine the error Jay Blanchard. – Hasan Sait Arslan May 21 '14 at 13:29
  • The following code is : print_r(sqlsrv_errors(), true); if($count!="") { session_register("sessionusername"); $_SESSION['login_username']=$user; header("Location: /website/index.php"); } else { header('Location:index.php'); } But it doesn't go to /website/index.php it goes to index.php – Hasan Sait Arslan May 21 '14 at 13:31
  • Have a look at the sqlsrv_query() example on http://www.php.net/manual/en/function.sqlsrv-query.php. It shows you how to output an error in case something is wrong... – Reinder Wit May 21 '14 at 13:32
  • and also it is the link: http://codepaste.net/kdrik8 – Hasan Sait Arslan May 21 '14 at 13:32
  • 1
    And please use parameterized queries, as your sample is extremely vulnerable to SQL injection – Reinder Wit May 21 '14 at 13:33
  • okay Reinder Wit, I will check now. thanks. :) – Hasan Sait Arslan May 21 '14 at 13:34
  • Jay Blanchard, and Reinder Wit, it gives such error: Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -50 [code] => -50 [2] => This function only works with statements that have static or keyset scrollable cursors. [message] => This function only works with statements that have static or keyset scrollable cursors. ) ) – Hasan Sait Arslan May 21 '14 at 13:50

1 Answers1

0

you might having an error in your database connection, or in your SQL statement. Check if $conn is set using print_r($conn), if is correctly setted, then you should check SQL statement . Note that in PHP, SQL execution errors will not raise an exception, but a warning message, so this will not be catched in a try catch statement.

Tarciso Junior
  • 549
  • 9
  • 16
  • I will try now, thanks Tarciso Junior. :) – Hasan Sait Arslan May 21 '14 at 13:34
  • now I got such an error, Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -50 [code] => -50 [2] => This function only works with statements that have static or keyset scrollable cursors. [message] => This function only works with statements that have static or keyset scrollable cursors. ) ) – Hasan Sait Arslan May 21 '14 at 13:51