TStyles Posted August 11, 2016 Share Posted August 11, 2016 I am new to PHP and this is the first try to code.I have a login Form with Email and password,when the user submits the form ,there should be a connection to a MS SQL Server and then Check to see if the values are valid.check1 is being displayed but everything after is not.Is this the correct way to connect to MS sql server?Most articles on the internet are using MYSQL so i can't find much info on SQL Server. PHP code is present on the same page as the html.I am desperate to know why the code isn't working,Thanks for the help.Here is what i have tried so Far: HTML code: <form class="form-horizontal" method="post" action="Login.php"> <input type="email" id="inputEmail" class="form-control" name="username" placeholder="Email address" required > <input type="password" id="inputPassword" class="form-control" name="password" placeholder="Password" required > <button class="btn btn-lg btn-primary btn-block" type="submit" name='Submit' value='Submit'>Login</button> </form> PHP Code: if (isset($_POST['Submit'])) { Login(); } function Login() { $username = trim($_POST['username']); $password = trim($_POST['password']); $serverName = "TAREX-09\MSSQLSERVER"; //name of server(locally) $connectionOptions = array("Database" => "Insurance"); echo 'check1'; //Connect using Windows Authentication. $conn = sqlsrv_connect($serverName, $connectionOptions); echo 'check2'; if (!$conn) { echo 'failed'; //die(FormatErrors(sqlsrv_errors())); } $tsql = "SELECT EmailAddress,Password FROM Customer_Detail WHERE EmailAddress=$username AND Password=$password "; $getlogin = sqlsrv_query($conn, $tsql); if ($getlogin === false) { echo 'Login Failed'; } else { echo 'Login Success'; } Quote Link to comment https://forums.phpfreaks.com/topic/301855-code-is-not-working-trying-to-login-using-sql-server/ Share on other sites More sharing options...
Barand Posted August 11, 2016 Share Posted August 11, 2016 To connect using SQL Server Authentication, include "UID" and "PWD" in the connection options array. You have only the database name Quote Link to comment https://forums.phpfreaks.com/topic/301855-code-is-not-working-trying-to-login-using-sql-server/#findComment-1535983 Share on other sites More sharing options...
Jacques1 Posted August 11, 2016 Share Posted August 11, 2016 (edited) Check the PHP error log. The sqlsrv_* functions may not be available in your current setup. In generally, I don't recommend using MS SQL, unless you absolutely must. Compared to a common open-source database system like MySQL/MariaDB or PostgreSQL, you'll have a lot more trouble and a lot less support. If you do choose it, be prepared for your own research. You have only the database name He's using Windows Authentication, and even if the function fails, this doesn't crash the whole script. Edited August 11, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/301855-code-is-not-working-trying-to-login-using-sql-server/#findComment-1535985 Share on other sites More sharing options...
TStyles Posted August 11, 2016 Author Share Posted August 11, 2016 (edited) Check the PHP error log. The sqlsrv_* functions may not be available in your current setup. In generally, I don't recommend using MS SQL, unless you absolutely must. Compared to a common open-source database system like MySQL/MariaDB or PostgreSQL, you'll have a lot more trouble and a lot less support. If you do choose it, be prepared for your own research. He's using Windows Authentication, and even if the function fails, this doesn't crash the whole script. I kinda have to use MS SQL, i'm not getting any error when i attempr to login,it's just that only the first echo is working ... i already have the sql drivers for php installed and configured. I'm using IIS too. Edited August 11, 2016 by TStyles Quote Link to comment https://forums.phpfreaks.com/topic/301855-code-is-not-working-trying-to-login-using-sql-server/#findComment-1535988 Share on other sites More sharing options...
Jacques1 Posted August 11, 2016 Share Posted August 11, 2016 PHP doesn't just crash for no reason. Have you checked the error log of IIS? Errors don't necessarily pop up on the screen, but they do get logged -- unless your PHP configuration is completely broken. Quote Link to comment https://forums.phpfreaks.com/topic/301855-code-is-not-working-trying-to-login-using-sql-server/#findComment-1535989 Share on other sites More sharing options...
TStyles Posted August 11, 2016 Author Share Posted August 11, 2016 (edited) PHP doesn't just crash for no reason. Have you checked the error log of IIS? Errors don't necessarily pop up on the screen, but they do get logged -- unless your PHP configuration is completely broken. Just checked for error logs and there are none in the inetpub directories. How can i see if the sqlsrv_* functions are working correctly? I'll uninstall then reinstall the sql drivers for php,and see if it works. Edited August 11, 2016 by TStyles Quote Link to comment https://forums.phpfreaks.com/topic/301855-code-is-not-working-trying-to-login-using-sql-server/#findComment-1535991 Share on other sites More sharing options...
TStyles Posted August 12, 2016 Author Share Posted August 12, 2016 (edited) PHP doesn't just crash for no reason. Have you checked the error log of IIS? Errors don't necessarily pop up on the screen, but they do get logged -- unless your PHP configuration is completely broken. You have only the database name i want to make sure i have the correct sql drivers installed. can you check it for me please(links are safe ,images uploaded to imgur) here's the files i downloaded from Microsoft's website: http://imgur.com/a/7O84l And here i moved the non thread safe x64 dlls to php ext directory: http://imgur.com/a/8Hz71 Finally i added them to the extensions in php.ini : http://imgur.com/a/TIGoB anything more i need to do? because i still don't have anything related to sqlserv when i go to phpinfo() page on the server! Edited August 12, 2016 by TStyles Quote Link to comment https://forums.phpfreaks.com/topic/301855-code-is-not-working-trying-to-login-using-sql-server/#findComment-1536036 Share on other sites More sharing options...
kicken Posted August 14, 2016 Share Posted August 14, 2016 (edited) Did you match the dll versions properly (nts vs ts, x86 vs x64)? Did you ensure to check the System requirements? Namely: The correct version of the Microsoft ODBC Driver for SQL Server or SQL Server Native Client is required on the computer where PHP is running. For version 4 you need Microsoft ODBC Driver 11 for SQL Server or Microsoft ODBC Driver 13 for SQL Server Lastly, I'd suggest using the PDO interface, not the sqlsrv_* functions. It's easier. //NULL username/password for Windows auth. $db = new PDO('sqlsrv:server=(local);Database=sample', null, null, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ]); $stmt = $db->query('SELECT fname, lname FROM people'); foreach ($stmt as $row){ echo $row['fname'].' '.$row['lname'].'<br>'; } Edited August 14, 2016 by kicken 1 Quote Link to comment https://forums.phpfreaks.com/topic/301855-code-is-not-working-trying-to-login-using-sql-server/#findComment-1536137 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.