Help with a query slq syntax error

Discussion in 'MySQL' started by co.ador, Jul 26, 2009.

  1. #1
    I have a query error

    the query goes like this

     
    co.ador, Jul 26, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Most likely you need to put the $shoename in single quotes.

    $query ="SELECT rating , COUNT(*) AS rating_counts FROM rating
    WHERE item_name = '$shoename' GROUP BY rating";

    Also make sure this is giving you the results you want the COUNT(*) and GROUP BY don't always play nicely.
     
    jestep, Jul 26, 2009 IP
  3. co.ador

    co.ador Peon

    Messages:
    120
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for the commas

    now i recieving this php error


    Notice: Undefined index: in C:\wamp\www\shoes\stores\itemdetails2.php on line 465
    1 Star Active
    PHP:

    $query ="SELECT rating , COUNT(*) AS rating_counts FROM rating
     WHERE item_name = '$platename' GROUP BY rating";
    
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
    
    // see if any rows were returned 
    if (mysql_num_rows($result) > 0) { 
    $ratings = array_fill(1,5,0);
    $rowsCount = count($result); // assume $rows is the result array
    
    for($i=0;$i<$rowsCount;$i++) {
        $ratings[$result[$i]['rating']]++; // line 465
    
    PHP:
     
    Last edited: Jul 26, 2009
    co.ador, Jul 26, 2009 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    Which line is 465. Undefined index means that you're referencing or trying to use an array key (index) that isn't currently set.
     
    jestep, Jul 26, 2009 IP
  5. co.ador

    co.ador Peon

    Messages:
    120
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Jestep the script has evolve to this new while loop for the count of each stars rating an item.

    now instead of 465 is on 484 and I commented the line on the script so you will be able to see which one is line 484.

    Notice: Undefined variable: num_1_star in C:\wamp\www\shoes\stores\itemdetails2.php on line 484

    <?php 
    $sql="SELECT rating, COUNT(rating) FROM rating WHERE (item_name='$platename') GROUP BY rating";
    $result=mysql_query($sql);
    if (mysql_num_rows($result)  > 0) {
        while ($row= mysql_fetch_assoc($result)) {
            $star= $row['rating'];
            switch($star) {
                case '1':
                    $num_1_star= $row['COUNT(rating)'];
                    break 1;
                case '2':
                    $num_2_star= $row['COUNT(rating)'];
                    break 1;
                case '3':
                    $num_3_star= $row['COUNT(rating)'];
                    break 1;
                case '4':
                    $num_4_star= $row['COUNT(rating)'];
                    break 1;
                case '5':
                    $num_5_star= $row['COUNT(rating)'];
                    break 1;
    				}
    
    
    echo '<h3>1 Star Active</h3>
    <ul class="rating onestar">
    	<li class="one"><a href="#" title="1 Star">1</a></li>
    	<li class="two"><a href="#" title="2 Stars">2</a></li>
    	<li class="three"><a href="#" title="3 Stars">3</a></li>
    	<li class="four"><a href="#" title="4 Stars">4</a></li>
    	<li class="five"><a href="#" title="5 Stars">5</a></li>
    	<li class="total">['. $num_1_star['rating'].'</li>  // line 484
    </ul>
    <h3>2 Stars Active</h3>
    <ul class="rating twostar">
    	<li class="one"><a href="#" title="1 Star">1</a></li>
    	<li class="two"><a href="#" title="2 Stars">2</a></li>
    	<li class="three"><a href="#" title="3 Stars">3</a></li>
    	<li class="four"><a href="#" title="4 Stars">4</a></li>
    
    	<li class="five"><a href="#" title="5 Stars">5</a></li>
    	<li class="total">['. $row['rating'].'</li> // When I put it $row['rating'] then it prints the index number location which is 2, the rating field is in the index number 2
    </ul>
    <h3>3 Stars Active</h3>
    <ul class="rating threestar">
    	<li class="one"><a href="#" title="1 Star">1</a></li>
    	<li class="two"><a href="#" title="2 Stars">2</a></li>
    	<li class="three"><a href="#" title="3 Stars">3</a></li>
    
    	<li class="four"><a href="#" title="4 Stars">4</a></li>
    	<li class="five"><a href="#" title="5 Stars">5</a></li>
    	<li class="total">[60]</li>
    </ul>
    <h3>4 Stars Active</h3>
    <ul class="rating fourstar">
    	<li class="one"><a href="#" title="1 Star">1</a></li>
    	<li class="two"><a href="#" title="2 Stars">2</a></li>
    
    	<li class="three"><a href="#" title="3 Stars">3</a></li>
    	<li class="four"><a href="#" title="4 Stars">4</a></li>
    	<li class="five"><a href="#" title="5 Stars">5</a></li>
    	<li class="total">[80]</li>
    </ul>
    <h3>5 Stars Active</h3>
    <ul class="rating fivestar">
    	<li class="one"><a href="#" title="1 Star">1</a></li>
    
    	<li class="two"><a href="#" title="2 Stars">2</a></li>
    	<li class="three"><a href="#" title="3 Stars">3</a></li>
    	<li class="four"><a href="#" title="4 Stars">4</a></li>
    	<li class="five"><a href="#" title="5 Stars">5</a></li>
    	<li class="total">[100]</li>
    </ul>';
    }
    } 
    ?>
    PHP:
     
    co.ador, Jul 26, 2009 IP