Basic mssql and php

Joined
May 18, 2005
Messages
293
Reaction score
7
In this tutorial I am going to show you how to connect to mssql via php

Very basic, but good for beginners :)

Ok lets go

Here is the finished script (I will explain below)
PHP:
<?php

$server = "localhost";
$username = "username";
$password = "password";
$database = "MuOnline";

$connect = mssql_connect($server, $username, $password) or die(mssql_get_last_message());
mssql_select_db($database, $connect) or die(mssql_get_last_message());

$query = mssql_query("SELECT * FROM Character WHERE cLevel > 200") or die(mssql_get_last_message());

while ($row = mssql_fetch_array($query)) {
echo "Account: $row[AccountID] Character name: $row[Name] Level: $row[cLevel]";
}

?>

Ok the first part is setting the variables for the mssql login

PHP:
$server = "localhost"; // This is your server connection. If your running on a localhost like appserver or easyphp this is normally localhost

$username = "username"; // This is your login username. This is normally sa.

$password = "password"; // This is the password for your username. You set the password under the installation of SQL Server.

$database = "MuOnline"; // This is the database where all the information is stored in.



$connect = mssql_connect($server, $username, $password) or die(mssql_get_last_message()); // Here we connect to the mssql server with the username and password that we have set in the variables.

The last part: or die mssql_get_last_message means we will get the last message that mssql has given. This is good for error reporting.


PHP:
mssql_select_db($database, $connect) or die(mssql_get_last_message());

Just selecting the database MuOnline ($database) with the connection from $connect


PHP:
$query = mssql_query("SELECT * FROM Character WHERE cLevel > 200")

Now this is where it gets interessting.

The first part: SELECT * means we select all the data in a table

If we say SELECT AccountID instet we will only select the account id/name

FROM Character is just the table we are selecting the data from

WHERE cLevel > 200 this means we will only select all data where the level is higher then 200 (> 200)

Now we have selected all data from a table called character where the level is higher then 200

Now we want to retrive that information
We use mssql_fetch_array and a while for that

PHP:
while ($row = mssql_fetch_array($query)) {
We are setting the data in a variable called $row

If you only want the first data from the table you can just make it like this:
PHP:
$row = mssql_fetch_array($query)
Remember to delete the }

So we can echo some information out like AccountID and level (You can choose what ever you want)

PHP:
echo "Account: $row[AccountID] Character name: $row[Name] Level: $row[cLevel]";


If you for example want to echo out how much the vitality the characters have you can say
PHP:
echo $row[Vitality];

If you want to see all the possible data just go to:
SQL Server Enterprise Manage -> A database -> Tables -> A table -> Right click and go to: "Open table -> Return all rows"

Links:





This ends up the tutorial for now.

If you have any question feel free to ask.
 
Re: [Guide] Basic mssql and php

/// Forget it ;) I maked it by my self :) PROBLEM SOLVED !


One question, i have not worked with mssql only with mysql, so what query's are used to change and add some data in database, coz iam making a sms shop and i making script who will update database table with user credits
Code:
<?php

$server = "localhost";
$username = "sa";
$password = "password";
$database = "MuOnline";

$connect = mssql_connect($server, $username, $password) or die(mssql_get_last_message());
mssql_select_db($database, $connect) or die(mssql_get_last_message());


?>
so like this i connect with database, but how to insert in table MEMB_CREDITS user nick name and credits ? what is querry to update ? or insert ? in mysql it was set , but in mssql ?
 
Last edited:
Re: [Guide] Basic mssql and php

mm, may i ask something diff from your guide?

everytime i try to open my table it say's class not registered, and when i try to open it again it say catastrophic failure.... the help says it odbc failure but i get confused what the error ...
 
Re: [Guide] Basic mssql and php

Well good for you

Ragezone has devolped into unserious answears with "I tested" "good release 10/10"

They did not take the time to say "Nice job. I like that you comment everything" or "Good work, but you could improve the mysql script"

If you take a look at a popular ragezone release 2/3 of all the posts in there are like "good job", "nice I test", "10/10", "you rock"

The last 1/3 is serious and a small part is flaming

zomg..
 
Re: [Guide] Basic mssql and php

Very begginer guide, very good & useful.
Well commented ( step by step ) with links to official php function description.

My suggestions are :
  • expand your guides in to lesson by lesson ( ex. next lesson creating account ) - you may develop a full book 'MuOnline webbys - For Dummies'.
  • i would add a downloadable sources, so everyone could download files, as copy n paste work may cause error [ missing letter 'n' so ] wich may cause questions like "I don't know where my error is, plx help mi".

And in the end just cause i'm a meannie *****:
good uber guide 10/10.
 
Back