Page 1 of 1

A question ?!!!

Posted: Wed Apr 13, 2011 10:51 am
by afshin2740
Hi
I'm new to PHP and I want to write a script to compare two products.
I have a table named mt_entry with 3 colomns named ID, Field_type and Field_content.

Image

As you see in the picture above, I have two products with two different IDs and different specifications. The code I have provided looks like this:

Code: Select all

<?php 
if ( isset ($_GET['submit']) )
{
	$phone1 = mysql_real_escape_string($_GET['phone1']);
	$phone2 = mysql_real_escape_string($_GET['phone2']);
    
	$query1 = mysql_query ("SELECT field_content FROM mt_entry WHERE `id`=$phone1") or die(mysql_error());
	$query2 = mysql_query ("SELECT field_content FROM mt_entry WHERE `id`=$phone2")or die(mysql_error());
	
    $row1 = mysql_fetch_assoc($query1);
	$row2 = mysql_fetch_assoc($query2);
}
?>


The missing part that I can't deal with is querying :

SELECT field_content FROM mt_entry WHERE `id`=$phone1 AND `Field_type`='model'
SELECT field_content FROM mt_entry WHERE `id`=$phone1 AND `Field_type`='brand'
SELECT field_content FROM mt_entry WHERE `id`=$phone1 AND `Field_type`='OS'
SELECT field_content FROM mt_entry WHERE `id`=$phone1 AND `Field_type`='status'

The function that I want from the code is that when the Field_type = model, show the related Field_content .
But having lot's of queries makes code awful. Is there any way to deal with this code ?!
I'm really new to PHP, so I would appreciate your help.
Thanks in advance!

Re: A question ?!!!

Posted: Wed Apr 13, 2011 12:06 pm
by afshin2740
Nobody wants to help ?!

Re: A question ?!!!

Posted: Wed Apr 13, 2011 12:12 pm
by danwguy
Your original post should select everything in the database associated with that phone, you just need to add a while loop and echo the content. I don't quite understand why you would need multiple SELECT statement on the db, you have it right so far, aside from ...

Code: Select all

$query1 = mysql_query ("SELECT * FROM mt_entry WHERE `id`=$phone1") or die(mysql_error());
        $query2 = mysql_query ("SELECT * FROM mt_entry WHERE `id`=$phone2")or die(mysql_error());
change to...

Code: Select all

$query1 = mysql_query ("SELECT * FROM mt_entry WHERE id='$phone1'") or die(mysql_error());
        $query2 = mysql_query ("SELECT * FROM mt_entry WHERE id='$phone2'")or die(mysql_error());
and then add a...

Code: Select all

while($row = mysql_fetch_assoc($query1) {
echo "model: " .$row['field_type'];
echo "<br /> " .$row['field_content'];
or something along those lines, but you have the select right.

Re: A question ?!!!

Posted: Wed Apr 13, 2011 12:25 pm
by afshin2740
I think I couldn't make it clear enough.
The query I want is this :

When ID = 1 , and Field_type = model, then show Field_content = E7

When ID = 1 , and Field_type = brand, then show Field_content = NOKIA


When ID = 2 , and Field_type = model, then show Field_content = Xperia

When ID = 2 , and Field_type = brand, then show Field_content = Sony Ericsson

and so on ...