mySQL, php query

Discussion in 'PHP' started by Lucy, May 5, 2007.

  1. #1
    Hi,

    Let's say I have a database created in MySQL containing 2 different tables. For instance table 1 is called people and table 2 is called interests.

    I know how to retrieve a query when there's one table from a database, for instance:

    // Define a query that retrieves the names
    $query = "SELECT first_name, last_name FROM people";



    However, what do I do when I want to get the info. from 2 different tables. I'm doing what's below but this isn't working. Any advice or direction would be greatly appreciated.


    // Run the query and store the result
    $result = mysqli_query($link, $query);

    // Define a query that retrieves the names and interest
    $query = "SELECT first_name, last_name FROM people, AND sport, hobby FROM interests";

    // Run the query and store the result
    $result = mysqli_query($link, $query);
     
    Lucy, May 5, 2007 IP
  2. cowguru2000

    cowguru2000 Member

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #2
    1. What is the error that you are getting?

    2. Is your version of MySQL the one where you should use mysqli or just mysql?

    3. Try splitting it up into 2 queries.
     
    cowguru2000, May 5, 2007 IP
  3. Lucy

    Lucy Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I tried that by doing this (below) and it didn't work. I'm new at this so I've done it wrong I bet:

    // Run the query and store the result
    $result = mysqli_query($link, $query);

    // Define a query that retrieves the names and interest
    $query = "SELECT first_name, last_name FROM people";

    // Run the query and store the result
    $result = mysqli_query($link, $query);

    // Run the query and store the result
    $result = mysqli_query($link, $query);

    // Define a query that retrieves the names and interest
    $query = "SELECT sport, hobby FROM interests";

    // Run the query and store the result
    $result = mysqli_query($link, $query);
     
    Lucy, May 5, 2007 IP
  4. cowguru2000

    cowguru2000 Member

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #4
    Try instead of using
    mysqli_query($link, $query);
    Code (markup):
    use
    mysql_query($query)
    Code (markup):
     
    cowguru2000, May 5, 2007 IP
  5. cowguru2000

    cowguru2000 Member

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    Also, I don't understand why you're doing this:
    
    // Run the query and store the result 
    $result = mysqli_query($link, $query); 
    
    // Define a query that retrieves the names and interest
    $query = "SELECT first_name, last_name FROM people"; 
    
    // Run the query and store the result 
    $result = mysqli_query($link, $query);
    
    // Run the query and store the result 
    $result = mysqli_query($link, $query); 
    
    // Define a query that retrieves the names and interest
    $query = "SELECT sport, hobby FROM interests"; 
    
    // Run the query and store the result 
    $result = mysqli_query($link, $query);
    
    PHP:
    It seems as if you have extraneous query functions. Try getting rid of some and using this:

    
    
    // Define a query that retrieves the names and interest
    $query1 = "SELECT first_name, last_name FROM people"; 
    
    // Run the query and store the result 
    $result1 = mysqli_query($link, $query1); 
    
    // Define a query that retrieves the names and interest
    $query2 = "SELECT sport, hobby FROM interests"; 
    
    // Run the query and store the result 
    $result2 = mysqli_query($link, $query2);
    
    PHP:
     
    cowguru2000, May 5, 2007 IP
  6. Lucy

    Lucy Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanks... but still didn't work... Is there are way I could make query on 1 line somehow

    $query = "SELECT first_name, last_name FROM people AND sport, hobby FROM interests";
     
    Lucy, May 5, 2007 IP
  7. Lucy

    Lucy Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Don't worry I finally worked it out.

    $query = "SELECT first_name, last_name, sport, hobby FROM people, interests";

    Thanks.
     
    Lucy, May 5, 2007 IP