SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Basic PHP

  1. #1
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Basic PHP

    Hey, I'm currently running into a brick wall with getting some php to take the values from a table and display them:

    In my database I have a table called Team.
    It has three columns:
    TeamID---Teamname---AverageRating

    I just want to take all the values in the table and display them.

    So first my connection code to connect to the database:
    Code PHP:
    <?php
     $Conn = mssql_connect("****","****","****") or die("ERROR connection server");
     
     echo 'Connection to server etablished<br />';
     
     $Db=mssql_select_db("sql0605353",$Conn);
     if($Db){
      echo '$Db is good<br /><br />';
      $query = mssql_query("SELECT * FROM team");
      echo 'Query is good :) There are '.mssql_num_rows($query).' records<br />';
     }
     ?>

    This on it own seems to work and when I run it I get:
    Connection to server etablished
    $Db is good

    Query is good There are 3 records

    Which is correct there are 3 records in the table. So I assume there no problems with the connection:

    The next task is to output the data onto a page:
    So I tried:
    Code PHP:
    <?php
    while($row = mysql_fetch_array($query))
      {
      echo $row['Teamname'] . " " . $row['AverageRating'];
      echo "<br />";
      }
      mysql_close($con);
     
    ?>

    But when I run this it doesnt change the page at all. It still shows:
    Connection to server etablished
    $Db is good

    Query is good There are 3 records


    but nothing else, I maybe dont understand the code correctly but should it not be showing the rows from the table?

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Easy mistake to make.

    First of all, you would know what was wrong if you display errors:
    PHP Code:
    error_reporting(E_ALL);
    ini_set('display_errors'2); 
    As for your problem, I'll show you what functions you're using and maybe you'll notice yourself:
    PHP Code:
    mssql_connect("****","****","****");
    mssql_select_db("sql0605353",$Conn);
    mssql_query("SELECT * FROM team");
    mssql_num_rows($query)
    mysql_fetch_array($query);
    mysql_close($con); 
    Notice the difference with those last two (hint: MySQL != MSSQL).

    Oh, and by the way don't bother using MySQL_Close or MSSQL_Close. The connection's automatically closed at the end of the execution anyway. It only needs to be closed if you have ALOT of processing going on after your last MySQL/MSSQL operation.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, arkinstall

    I've been tearing my hair out over that for a good few hours, I don't know if I feel worse or not that its so easily fixed!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •