user2968767
user2968767

Reputation:

SELECT FROM 2 different tables but related data

I have the following code

<?php

require_once('db_connection.php');
$return_arr = array();

$param = $_GET["term"];

$query = "SELECT *
            FROM exp_weblog_data,exp_weblog_titles WHERE field_id_5
            LIKE '%". $param ."%'
            LIMIT 50";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

/* Retrieve and store in array the results of the query.*/
while ($row = $result->fetch_assoc()) {

    $row_array['jItemCode']             = $row['field_id_5'];
    $row_array['jItemDesc']             = $row['title'];
   /* $row_array['jItemWholesale']        = $row['itemWholesale'];
    $row_array['jItemRetail']           = $row['itemRetail'];
    $row_array['jItemPrice']            = $row['itemPrice'];
    $row_array['jQtyOnHand']            = $row['qtyOnHand'];*/

    array_push( $return_arr, $row_array );
}


$result->free_result();
$mysqli->close();

/* Toss back results as json encoded array. */
echo json_encode($return_arr);

I have two tables. "exp_weblog_data" and "exp_weblog_titles". Each table has "entry_id". When I use "field_id_5" from "exp_weblog_data" to start the autosuggest I need to pull additional information from the "exp_weblog_titles" table

This is for an auto complete query. I need to pull related data from "title" in another table in the same database can someone please help I know the problem lies with my query but I have tried all kinds of syntax with JOINS and UNIONS and LEFT JOINS what have you. Can someone please help me

Upvotes: 1

Views: 129

Answers (2)

user2968767
user2968767

Reputation:

I got it to work this way

$query = "SELECT field_id_5, exp_weblog_titles.title, field_id_57  
          FROM exp_weblog_data, exp_weblog_titles 
          WHERE exp_weblog_titles.entry_id = exp_weblog_data.entry_id AND            field_id_5
          LIKE '%". $param ."%'
          LIMIT 10";

Thanks for all the help guys!

Upvotes: 2

David Ansermot
David Ansermot

Reputation: 6112

Use a JOIN clause. You can with one request get related data from 2 or more tables. http://dev.mysql.com/doc/refman/5.0/en/join.html

Upvotes: 0

Related Questions