How to get specific records from a table?

Discussion in 'MySQL' started by LeftoverSoup, Sep 13, 2009.

  1. #1
    I have a form that inputs data into my database. The table is called "profile" and it has 3 fields... "ip" "name" and "bio" the data goes into the database great, but it’s just how to display it is the problem... I want to be able to display the name and the bio, just by using the ip address, for you to understand this a bit better, this is my attempt...

    $currentuserinfo = mysql_query( "SELECT * FROM profile
     WHERE ip = $ip and show bio + name " ) ;
    Code (markup):
    There will be a new page for this... So if you went to http://mysite.com/index.php?ip=34.433.2344 I want that to show the users bio and name.


    Oh and 1 other thing, on my form it creates a profile for the user when submitted. But rather than having duplicate ip's in my database i want it to update the data if the ip address is already in my database, rather than adding a new user under that ip everytime something is changed. This is my current code for adding a new profile, but i need it to be able to update it instead of just adding new profiles even for the same ip..

    $query = "UPDATE and INSERT INTO profile VALUES
    ('$ip','$name','$bio')";
    Code (markup):
    The above 2 codes do not work there just to try and better illustrate what I need done. If you could help me out here it would be greatly appreciated!!
     
    LeftoverSoup, Sep 13, 2009 IP
  2. LeftoverSoup

    LeftoverSoup Guest

    Messages:
    95
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Anyone help? I'll pay $5 if you get it fully working?
     
    LeftoverSoup, Sep 14, 2009 IP
  3. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #3
    Your pretty far off. Its hard to help without seeing everything though.

    The first query would be something like..

    $query = mysql_query( "SELECT * FROM profile
     WHERE ip = $ip " ) ;
    PHP:
    This will return all the data in profile. Thats what the SELECT * is (* = all). If you wanted just IP you would SELECT IP. So it returns bio and name also. But now you have to get that info. This would go right below the other line.

    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_array($result) or die(mysql_error());
    echo $row['bio']. " - ". $row['name'];
    
    PHP:
    I dont know what your fields are called but when you want the info its $row['fieldname'].

    Try that. If it dont work then read

    http://www.tizag.com/mysqlTutorial/mysqlquery.php
    http://www.tizag.com/mysqlTutorial/mysqlfetcharray.php
    http://www.tizag.com/mysqlTutorial/mysqlselect.php
     
    aaron_nimocks, Sep 14, 2009 IP
  4. LeftoverSoup

    LeftoverSoup Guest

    Messages:
    95
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for your time :) +rep

    I added that code to the correct section but my site is now saying "Query was empty" when loaded. I'm going to study those links you sent me and try and find out the source of the problem. thanks again
     
    LeftoverSoup, Sep 14, 2009 IP
  5. LeftoverSoup

    LeftoverSoup Guest

    Messages:
    95
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Changed a few things around and your code works great! :) thanks.


    any idea for a solution to the second problem?
     
    LeftoverSoup, Sep 14, 2009 IP
  6. n00ne

    n00ne Member

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #6
    u need some queries for second problem:

    1. select by ip.
    2. if result of query !=0 then next query update. if result of query =0 then next query add.
     
    n00ne, Sep 15, 2009 IP