Mohanddo Posted November 22, 2009 Share Posted November 22, 2009 Hello i just started learning php yesterday and i wanted to make a simple login system. Im using a mssql database for this and no matter what i do im just not getting any results. Ive enabled mssql extension in php.ini already. Here is the php code: <?php $username = $_POST['username']; $password = $_POST['password']; $server = '127.0.0.1\SQLEXPRESS'; $link = mssql_connect($server, 'username', 'pass'); if(!$link) { die('Error connecting to server'); } mssql_select_db('ACCOUNT_DBF', $link); $query = mssql_query("SELECT account, password FROM ACCOUNT WHERE account = '$username' "); $loginrow = mssql_fetch_assoc($query); echo $loginrow['account']; ?> After i press the submit button i just get a blank page. Am i going wrong somewhere? Thnx for your time. Quote Link to comment https://forums.phpfreaks.com/topic/182488-php-mssql-help/ Share on other sites More sharing options...
waynew Posted November 22, 2009 Share Posted November 22, 2009 Do you have errors and notices set to on? Quote Link to comment https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963148 Share on other sites More sharing options...
Mohanddo Posted November 22, 2009 Author Share Posted November 22, 2009 I think so: error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED Quote Link to comment https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963150 Share on other sites More sharing options...
rajivgonsalves Posted November 22, 2009 Share Posted November 22, 2009 Does the page load, or its in loading, I think php is not connecting to mssql server you'll have to check your connection string. Quote Link to comment https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963163 Share on other sites More sharing options...
cags Posted November 22, 2009 Share Posted November 22, 2009 The OP is checking $link and if it is FALSE is exiting the script using die, therefore it should be connected fine. I'd guess the Query is either failing or simply not finding a row that matches. Unfortunately, never having worked with mssql I'm not sure how you'd go about debugging it correctly. Checking if $query is equal to FALSE before passing it to mssql_fetch_assoc would be a good start though. Quote Link to comment https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963168 Share on other sites More sharing options...
rajivgonsalves Posted November 22, 2009 Share Posted November 22, 2009 actually I took the script and ran it on localhost it kept loading(Blank page) I do not have a ms sql server running but unless it times out I guess it won't show the error Quote Link to comment https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963171 Share on other sites More sharing options...
cags Posted November 22, 2009 Share Posted November 22, 2009 Well if I run it I get... Notice: Undefined index: username in C:\xampp\htdocs\sandbox\index.php on line 3 Notice: Undefined index: password in C:\xampp\htdocs\sandbox\index.php on line 4 Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: 127.0.0.1\SQLEXPRESS in C:\xampp\htdocs\sandbox\index.php on line 6 Error connecting to server Quote Link to comment https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963173 Share on other sites More sharing options...
rajivgonsalves Posted November 22, 2009 Share Posted November 22, 2009 hmm.. still does not load on mine, anyways I guess you'd pretty much hit the hammer on the nail Quote Link to comment https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963175 Share on other sites More sharing options...
Mohanddo Posted November 22, 2009 Author Share Posted November 22, 2009 Well if I run it I get... Notice: Undefined index: username in C:\xampp\htdocs\sandbox\index.php on line 3 Notice: Undefined index: password in C:\xampp\htdocs\sandbox\index.php on line 4 Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: 127.0.0.1\SQLEXPRESS in C:\xampp\htdocs\sandbox\index.php on line 6 Error connecting to server Well of course you will get this error because there are no POST variables being sent to your page and you do not have a mssql server installed. But thnx to everyone for replies i will try to check the string and also execute it to see results. Is the syntax fine? Im not too good with encapsulting quotes Quote Link to comment https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963179 Share on other sites More sharing options...
Mohanddo Posted November 22, 2009 Author Share Posted November 22, 2009 Something i want to make sure also: In my query function $query = mssql_query("SELECT account, password FROM ACCOUNT WHERE account = '$username' "); The query by itself has to be SELECT acccount, password FROM ACCOUNT WHERE account="usernamehere" Does my line for this query also include the quotes around usernamehere? This is the only thing i can think of now... i also executed the query inside management studio and it worked fine. Quote Link to comment https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963188 Share on other sites More sharing options...
cags Posted November 22, 2009 Share Posted November 22, 2009 Obviously I should get those message since as you say I don't have the $_POST values set nor an mssql installation, I was simply pointing it out since rajivgonsalves said they didn't get an error. Does $query have a value other than FALSE? I don't know about MsSQL specifically but in general SQL strings should be encased in quotes so I imagine the same applies to the MS 'flavour'. Quote Link to comment https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963192 Share on other sites More sharing options...
Mohanddo Posted November 22, 2009 Author Share Posted November 22, 2009 How can i check that it returns false? Like this? if($query==false) { echo "QUERY returned FALSE"; } sorry i am very new to php lol Quote Link to comment https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963196 Share on other sites More sharing options...
cags Posted November 22, 2009 Share Posted November 22, 2009 Probably best to use three equal signs rather than two, but it should work either way. Yes, thats the general idea. Quote Link to comment https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963198 Share on other sites More sharing options...
Mohanddo Posted November 22, 2009 Author Share Posted November 22, 2009 I tried both double and triple and still im getting a blank page. What is the difference between == and === ? Also i think i noticed that in mysql you dont need to have quotes around VARCHAR datatypes however in mssql it is nessecary. I know im being a little picky on this but does my query definetly have quotes around the string $username? Do the single quotes around $username just represent that $username will be a string or are they actually quotes that the query will have? Quote Link to comment https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963201 Share on other sites More sharing options...
cags Posted November 22, 2009 Share Posted November 22, 2009 The double equals checks if something is equal, the triple quote checks if they are exactly equal (including data type). For example 0 is == to FALSE but it is not === to FALSE. In that specific case it probably didn't matter, but it's good to get in the habit of using it when your actually checking for FALSE. Try it out... if(0==FALSE) { echo "Match"; } else { echo "No Match"; } if(0===FALSE) { echo "Match"; } else { echo "No Match"; } In MySQL VARCHAR entries in a database should have quotes around them. You can easily debug with MySQL by echo'ing out mysql_error a quick look at PHP.net seems to indicate that mssql_get_last_message is approximately the equivalent. So try echo'ing that out before echo'ing out the $loginrow value. If MsSQL is throwing any errors you'll then know what it is. Quote Link to comment https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963214 Share on other sites More sharing options...
Mohanddo Posted November 22, 2009 Author Share Posted November 22, 2009 Tried that and still getting a blank page Quote Link to comment https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963220 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.