pasujemito
pasujemito

Reputation: 332

MySQL Output different values by the same id

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'];

;}

Upvotes: 0

Views: 1249

Answers (5)

R R
R R

Reputation: 2956

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)

Upvotes: 1

sas
sas

Reputation: 2597

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

Upvotes: 0

Peon
Peon

Reputation: 8020

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

Upvotes: 0

rray
rray

Reputation: 2556

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

Upvotes: 0

Cris
Cris

Reputation: 13351

Use

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

Upvotes: 2

Related Questions