SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    347
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Why am I getting apparently duplicated results ?

    I'm trying to extract info from one column of my database, from some rows only. I'm getting the correct information, but I'm getting it twice, in an array, as shown below:
    Code:
    $emailquery = "SELECT email FROM accommodations WHERE accn_id = 2073 OR accn_id = 2066 OR accn_id = 2075 OR accn_id = 2043 OR accn_id = 2003";
    ...[connect to database]...
    $emailresult = mysqli_query($link, $emailquery);
    	while ($row = mysqli_fetch_array($emailresult)) {
    		$emails[] = $row;
    		echo "<br />\n";
    		print_r ($row);
    	}
    This prints: Array ( [0] => fred@example.com [email] => fred@example.com)
    I expected the contents of $row to be an e-mail address ('fred@example.com'), instead it's an array as above and I get a similar array (different e-mail address, of course) for each of the (five) rows returned.

    I can extract what I want from $row[0] (or $row['email']), but why is the data returned in this form ?

    Some books I have lead me to think this may in fact be normal MySQL behaviour, but if so can someone tell me why it's done this way, please.
    Tim Dawson
    Isle of Mull, Scotland

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    it is not normal for mysql to return double results

    just run your query outside of php to confirm this
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,139
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    mysqli_fetch_array()
    PHP Manual
    resulttype:

    This optional parameter is a constant indicating what type of array should be produced from the current row data. The possible values for this parameter are the constants MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH. Defaults to MYSQLI_BOTH.
    By using the MYSQLI_ASSOC constant this function will behave identically to the mysqli_fetch_assoc(), while MYSQLI_NUM will behave identically to the mysqli_fetch_row() function. The final option MYSQLI_BOTH will create a single array with the attributes of both.
    PHP Code:
    $emailquery "SELECT email FROM accommodations WHERE accn_id = 2073 OR accn_id = 2066 OR accn_id = 2075 OR accn_id = 2043 OR accn_id = 2003";
    ...[
    connect to database]...
    $emailresult mysqli_query($link$emailquery);
        while (
    $row mysqli_fetch_array($emailresult,MYSQLI_ASSOC)) {
            
    $emails[] = $row;
            echo 
    "<br />\n";
            
    print_r ($row);
        } 

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,023
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    The query is SELECTing only the email field. It is PHP that is assigning the value to BOTH a numeric AND an associative key and print_r shows this. See oddz's reply.

  5. #5
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    347
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you all. You have explained it very clearly, and now I can understand why in books I sometimes see '$row[0]' being processed for further use, and sometimes '$row['email']' (making an example from my instance).
    Tim Dawson
    Isle of Mull, Scotland


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
  •