Results 1 to 4 of 4

Thread: [RESOLVED] Remove repeating values

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Resolved [RESOLVED] Remove repeating values

    Hi,

    I am trying to display data to the screen using the following code:

    PHP Code:
    <?php

    $db_host 
    "localhost";

    $db_name "students";

    $db_user "root";

    $db_pass "";

    $table "students";

     
    mysql_connect($db_host,$db_user,$db_pass);
    mysql_select_db($db_name) or die("Unable to select database.");

        
    $data mysql_query("SELECT * FROM $table");

    $info mysql_fetch_array($data);
    while(
    $info mysql_fetch_array$data )){

    $comma_separated implode(","$info);

    print 
    $comma_separated;
    }
    ?>
    It works! However, I get a double up of the values like so:

    2 2 Mike Mike 17 17 Male Male 05/06/1990 05/06/19903 3 Joann Joann 17 17 Female Female 26/06/1982 26/06/19824 4 Rachel Rachel 14 14 Female Female 23/07/1980 23/07/19805 5 Robert Robert 13 13 Male Male 23/06/1990 23/06/19906 6 Sarah Sarah 14 14 Female Female 21/4/1990 21/4/19907 7 evan evan e e e e e e8 8 fiona fiona f f f f f f
    I checked over my code to see what might be causing the problem but I can't find it.

    Thanks,


    Nightwalker
    Last edited by Nightwalker83; Dec 31st, 2009 at 08:52 PM. Reason: Fixed spelling
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Remove repeating values

    For one thing, take out:
    Code:
    $info = mysql_fetch_array($data);
    ...unless you want to skip printing the first row of results, because that's what this will cause in your code.

    As for the duplicates, this is because the array returned by mysql_fetch_array (by default) has each value in the row twice - once under a numeric index, and once under the column name. Try this and you can see what I mean:
    Code:
    while($info = mysql_fetch_array( $data )){
    print_r($info);
    }
    To change this, you can set a second argument in mysql_fetch_array:
    Code:
    while($info = mysql_fetch_array( $data,MYSQL_ASSOC )){
    
    OR
    
    while($info = mysql_fetch_array( $data,MYSQL_NUM )){
    The first option will return an array with only column names as array keys, and the second option will return an array with only numeric indexes for your columns.

  3. #3

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Remove repeating values

    Thanks, that solved the problem.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [RESOLVED] Remove repeating values

    a better way would be to just use the associative array function mysql_fetch_assoc() (though it does accomplish the same thing, it just does it in less typing and is probably easier to read).

    for future reference, you can alternatively use mysql_fetch_row() to return just the numeric array; there are very, very few circumstances where using the numeric indexes would actually be practical, however.

Posting Permissions

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



Click Here to Expand Forum to Full Width