SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array_unique function question?

    Hi Everyone,

    I want to display the vehicles every user is selling but for some reason I'm getting duplicate information. I think the answer to my question is the array_unique function but I'm having a hard time trying to figure out where the function should go? Any tips would be great, thanks!

    http://www.whatsmyowncarworth.com/fo...ql-command.php

    PHP Code:
    <?php
    include_once "init.php";

    echo 
    "<table border='1'>";
    echo 
    "<tr> <th>User ID</th> <th>First Name</th> <th>Last Name</th> <th>Car Desc ID</th> <th>Price</th>  <th>Mileage</th> <th>Transmission</th> <th>Color</th> <th>Year</th> <th>Make</th> <th>Model</th> </tr>";

    $query "SELECT users.user_id, users.firstname, users.lastname, prices.car_desc_id, prices.price, \n"
        
    "\n"
        
    "prices.mileage, prices.transmission, prices.color, car_desc.year, car_desc.make, car_desc.model\n"
        
    "\n"
        
    "FROM users, prices, car_desc\n"
        
    "\n"
        
    "WHERE prices.car_desc_id LIMIT 0, 30 ";    

    // $result = array_unique($query);    

    // $query = "SELECT * FROM car_desc ORDER BY id ASC";  
    $thedude mysql_query($query) or die(mysql_error());
    // $result = array_unique($thedude);
    while ($row mysql_fetch_array($thedude)) {

        
    $user_id = ($row['user_id']);
        
    $firstname = ($row['firstname']);
        
    $lastname = ($row['lastname']); 
        
    $car_desc_id =  ($row['car_desc_id']);
        
    $price = ($row['price']);
        
    $mileage = ($row['mileage']);
        
    $transmission = ($row['transmission']);
        
        
    $color = ($row['color']);
        
    $year = ($row['year']);
        
    $make = ($row['make']);
        
    $model = ($row['model']);


        
    // keeps getting the next row until there are no more to get
         /*while ($row = mysql_fetch_array($result))*/ 
    {
            
    // Print out the contents of each row into a table
    ?>

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    If your query is bringing back duplicate rows from your database then that is where you should be looking first in order to eliminate them.

    See if you can do this using the mysql DISTINCT keyword or something:

    Code mysql:
     
    $query = "SELECT users.user_id
    , users.firstname
    , users.lastname
    , prices.car_desc_id
    , prices.price
    , prices.mileage
    , prices.transmission
    , prices.color
    , car_desc.year
    , car_desc.make
    , car_desc.model
    FROM users, prices, car_desc 
    WHERE prices.car_desc_id LIMIT 0, 30 ";

    I reformatted your query so it is easier to read and understand for others.

    I think the problem lies in you doing joins but not doing so implicitly with JOIN statements.

    You could ask to have this moved to the sql forum, using PHP to remove the duplicates is not the way to go about this.

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Cups,

    Thanks for your help. I tried the DISTINCT keyword and nothing has changed.

    I would be interested in moving this question to the mysql forum. Who would I need to ask?

    Thanks everyone!

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,017
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    the duplicates are coming from your query

    look up cross join

    it's caused by this --
    Code:
    FROM users, prices, car_desc
    every user is matched with every price, and every one of those combinations is matched with every car_desc
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •