0

I have a database containing two tables:

  1. products_attributes ( id , image )
  2. options_values ( id , text )

The id value is the same in both tables. Now I need to output the text and the image based on the same id from both tables.

Then possibly base this on the loop. I have created something like this, but it doesn't work correctly.

$sqlquery = "SELECT * FROM products_attributes JOIN options_values WHERE BY
 id=id ASC LIMIT 0,40";
$sqlresult = @mysql_query($sqlquery);
while ($content = mysql_fetch_array($sqlresult, MYSQL_NUM)) {
    echo "<img src='$content[1]'/> $content[2]";
}

@EDIT

So the query ended up like this, however I can not get the attributes_image (table column name) to display. It does however work using the numerical system.

$sqlquery = "SELECT * FROM zen_products_attributes pa JOIN zen_products_options_values ov ON pa.options_values_id = ov.products_options_values_id LIMIT 0,40;";
$sqlresult = @mysql_query ($sqlquery);
while ($content = mysql_fetch_array($sqlresult, MYSQL_NUM)){

    echo $content['attributes_image'];

;}
0

5 Answers 5

2

Use

   $sqlquery = "SELECT * FROM products_attributes JOIN options_values 
               ON products_attributes.id=options_values.id ASC LIMIT 0,40";
Sign up to request clarification or add additional context in comments.

3 Comments

just curious what does 'WHERE BY ' does?
@Mrcoder oops my bad!!!i have copied the query from OP's post,didnt recognized it,thanks for correcting
How do I go about displaying the results in a PHP $content form, using your example?
1

1:You need to use alias for tables when you are joining on same column name to avoid ambiguity.i.e 'id=id' is wrong.

2.Syntax error.there is no 'WHERE BY' in mysql.

$sqlquery = "SELECT * FROM 
 products_attributes pa 
JOIN options_values ov 
on pa.id=ov.id ASC LIMIT 0,40";

Edit answer:

while ($content = mysql_fetch_array($sqlresult, MYSQL_NUM)){

    echo $content['attributes_image'];

;}

--^(extra semi colon,remove this)

14 Comments

How do I go about outputting the data from those tables. As I'd like it to only display data that has the exact the same id. ie. Table 1 - id:1 Green, id:2 Red, id:3 Yellow Table 2 - id:1 green.jpg, id:2 red.jpg, id:3 yellow.jpg The PHP quarry will display the text and the image based on the same ID.
@DamianJanik please elaborate or if possible update your question.
@DamianJanik the query i have written will do the same as we are joining on id value.
What I mean is how would I go about using PHP in this case. As previously I was able to retrieve the data using $content[1], now I have two tables and not sure how to go about getting it from both tables. As my tables contain more columns and I need to specify the data I'd like to retrieve.
just give the index as name like this $content['id'],$content['image']
|
0

if you have fields with same name give them alias.

SELECT p.`desc` as description, v.`desc` FROM products_attributes as p
JOIN options_values as v ON  p.id=v.id
ASC LIMIT 0,40

Comments

0
SELECT
    *
FROM
    products_attributes pa
JOIN
    options_values ov
ON
    pa.id = ov.id
LIMIT
    0,40;

Comments

0
SELECT * FROM products_attributes AS pa JOIN options_values AS ov ON pa.id=ov.id ORDER BY ov.id ASC LIMIT 0,40

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.