php and SQL example

Discussion in 'PHP' started by FireElement, Oct 15, 2007.

  1. #1
    Can some one tell me a very basic way to set up a sql database with phpMyAdmin. Say an example with one row and give me the conect code to show this row on a php page.

    The way to set up the database for the following bit of code would be useful! As am having problems. I can't seem to get my head round how you call a table from a sql database. I can set them up fine. I seen enough tutorials and used access and excell enough to know how to set up databases like this quite easy. I just dont get how to call it and display it using php. Any help would be greatly appreciated. Thanks.

    
    <?php
    
    $c =mysql_connect(localhost,root, "");
    mysql_select_db(user.$c);
    
    for($i=1; $i<=50;$i++) 
    {
      $sql = "SELECT * FROM `users` WHERE `username` = $i";
      $res = mysql_query($sql) or die(mysql_error());
      $row = mysql_fetch_assoc($res);
      echo "Row $row[id] has the username of $row[username]<br>\n";
    }
    
    ?>
    
    Code (markup):
     
    FireElement, Oct 15, 2007 IP
  2. Ned

    Ned Active Member

    Messages:
    402
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    78
    #2
    
    <?php
    
    $c =mysql_connect(localhost,root, "");
    mysql_select_db(user,$c);
    
    for($i=1; $i<=50;$i++) 
    {
      $sql = "SELECT * FROM users WHERE username = '$i'";
      $res = mysql_query($sql);
      $myrow = mysql_fetch_array($res);
      echo "Row $myrow['id'] has the username of $myrow['username']<br>\n";
    }
    
    ?>
    
    Code (markup):
     
    Ned, Oct 15, 2007 IP
    SERPalert likes this.
  3. grikis

    grikis Banned

    Messages:
    333
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <?php
    $c = mysql_connect("localhost", "username", "password");
    mysql_select_db("user", $c);
    $res = mysql_query("SELECT * FROM users WHERE username = '$i' LIMIT 0, 50");
    while ($row = mysql_fetch_assoc($res))
    {
    echo "Row ".$row['id']." has the username of ".$row['username']."<br>";
    }
    ?>
     
    grikis, Oct 15, 2007 IP
  4. FireElement

    FireElement Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    :confused:

    I am using apache server on my computer, the server is "localhost", username is "root", there is no password so its "", I created a database called "test" and a table called "poo".

    How would I connect to this table and be able to add data to it. I have added. three rows. ID, name, surname. So you could make a basic form input name and surname and get it to post it to the table. I have no problem writing the code for this but everytime I try to I seem to have problem connecting to the database as it goes NO Database found or brings up an error.

    Any help would be great! Thanks!
     
    FireElement, Oct 15, 2007 IP
  5. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Not entirely sure what you're asking.

    Ned points out that your mysql_select_db had an error.
    grikis points out that you're better of selecting all the rows and outputing them in a loop though his code won't work.

    I think however you're asking how to setup the database table in the first place.
     
    jnestor, Oct 15, 2007 IP
  6. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    mysql_connect("localhost", "root", "");
    mysql_select_db("test");
     
    jnestor, Oct 15, 2007 IP
  7. grikis

    grikis Banned

    Messages:
    333
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    <?php
    $c = mysql_connect("localhost", "root", "");
    mysql_select_db("test", $c);
    $res = mysql_query("SELECT * FROM poo");
    while ($row = mysql_fetch_assoc($res))
    {
    echo "Id - ".$row['id']." Name - ".$row['name']." Surname - ".$row['surname']."<br>";
    }
    ?>

    This code will display all records
     
    grikis, Oct 15, 2007 IP
  8. FireElement

    FireElement Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks a lot for anyone who posted a reply!

    I realise now what I was doing wrong. I did not understand that I was opening the database at the start. Then I was trying to open it again later.

    It was this bit that confused me ("SELECT * FROM poo");
    I was trying to ("SELECT * FROM test"); and it was not working!

    Basically I did not understand properly how to select a row out of a table and call it.

    Thanks for any help I understand it now.

    Cheers all!
     
    FireElement, Oct 15, 2007 IP