Basic mssql and php

Results 1 to 17 of 17
  1. #1
    Account Upgraded | Title Enabled! MemmeR is offline
    MemberRank
    May 2005 Join Date
    DenmarkLocation
    753Posts

    Basic mssql and php

    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.


  2. #2
    Account Upgraded | Title Enabled! Alwayz is offline
    MemberRank
    Oct 2007 Join Date
    213Posts

    Re: [Guide] Basic mssql and php

    nice guide...

  3. #3
    Alpha Member bramdebouvere is offline
    MemberRank
    Aug 2006 Join Date
    BelgiumLocation
    2,409Posts

    Re: [Guide] Basic mssql and php

    Nice guide.. will be usefull for a lot of people

  4. #4
    Account Upgraded | Title Enabled! MemmeR is offline
    MemberRank
    May 2005 Join Date
    DenmarkLocation
    753Posts

    Re: [Guide] Basic mssql and php

    Thank you :P

  5. #5
    [Czt] Coder Team Member noobies is offline
    MemberRank
    Aug 2005 Join Date
    Behind you !!Location
    747Posts

    Re: [Guide] Basic mssql and php

    Good guide man :)

  6. #6
    Apprentice mosberg is offline
    MemberRank
    May 2006 Join Date
    BulgariaLocation
    21Posts

    Re: [Guide] Basic mssql and php

    wow this is very good for beginners like me :) thank you

  7. #7
    Account Upgraded | Title Enabled! MemmeR is offline
    MemberRank
    May 2005 Join Date
    DenmarkLocation
    753Posts

    Re: [Guide] Basic mssql and php

    Your welcome

  8. #8
    Member TZM is offline
    MemberRank
    Nov 2007 Join Date
    LatviaLocation
    72Posts

    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 by TZM; 08-03-08 at 02:57 AM.

  9. #9
    Proficient Member nokipa is offline
    MemberRank
    Jan 2007 Join Date
    156Posts

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

  10. #10
    Account Upgraded | Title Enabled! MemmeR is offline
    MemberRank
    May 2005 Join Date
    DenmarkLocation
    753Posts

    Re: [Guide] Basic mssql and php

    O.o

    no idea ^^

  11. #11
    Legend MuIsBest is offline
    LegendRank
    Dec 2006 Join Date
    NorwayLocation
    2,144Posts

    Re: [Guide] Basic mssql and php

    nice guide, usefull.. :)

  12. #12
    Account Upgraded | Title Enabled! MemmeR is offline
    MemberRank
    May 2005 Join Date
    DenmarkLocation
    753Posts

    Re: [Guide] Basic mssql and php

    Quote Originally Posted by MuIsBest View Post
    nice guide, usefull.. :)
    gaah stop posting dumb comments like nice

    Im glad you appreciate it, but hard to see when you only type 3 words...

  13. #13
    Legend MuIsBest is offline
    LegendRank
    Dec 2006 Join Date
    NorwayLocation
    2,144Posts

    Re: [Guide] Basic mssql and php

    well, i tested :S

  14. #14
    Account Upgraded | Title Enabled! MemmeR is offline
    MemberRank
    May 2005 Join Date
    DenmarkLocation
    753Posts

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

  15. #15
    Legend MuIsBest is offline
    LegendRank
    Dec 2006 Join Date
    NorwayLocation
    2,144Posts

    Re: [Guide] Basic mssql and php

    k, i wont test your files more lol :S -_-
    (and i wont like comment your threads)

    (Danske gut!) =)

  16. #16
    Account Upgraded | Title Enabled! MemmeR is offline
    MemberRank
    May 2005 Join Date
    DenmarkLocation
    753Posts

    Re: [Guide] Basic mssql and php

    If your comment are good then please do if not thank you..

  17. #17
    hello danse is offline
    MemberRank
    Jun 2004 Join Date
    1,809Posts

    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 bitch:
    good uber guide 10/10.



Advertisement