Jump to content

PHP mssql help


Mohanddo

Recommended Posts

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?  :confused:

 

Thnx for your time.

Link to comment
https://forums.phpfreaks.com/topic/182488-php-mssql-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963168
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963173
Share on other sites

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  :shrug:

Link to comment
https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963179
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963188
Share on other sites

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'.

Link to comment
https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963192
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963201
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/182488-php-mssql-help/#findComment-963214
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.