Simple Question

Discussion in 'MySQL' started by Maneel, Mar 27, 2008.

  1. #1
    Can somebody help me with mySQL ? I want to create a database + table in mySQL with the following columns:

    1. Name
    2. Type
    3. Runs
    4. Wickets
    5. Team
    6. Matches


    After I've put in records in the mySQL table, I want to be able to fetch these records on HTML pages.

    Can I get some help, this is sort of urgent ?
     
    Maneel, Mar 27, 2008 IP
  2. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could use PHPMyAdmin to create the table and insert the data.
    Then, you'll need to write a script in PHP that'll read the data from the table and displays it in a HTML page.
     
    CreativeClans, Mar 27, 2008 IP
    Maneel likes this.
  3. Maneel

    Maneel Peon

    Messages:
    753
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Can I get an example of such a script ? I am sure after seeing an example, I can code my own.
     
    Maneel, Mar 27, 2008 IP
  4. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    $link = mysql_connect($DBHost, $DBUser, $DBPassword) or die("Impossible connecting to DB - error: ".mysql_errno()." - ".mysql_error());
    mysql_select_db($cleanDBName) or die("Impossible selecting DB - error: ".mysql_errno()." - ".mysql_error());
    
    $query = "SELECT field1, field2 FROM tablename";
    $result = mysql_query($query) or die("MySQL error - ".mysql_errno()." - ".mysql_error());
    
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
    {
       echo "field1: " . $line['field1'] . "<br/>";
       echo "field2: " . $line['field2'] . "<br/>";
    } 
    
    PHP:
     
    CreativeClans, Mar 27, 2008 IP
  5. Maneel

    Maneel Peon

    Messages:
    753
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #5
    1. If I put this script on the page, won't my DB details like username, password etc be visible to anyone ?
    2. I plan to display an image along with each record, can I store the image path with the record and retreive it ? If yes, then how ?
     
    Maneel, Mar 27, 2008 IP
  6. Maneel

    Maneel Peon

    Messages:
    753
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Another query, I plan to create a seperate html page for each record, can I add a having field1='abvdcdjas' clause in the statement.
     
    Maneel, Mar 27, 2008 IP
  7. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    1. You put the script in a file that has the .php extension, like this:

    example.php
    
    <?php
      $link = mysql_connect($DBHost, $DBUser, $DBPassword) or die("Impossible connecting to DB - error: ".mysql_errno()." - ".mysql_error());
      mysql_select_db($cleanDBName) or die("Impossible selecting DB - error: ".mysql_errno()." - ".mysql_error());
    
      $query = "SELECT field1, field2 FROM tablename";
      $result = mysql_query($query) or die("MySQL error - ".mysql_errno()." - ".mysql_error());
    ?>
    <html>
    <head>
    <title>title</title>
    </head>
    <body>
    <?php
      while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
      {
         echo "field1: " . $line['field1'] . "<br/>";
         echo "field2: " . $line['field2'] . "<br/>";
      }
    ?>
    </body>
    </html>
    
    PHP:
    Nobody will see your username and password, because all PHP code will be executed server side, and the user will only see the endresult.

    2. Yes you can. Add a field (for example a VARCHAR(255)) to your table. Add it to the query in the script. Then, in the while loop, add an echo statement for the <img> .

    3. Yes, just add a WHERE field1 = 'whatever' to the query.
     
    CreativeClans, Mar 27, 2008 IP
  8. Maneel

    Maneel Peon

    Messages:
    753
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks, reps added.
     
    Maneel, Mar 27, 2008 IP