amwd07 Posted April 5, 2008 Share Posted April 5, 2008 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 More sharing options...
zenag Posted April 5, 2008 Share Posted April 5, 2008 CLOSE THE FIRST WHILE LOOP AT LAST OF QUERY... Link to comment https://forums.phpfreaks.com/topic/99673-help-with-very-simple-query/#findComment-509903 Share on other sites More sharing options...
zenag Posted April 5, 2008 Share Posted April 5, 2008 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 />"; } } ?> Link to comment https://forums.phpfreaks.com/topic/99673-help-with-very-simple-query/#findComment-509904 Share on other sites More sharing options...
amwd07 Posted April 5, 2008 Author Share Posted April 5, 2008 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>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/99673-help-with-very-simple-query/#findComment-509908 Share on other sites More sharing options...
amwd07 Posted April 5, 2008 Author Share Posted April 5, 2008 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)); ?> Link to comment https://forums.phpfreaks.com/topic/99673-help-with-very-simple-query/#findComment-509925 Share on other sites More sharing options...
quiettech Posted April 5, 2008 Share Posted April 5, 2008 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. Link to comment https://forums.phpfreaks.com/topic/99673-help-with-very-simple-query/#findComment-509932 Share on other sites More sharing options...
kenrbnsn Posted April 5, 2008 Share Posted April 5, 2008 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 Link to comment https://forums.phpfreaks.com/topic/99673-help-with-very-simple-query/#findComment-509942 Share on other sites More sharing options...
amwd07 Posted April 5, 2008 Author Share Posted April 5, 2008 Maybe I am goign about this all wrong it is no showing all records in db, I only want to show new records which are not in the firast query. Should I use Join instead? Link to comment https://forums.phpfreaks.com/topic/99673-help-with-very-simple-query/#findComment-509945 Share on other sites More sharing options...
kenrbnsn Posted April 5, 2008 Share Posted April 5, 2008 Can you show us your database layout and sample data? Ken Link to comment https://forums.phpfreaks.com/topic/99673-help-with-very-simple-query/#findComment-509958 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.