co.ador Posted February 8, 2010 Share Posted February 8, 2010 <?php $query1= "SELECT id, name FROM products $result = mysql_query($query1, $connection); while ($row = mysql_fetch_array($result)) { echo'<div id="namerating"> <p class="name">Name:</p><p class="p">'. $row['name'] . '</p> </div>'; $query2="SELECT product_id, variety, price, description,image FROM product_varieties WHERE product_id= "the id in $query1, How can I script it in here?" $result2 = mysql_query($query2, $connection); $row2=mysql_fetch_array($result2) { echo '<div class="firstiteration"> <div class="small"> <p class="size">'. $row2['variety'] . '</p> <img src="'. $row2['image'] . '" width="80" height="90"/> <p class="price">'. $row2['price'] . '</p> <p class="serve1">'. $row2['description'] . '</p> </div>'; } } ?> How can I compare the product_id with the id in $query1? Help... Link to comment https://forums.phpfreaks.com/topic/191418-i-need-help-comparing-two-queries/ Share on other sites More sharing options...
jl5501 Posted February 8, 2010 Share Posted February 8, 2010 You only need 1 query <?php $query1= "p.id, p.name,v.product_id,v.price,v.description,v.image from products p,product_varieties v where p.id = v.product_id FROM products $result = mysql_query($query1, $connection); ?> Link to comment https://forums.phpfreaks.com/topic/191418-i-need-help-comparing-two-queries/#findComment-1009134 Share on other sites More sharing options...
jl5501 Posted February 8, 2010 Share Posted February 8, 2010 sorry <?php $query1= "select p.id, p.name,v.product_id,v.price,v.description,v.image from products p,product_varieties v where p.id = v.product_id $result = mysql_query($query1, $connection); ?> that is correct Link to comment https://forums.phpfreaks.com/topic/191418-i-need-help-comparing-two-queries/#findComment-1009136 Share on other sites More sharing options...
co.ador Posted February 8, 2010 Author Share Posted February 8, 2010 ok the SELECT thank you.. That would be call a join of table? Link to comment https://forums.phpfreaks.com/topic/191418-i-need-help-comparing-two-queries/#findComment-1009137 Share on other sites More sharing options...
jl5501 Posted February 8, 2010 Share Posted February 8, 2010 That is an implicit join yes I am also using table aliases to simplify the typing Link to comment https://forums.phpfreaks.com/topic/191418-i-need-help-comparing-two-queries/#findComment-1009140 Share on other sites More sharing options...
co.ador Posted February 8, 2010 Author Share Posted February 8, 2010 The aliases like p and v. Thank you for the explanation but, I did one mistake I apologizes. I miss to put in the first post that products table has a field called p.colon_id and it is compare with a variable coming from the url call menu. the query alone would look like: $query1= "select p.id, p.name, p.image, p.colon_id from products p where p.colon=" .(int) $_GET['menu'] $result=mysql_query($query1,$connection); $row = mysql_fetch_array($result) { But I wonder how it would look with the implicit join? I have done as below but don't know if it is correct. correct me if I am wrong please. <?php $query1= "select p.id, p.name, p.image, p.colon_id, v.product_id,v.price,v.description,v.image from products p,product_varieties v where p.colon=" .(int) $_GET['menu'] AND p.id = v.product_id $result = mysql_query($query1, $connection); ?> Link to comment https://forums.phpfreaks.com/topic/191418-i-need-help-comparing-two-queries/#findComment-1009143 Share on other sites More sharing options...
jl5501 Posted February 8, 2010 Share Posted February 8, 2010 you forgot to go back into quotes, but essentially correct $query1= "select p.id, p.name, p.image, p.colon_id, v.product_id,v.price,v.description,v.image from products p,product_varieties v where p.colon=" .(int) $_GET['menu'] ."AND p.id = v.product_id"; Link to comment https://forums.phpfreaks.com/topic/191418-i-need-help-comparing-two-queries/#findComment-1009146 Share on other sites More sharing options...
jl5501 Posted February 8, 2010 Share Posted February 8, 2010 with a space inside the quotes before the and Link to comment https://forums.phpfreaks.com/topic/191418-i-need-help-comparing-two-queries/#findComment-1009148 Share on other sites More sharing options...
co.ador Posted February 8, 2010 Author Share Posted February 8, 2010 $query1= "select p.id, p.name, p.image, p.colon_id, v.product_id,v.price,v.description,v.image from products p,product_varieties v where p.colon=" .(int) $_GET['menu'] ." AND p.id = v.product_id "; What's the reason for the quotes before AND thanks. Link to comment https://forums.phpfreaks.com/topic/191418-i-need-help-comparing-two-queries/#findComment-1009149 Share on other sites More sharing options...
co.ador Posted February 8, 2010 Author Share Posted February 8, 2010 $query1= "select p.id, p.name, p.image, p.colon_id, v.product_id,v.price,v.description,v.image from products p,product_varieties v where p.colon=" .(int) $_GET['menu'] ." AND p.id = v.product_id "; $result = mysql_query($query3, $connection); while ($row4 = mysql_fetch_array($result)) { // line 258 html.... } PHP is throwing the following WARNING and ít's not letting the data to query and display Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in public_html/example2.php on line 258 Help... Link to comment https://forums.phpfreaks.com/topic/191418-i-need-help-comparing-two-queries/#findComment-1009180 Share on other sites More sharing options...
co.ador Posted February 9, 2010 Author Share Posted February 9, 2010 any suggesstion on reply #9? Thanks Link to comment https://forums.phpfreaks.com/topic/191418-i-need-help-comparing-two-queries/#findComment-1009208 Share on other sites More sharing options...
jl5501 Posted February 9, 2010 Share Posted February 9, 2010 yes you have p.colon in there where clause. Should that be p.colon_id? Link to comment https://forums.phpfreaks.com/topic/191418-i-need-help-comparing-two-queries/#findComment-1009296 Share on other sites More sharing options...
co.ador Posted February 9, 2010 Author Share Posted February 9, 2010 I went to phpmyadmin and the query had a field that didn't exist. Link to comment https://forums.phpfreaks.com/topic/191418-i-need-help-comparing-two-queries/#findComment-1009644 Share on other sites More sharing options...
jl5501 Posted February 9, 2010 Share Posted February 9, 2010 exactly Link to comment https://forums.phpfreaks.com/topic/191418-i-need-help-comparing-two-queries/#findComment-1009674 Share on other sites More sharing options...
co.ador Posted February 9, 2010 Author Share Posted February 9, 2010 Now it is displaying three row of the same product, Plus prices and varieties of one product is displaying it in different rows as well. What I am trying to aim is the price 20.30 to display in small tray of the first row, then 25.90 display in the medium tray of the first row, and 30.90 display on large tray of the first row. As of displaying as it is now. CREATE TABLE products ( id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, name VARCHAR(255) ); CREATE TABLE product_varieties ( product_id INT, variety VARCHAR(100), price DOUBLE, description TEXT, PRIMARY KEY (product_id, variety) ); INSERT INTO products (id, name) VALUES (1, 'Cotél de camarone'); INSERT INTO product_varieties (product_id, variety, price) VALUES (1, 'Small Tray', 2.9, ); INSERT INTO product_varieties (product_id, variety, price) VALUES (1, 'Medium Tray', 6.9,); INSERT INTO product_varieties (product_id, variety, price) VALUES (1, 'Large Tray', 8.9, ); This is the link http://www.nyhungry.com/example2.php?subject=4&id=2®ister=1&menu=38 What's your take on this. Link to comment https://forums.phpfreaks.com/topic/191418-i-need-help-comparing-two-queries/#findComment-1009696 Share on other sites More sharing options...
co.ador Posted February 10, 2010 Author Share Posted February 10, 2010 Thank you guys I got it. Link to comment https://forums.phpfreaks.com/topic/191418-i-need-help-comparing-two-queries/#findComment-1009868 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.