- 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)
Ok the first part is setting the variables for the mssql login
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.
Just selecting the database MuOnline ($database) with the connection from $connect
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
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:
Remember to delete the }
So we can echo some information out like AccountID and level (You can choose what ever you want)
If you for example want to echo out how much the vitality the characters have you can say
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.
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)) {
If you only want the first data from the table you can just make it like this:
PHP:
$row = mssql_fetch_array($query)
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:
To view the content, you need to sign in or register
To view the content, you need to sign in or register
To view the content, you need to sign in or register
To view the content, you need to sign in or register
This ends up the tutorial for now.
If you have any question feel free to ask.

