Jump to content

help with variables and query...


co.ador

Recommended Posts

<?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

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.