Jump to content

Help with very simple query


amwd07

Recommended Posts

This should be very easy.

I can't think why the second query is ignoring the first

all I am trying to do is show only new products which are not matching products model in $result?

 

<?php
// Make a MySQL Connection
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

// Get a specific result from products table
$result = mysql_query("SELECT * FROM products") or die(mysql_error());  
$products = mysql_fetch_array( $result );
while($products = mysql_fetch_array($result)){
$productmodel = $products['products_model'];
}
// Get a specific result from Catalogue table
$result2 = mysql_query("SELECT * FROM CSV_Catalogue
WHERE Model != $productmodel") or die(mysql_error());  
$catalogue = mysql_fetch_array( $result2 );
$num_rows = mysql_num_rows($result2);

echo $num_rows;
echo "<br />";
while($catalogue = mysql_fetch_array($result2)){
$catmodel = $catalogue['Model']; 
$catname = $catalogue['Name'];
echo $catmodel." - ".$catname;
echo "<br />";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/99673-help-with-very-simple-query/
Share on other sites

TRY THIS...

 

<?php

// Make a MySQL Connection

mysql_connect("localhost", "username", "password") or die(mysql_error());

mysql_select_db("database") or die(mysql_error());

 

// Get a specific result from products table

$result = mysql_query("SELECT * FROM products") or die(mysql_error()); 

$products = mysql_fetch_array( $result );

while($products = mysql_fetch_array($result)){

$productmodel = $products['products_model'];

 

// Get a specific result from Catalogue table

$result2 = mysql_query("SELECT * FROM CSV_Catalogue

WHERE Model != $productmodel") or die(mysql_error()); 

$catalogue = mysql_fetch_array( $result2 );

$num_rows = mysql_num_rows($result2);

 

echo $num_rows;

echo "<br />";

while($catalogue = mysql_fetch_array($result2)){

$catmodel = $catalogue['Model'];

$catname = $catalogue['Name'];

echo $catmodel." - ".$catname;

echo "<br />";

}

}

?>

 

this still ignores $productmodel & shows up all results ?

 

// Get a specific result from products table
$result = mysql_query("SELECT * FROM products") or die(mysql_error());  
$products = mysql_fetch_array( $result );
while($products = mysql_fetch_array($result)){
$productmodel = $products['products_model'];

// Get a specific result from Catalogue table
$result2 = mysql_query("SELECT * FROM CSV_Catalogue
WHERE Model != $productmodel") or die(mysql_error());  
$catalogue = mysql_fetch_array( $result2 );
$num_rows = mysql_num_rows($result2);

echo $num_rows;
echo "</br>";
while($catalogue = mysql_fetch_array($result2)){
$catmodel = $catalogue['Model']; 
$catname = $catalogue['Name'];
echo $catmodel." - ".$catname;
echo "</br>";
}
}
?>

I have tried another approch and still doesn't work ???

 

<?php 
// Get a specific result from products table
$result = mysql_query("SELECT * FROM products") or die(mysql_error()); 
$products = mysql_fetch_assoc($result);
do {
$productmodel = $products['products_model'];
}
while($products = mysql_fetch_assoc($result));

// Get a specific result from Catalogue table
$result2 = mysql_query("SELECT * FROM CSV_Catalogue
WHERE Model != $productmodel") or die(mysql_error());
$catalogue = mysql_fetch_assoc($result2);
$num_rows = mysql_num_rows($result2);


echo $num_rows;
echo "<p />";
do {
$catmodel = $catalogue['Model']; 
$catname = $catalogue['Name'];
echo $catmodel." - ".$catname;
echo "</br>";
} 
while($catalogue = mysql_fetch_assoc($result2));
?>

Your indentation - or lack of it - is confusing me, so I'm not going to read much into your code. However it seems to me you are trying to read only one position of the fetched array, which will show all but that one.

 

$productmodel is storing only one element of the array.

You were using mysql_fetch_array() too many times. Try this:

<?php
// Get a specific result from products table
$q = "Select * from products";
$result = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());  
while($products = mysql_fetch_assoc($result)){
$productmodel = $products['products_model'];

// Get a specific result from Catalogue table
$q2 = "SELECT * FROM CSV_Catalogue WHERE Model != $productmodel";
$result2 = mysql_query($q2) or die("Problem with the query: $q2<br>" . mysql_error());  
$num_rows = mysql_num_rows($result2);
echo $num_rows . '<br>';
while($catalogue = mysql_fetch_assoc($result2)){
	echo $catalogue['Model']." - ".$catalogue['Name'] . '<br>';
}
}
?>

 

I fixed the indentation -- see how much easier it is to see the loops?

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.