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 Code:
<?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 Code:
$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 Code:
mssql_select_db($database, $connect) or die(mssql_get_last_message());
Just selecting the database MuOnline ($database) with the connection from $connect
PHP Code:
$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 Code:
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 Code:
$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 Code:
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 Code:
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:
PHP: mssql_connect - Manual
PHP: mssql_select_db - Manual
PHP: mssql_fetch_array - Manual
PHP: Variables - Manual
This ends up the tutorial for now.
If you have any question feel free to ask.