co.ador Posted November 22, 2009 Share Posted November 22, 2009 <?php $restaurantname = $_GET['name']; $zipcode = $_GET['zipcode']; $state = $_GET['state']; $foodtype = $_GET['foodtype']; $checkboxes = $_GET['example']; ?> <?php $query3= "SELECT r.restaurantname, r.image, f.name FROM restaurants r INNER JOIN foodtypes f ON r.restaurants_id = f.restaurants_id WHERE f.name = " . $_GET['foodtype']; ?> I having a warning Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\stores\restaurants.php on line 50 Help please Quote Link to comment https://forums.phpfreaks.com/topic/182501-help-with-variables-and-query/ Share on other sites More sharing options...
KingIsulgard Posted November 22, 2009 Share Posted November 22, 2009 I guess that the foodtype you specify is a string so it needs quotes <?php $query3= "SELECT r.restaurantname, r.image, f.name FROM restaurants r INNER JOIN foodtypes f ON r.restaurants_id = f.restaurants_id WHERE f.name = '" . $_GET['foodtype'] . "'"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/182501-help-with-variables-and-query/#findComment-963233 Share on other sites More sharing options...
co.ador Posted November 22, 2009 Author Share Posted November 22, 2009 you are right it is an string but still the problem remains. I have it like this $query3= "SELECT r.restaurantname, r.image, f.name FROM restaurants r INNER JOIN foodtypes f ON r.restaurants_id = f.restaurants_id WHERE f.name = '" . $_GET['foodtype'] . "'"; I have three tables to implement the The Mapping Table Method of many-to-many relationship. 1. restaurants table or r.restaurants CREATE TABLE IF NOT EXISTS `restaurants` ( `restaurants_id` int(1) unsigned NOT NULL AUTO_INCREMENT, `restaurantname` varchar(255) NOT NULL, `image` varchar(100) NOT NULL, PRIMARY KEY (`restaurants_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=173 ; Then the footype tables or r.footype CREATE TABLE IF NOT EXISTS `foodtypes` ( `id` int(1) NOT NULL, `name` varchar(37) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; then the third one the table that links both tables above restaurants_footypes.. CREATE TABLE IF NOT EXISTS `restaurants_foodtypes` ( `restaurants_id` int(1) NOT NULL, `foodtypes_id` int(1) NOT NULL, PRIMARY KEY (`restaurants_id`,`foodtypes_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; The query above were the one we have checked I have changed as below. $query3= "SELECT r.restaurantname, r.image, f.name FROM restaurants r INNER JOIN foodtypes f ON r.restaurants_id = f.id WHERE f.name = '" . $_GET['foodtype'] . "'"; If you notice the differences is around the ON r.restaurants_id = f.id. When I change it to that then the warning doesn't display but the r.restaurantname and r.image field doesn't display either. it just a blank screen. I was wondering what is wrong plus I notice that the link table, the last one listed above restaurants_foodtypes that contain the fields of restaurants_id and footype_id is not used at the query? I was wondering what's the role of this table and most importantly how it can be used, applied or implemented to the goal I am aiming for which is: To have a query where the r.restaurantname and r.image field in the restaurants with an x foodtype display on restaurant.php based on the users foodtype choices ? Quote Link to comment https://forums.phpfreaks.com/topic/182501-help-with-variables-and-query/#findComment-963239 Share on other sites More sharing options...
taquitosensei Posted November 22, 2009 Share Posted November 22, 2009 try escaping your foodtype WHERE f.name='".mysql_real_escape_string)$_GET['foodtype']."'"; Quote Link to comment https://forums.phpfreaks.com/topic/182501-help-with-variables-and-query/#findComment-963243 Share on other sites More sharing options...
co.ador Posted November 22, 2009 Author Share Posted November 22, 2009 Thank you tanquito i have another query structure now and i need some help. the query seems to be well contructed but it won't display the div container4 and wrap in the screen. Right now as it is it doesn't display any warnings, errors etc. It won't display the results <div id="container4"> <div class="wrap"> <?php"SELECT r.* FROM RESTAURANTS r INNER JOIN RESTAURANTS_TO_RESTAURANT_FOOD_TYPES r2rft ON r.restaurants_id = r2rft.restaurants_id WHERE r2rft.restaurant_food_types_id IN (6,4) GROUP BY r.restaurants_id HAVING COUNT(DISTINCT r2rft.restaurant_food_types_id) = 2 "; $result3 = mysql_query($query3, $connection); $i = 1; while ($content3 = mysql_fetch_array($result3)) { echo "<div class=\"shoeinfo1\"> <img src=\"images/spacer.gif\" alt=\"spacer\" class=\"spacer2\" /> <h2 class=\"infohead\">". $content3['restaurantname'] . "</h2> <div class=\"pic\"><img class=\"line\" src= ". $content3['image'] ." alt=\"picture\" width=\"100%\" height=\"100%\" /></div> </div>"; $i++; } if ($i > 1 && $i % 3 == 0 ) { echo "<div class=\"clearer\"></div>"; }?></div> i have to say I am coming from page1.php to restaurant.php page and I am passing values throught the url to restaurant.php and I recieve the following values in restaurants such as.. <?php $restaurantname = $_GET['name']; $zipcode = $_GET['zipcode']; $state = $_GET['state']; $foodtype = $_GET['foodtype']; $checkboxes = $_GET['example']; ?> The file together would look like <?php $restaurantname = $_GET['name']; $zipcode = $_GET['zipcode']; $state = $_GET['state']; $foodtype = $_GET['foodtype']; $checkboxes = $_GET['example']; ?> <div id="container4"> <div class="wrap"> <?php "SELECT r.* FROM RESTAURANTS r INNER JOIN RESTAURANTS_TO_RESTAURANT_FOOD_TYPES r2rft ON r.restaurants_id = r2rft.restaurants_id WHERE r2rft.restaurant_food_types_id IN (6,4) GROUP BY r.restaurants_id HAVING COUNT(DISTINCT r2rft.restaurant_food_types_id) = 2 "; $result3 = mysql_query($query3, $connection); $i = 1; while ($content3 = mysql_fetch_array($result3)) { echo "<div class=\"shoeinfo1\"> <img src=\"images/spacer.gif\" alt=\"spacer\" class=\"spacer2\" /> <h2 class=\"infohead\">". $content3['restaurantname'] . "</h2> <div class=\"pic\"><img class=\"line\" src= ". $content3['image'] ." alt=\"picture\" width=\"100%\" height=\"100%\" /></div> </div>"; $i++; } if ($i > 1 && $i % 3 == 0 ) { echo "<div class=\"clearer\"></div>"; }?></div> If you notice i have added the image field in the restaurant table while thinking about building a table along for the images later. Now with the ideas of passing variables through the url instead of structuring. [HIGHLIGHT=SQL]HAVING COUNT(DISTINCT r2rft.restaurant_food_types_id) = 2[/HIGHLIGHT] should it include the variable as a comparison instead of the number like: [HIGHLIGHT=SQL]HAVING COUNT(DISTINCT r2rft.restaurant_food_types_id) = $foodtype[/HIGHLIGHT] The main issue is displaying the content assigned above. It doesn't display any erros messages or warning or anything like that just, the background of the body but not the content which would be the name of the restaurant and it's respective image. Quote Link to comment https://forums.phpfreaks.com/topic/182501-help-with-variables-and-query/#findComment-963317 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.