Orionsbelter Posted May 17, 2011 Share Posted May 17, 2011 Hi there i have a big issues which is starting to cause me lots of stress. I have two tables one which is products and contains the following below and the other which is additionalCategories which the contains is also below. products id name image1 image2 image3 image4 image5 description department category subcategory Price additonalCategories id departmentID categoryID subcategoryID productID i have made a products upload script to my site where i can add products too it saves the category and subcategory id's in the fields above then if i wish to add the product to additional categories i do so and again this adds all the data to the addiotnalCategories table. however i can seems to be able to display this on the view products page i tried the below code but get no luck <?php session_start(); $username=$_SESSION['username']; include_once"../includes/db_connect.php"; $cat=$_GET['cat']; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php $query("SELECT products.name AS name, products.price AS price, addtionalCategories.productID AS productID FROM products, addtionalCategories WHERE category='$cat' AND products.id = addtionalCategories.productID"); while($fetch=mysql_fetch_object($query)){ echo"$fetch->name"; } ?> </body> </html> i need a way in which on the page it check the category id then finds all the products that are saved in the additionalCategories table then to find the data saved in the products table such as the name and price etc please any help will be appreciated thank you for reading Quote Link to comment https://forums.phpfreaks.com/topic/236675-getting-product-list-from-two-tables-in-the-database/ Share on other sites More sharing options...
jkewlo Posted May 17, 2011 Share Posted May 17, 2011 have you tried any joins?? http://dev.mysql.com/doc/refman/5.0/en/join.html Quote Link to comment https://forums.phpfreaks.com/topic/236675-getting-product-list-from-two-tables-in-the-database/#findComment-1216659 Share on other sites More sharing options...
Orionsbelter Posted May 18, 2011 Author Share Posted May 18, 2011 thats what am tring in my code but it doesnt work Quote Link to comment https://forums.phpfreaks.com/topic/236675-getting-product-list-from-two-tables-in-the-database/#findComment-1216785 Share on other sites More sharing options...
mikosiko Posted May 18, 2011 Share Posted May 18, 2011 part of your code: $query("SELECT products.name AS name, products.price AS price, addtionalCategories.productID AS productID FROM products, addtionalCategories WHERE category='$cat' AND products.id = addtionalCategories.productID"); while($fetch=mysql_fetch_object($query)){ read... and compare your code to the examples http://php.net/manual/en/function.mysql-query.php Quote Link to comment https://forums.phpfreaks.com/topic/236675-getting-product-list-from-two-tables-in-the-database/#findComment-1216801 Share on other sites More sharing options...
Orionsbelter Posted May 18, 2011 Author Share Posted May 18, 2011 silly newbie mistake <?php $query=mysql_query("SELECT products.name AS name, products.price AS price, addtionalCategories.productID AS productID FROM products, addtionalCategories WHERE category='1' AND products.id = addtionalCategories.productID"); while($fetch=mysql_fetch_object($query)){ echo"$fetch->name"; } ?> however it still echos only the following id departmentID categoryID subcategoryID productID Quote Link to comment https://forums.phpfreaks.com/topic/236675-getting-product-list-from-two-tables-in-the-database/#findComment-1217120 Share on other sites More sharing options...
Orionsbelter Posted May 18, 2011 Author Share Posted May 18, 2011 ? Quote Link to comment https://forums.phpfreaks.com/topic/236675-getting-product-list-from-two-tables-in-the-database/#findComment-1217146 Share on other sites More sharing options...
Orionsbelter Posted May 18, 2011 Author Share Posted May 18, 2011 anymore help on this? Quote Link to comment https://forums.phpfreaks.com/topic/236675-getting-product-list-from-two-tables-in-the-database/#findComment-1217213 Share on other sites More sharing options...
mikosiko Posted May 18, 2011 Share Posted May 18, 2011 if you post your complete code and explain clearly your objectives sure someone will chime. Quote Link to comment https://forums.phpfreaks.com/topic/236675-getting-product-list-from-two-tables-in-the-database/#findComment-1217214 Share on other sites More sharing options...
Orionsbelter Posted May 18, 2011 Author Share Posted May 18, 2011 my explanation is above so is my full code, apart from the mysql_query issue is now fixed in the code. Quote Link to comment https://forums.phpfreaks.com/topic/236675-getting-product-list-from-two-tables-in-the-database/#findComment-1217217 Share on other sites More sharing options...
mikosiko Posted May 18, 2011 Share Posted May 18, 2011 <body> <?php $query("SELECT products.name AS name, products.price AS price, addtionalCategories.productID AS productID FROM products, addtionalCategories WHERE category='$cat' AND products.id = addtionalCategories.productID"); while($fetch=mysql_fetch_object($query)){ echo"$fetch->name"; } ?> this is your full code? .... and what else have you tried?... what results are you expecting? according to one of your posts: however it still echos only the following id departmentID categoryID subcategoryID productID I don't see anything like that in your code... only one echo"$fetch->name" ?? Quote Link to comment https://forums.phpfreaks.com/topic/236675-getting-product-list-from-two-tables-in-the-database/#findComment-1217222 Share on other sites More sharing options...
Orionsbelter Posted May 18, 2011 Author Share Posted May 18, 2011 my full code is <?php session_start(); $username=$_SESSION['username']; include_once"../includes/db_connect.php"; $cat=$_GET['cat']; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php $query=mysql_query("SELECT products.name AS name, products.price AS price, addtionalCategories.productID AS productID FROM products, addtionalCategories WHERE products.category='1' AND products.id = addtionalCategories.productID"); while($fetch=mysql_fetch_object($query)){ echo"$fetch->name"; } ?> </body> </html> i'm trying to display all my products by name from my mysql database however i two tables one which is products which contains the master category in a field called category and another table which is called additionalCategories which contains a field called productID which is the same as a record in the products table i use the id field in the products table and inset into additonalCategories to allow myself to add products to more than one categories however when i try join the too so i can display all products in the category from the products table and all products from additionalCategories. however it isn't displaying anything. Quote Link to comment https://forums.phpfreaks.com/topic/236675-getting-product-list-from-two-tables-in-the-database/#findComment-1217229 Share on other sites More sharing options...
mikosiko Posted May 18, 2011 Share Posted May 18, 2011 I did post ONLY your relevant code ... the one that is supposed to "display" your data.... and I still seeing only ONE echo there... if that piece of (incomplete) code is not displaying anything means that your query is not returning anything or is failing... or you are not connected to the DB... try this simple changes (very dirty... just to help you to start) : $query = "SELECT products.name, products.price, addtionalCategories.productID FROM products, addtionalCategories WHERE products.category='1' AND products.id = addtionalCategories.productID"; $query = mysql_query($query) or die("error : " . mysql_error()); while( $fetch = mysql_fetch_object($query)){ echo "Product Name : " . $fetch->name; echo "<br />Product Price : " . $fetch->price; // etc... etc...etc... } see what you get from this Quote Link to comment https://forums.phpfreaks.com/topic/236675-getting-product-list-from-two-tables-in-the-database/#findComment-1217240 Share on other sites More sharing options...
Orionsbelter Posted May 19, 2011 Author Share Posted May 19, 2011 thanks for replying, however it still doesn't shows anything it isn't reporting any mysql_errors either Quote Link to comment https://forums.phpfreaks.com/topic/236675-getting-product-list-from-two-tables-in-the-database/#findComment-1217518 Share on other sites More sharing options...
mikosiko Posted May 19, 2011 Share Posted May 19, 2011 if you replace this line: $query = mysql_query($query) or die("error : " . mysql_error()); with this: $query = mysql_query($query) or die("error : " . mysql_error()); $num_rows = mysql_num_rows($query); echo "Rows Returned by the Query : " . $num_rows; what do you get? Quote Link to comment https://forums.phpfreaks.com/topic/236675-getting-product-list-from-two-tables-in-the-database/#findComment-1217557 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.