PHP SQL, query returns only one row of data

前端 未结 3 796
后悔当初
后悔当初 2020-12-22 14:38

quick question... my sql query is spitting out this data...

Array ( [id] => 1 [name] => Test Name [text] => This is text [image] => image.jpg ) 
         


        
相关标签:
3条回答
  • 2020-12-22 15:19

    mysql_fetch_assoc fetches one row at a time. You need to loop over the result set:

    while(false !== ($row = mysql_fetch_assoc($results))){
        [handle $row here]
    }
    

    From the docs (search is your friend):

    Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows.

    If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you either need to access the result with numeric indices by using mysql_fetch_row() or add alias names. See the example at the mysql_fetch_array() description about aliases.

    Cheers

    0 讨论(0)
  • 2020-12-22 15:23

    mysql_fetch_assoc($results) does only fetches one row.

    http://php.net/manual/en/function.mysql-fetch-assoc.php

    0 讨论(0)
  • 2020-12-22 15:28

    The fetch functions only return a single row at a time. If you want all the rows, you'll have to use a loop:

    while($row = mysql_fetch_assoc($results)) {
       ... do stuff with $row
    }
    
    0 讨论(0)
提交回复
热议问题