Author Topic: I need help comparing two queries.  (Read 787 times)

0 Members and 1 Guest are viewing this topic.

Offline co.adorTopic starter

  • Devotee
  • Posts: 724
  • Gender: Male
    • View Profile
I need help comparing two queries.
« on: February 08, 2010, 05:18:15 PM »
Code: [Select]
<?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_idvarietypricedescription,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...
« Last Edit: February 08, 2010, 05:21:06 PM by co.ador »
:) Help others and you will be helped,

I have learn through help, that Debugging and code ordering are extremely important tools to help my programming.

Offline jl5501

  • Devotee
  • Posts: 728
  • Gender: Male
  • PHP Freelancer
    • View Profile
Re: I need help comparing two queries.
« Reply #1 on: February 08, 2010, 05:24:35 PM »
You only need 1 query

Code: [Select]
<?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);
?>
Secure forms without Captcha

Offline jl5501

  • Devotee
  • Posts: 728
  • Gender: Male
  • PHP Freelancer
    • View Profile
Re: I need help comparing two queries.
« Reply #2 on: February 08, 2010, 05:25:41 PM »
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
Secure forms without Captcha

Offline co.adorTopic starter

  • Devotee
  • Posts: 724
  • Gender: Male
    • View Profile
Re: I need help comparing two queries.
« Reply #3 on: February 08, 2010, 05:26:44 PM »
ok the SELECT thank you..

That would be call a join of table?

:) Help others and you will be helped,

I have learn through help, that Debugging and code ordering are extremely important tools to help my programming.

Offline jl5501

  • Devotee
  • Posts: 728
  • Gender: Male
  • PHP Freelancer
    • View Profile
Re: I need help comparing two queries.
« Reply #4 on: February 08, 2010, 05:30:46 PM »
That is an implicit join yes

I am also using table aliases to simplify the typing
Secure forms without Captcha

Offline co.adorTopic starter

  • Devotee
  • Posts: 724
  • Gender: Male
    • View Profile
Re: I need help comparing two queries.
« Reply #5 on: February 08, 2010, 05:38:24 PM »
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);
?>

:) Help others and you will be helped,

I have learn through help, that Debugging and code ordering are extremely important tools to help my programming.

Offline jl5501

  • Devotee
  • Posts: 728
  • Gender: Male
  • PHP Freelancer
    • View Profile
Re: I need help comparing two queries.
« Reply #6 on: February 08, 2010, 05:42:04 PM »
you forgot to go back into quotes, but essentially correct

Code: [Select]
$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";
Secure forms without Captcha

Offline jl5501

  • Devotee
  • Posts: 728
  • Gender: Male
  • PHP Freelancer
    • View Profile
Re: I need help comparing two queries.
« Reply #7 on: February 08, 2010, 05:43:07 PM »
with a space inside the quotes before the and
Secure forms without Captcha

Offline co.adorTopic starter

  • Devotee
  • Posts: 724
  • Gender: Male
    • View Profile
Re: I need help comparing two queries.
« Reply #8 on: February 08, 2010, 05:44:59 PM »
Code: [Select]
$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.

:) Help others and you will be helped,

I have learn through help, that Debugging and code ordering are extremely important tools to help my programming.

Offline co.adorTopic starter

  • Devotee
  • Posts: 724
  • Gender: Male
    • View Profile
Re: I need help comparing two queries.
« Reply #9 on: February 08, 2010, 06:43:52 PM »



$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

Code: [Select]
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in
public_html/example2.php on line 258

Help...
:) Help others and you will be helped,

I have learn through help, that Debugging and code ordering are extremely important tools to help my programming.

Offline co.adorTopic starter

  • Devotee
  • Posts: 724
  • Gender: Male
    • View Profile
Re: I need help comparing two queries.
« Reply #10 on: February 08, 2010, 08:26:17 PM »
any suggesstion on reply #9?

Thanks
:) Help others and you will be helped,

I have learn through help, that Debugging and code ordering are extremely important tools to help my programming.

Offline jl5501

  • Devotee
  • Posts: 728
  • Gender: Male
  • PHP Freelancer
    • View Profile
Re: I need help comparing two queries.
« Reply #11 on: February 09, 2010, 03:05:53 AM »
yes

you have p.colon  in there where clause. Should that be p.colon_id?
Secure forms without Captcha

Offline co.adorTopic starter

  • Devotee
  • Posts: 724
  • Gender: Male
    • View Profile
Re: I need help comparing two queries.
« Reply #12 on: February 09, 2010, 03:30:40 PM »
I went to phpmyadmin and the query had a field that didn't exist.
:) Help others and you will be helped,

I have learn through help, that Debugging and code ordering are extremely important tools to help my programming.

Offline jl5501

  • Devotee
  • Posts: 728
  • Gender: Male
  • PHP Freelancer
    • View Profile
Re: I need help comparing two queries.
« Reply #13 on: February 09, 2010, 03:59:09 PM »
exactly
Secure forms without Captcha

Offline co.adorTopic starter

  • Devotee
  • Posts: 724
  • Gender: Male
    • View Profile
Re: I need help comparing two queries.
« Reply #14 on: February 09, 2010, 04:18:22 PM »
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.

Code: [Select]
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&register=1&menu=38

What's your take on this.
« Last Edit: February 09, 2010, 04:19:08 PM by co.ador »
:) Help others and you will be helped,

I have learn through help, that Debugging and code ordering are extremely important tools to help my programming.

Offline co.adorTopic starter

  • Devotee
  • Posts: 724
  • Gender: Male
    • View Profile
Re: I need help comparing two queries.
« Reply #15 on: February 09, 2010, 10:34:58 PM »
Thank you guys I got it.
:) Help others and you will be helped,

I have learn through help, that Debugging and code ordering are extremely important tools to help my programming.