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);
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.
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);
Try instead of using mysqli_query($link, $query); Code (markup): use mysql_query($query) Code (markup):
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:
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";
Don't worry I finally worked it out. $query = "SELECT first_name, last_name, sport, hobby FROM people, interests"; Thanks.