mysql fetch assoc VS mysql fetch array

后端 未结 10 1857
太阳男子
太阳男子 2020-12-10 04:10

Below are two methods commonly used in most php codes for fetch mysql data .

  1. mysql_fetch_array()
  2. mysql_fetch_assoc()
  3. <
10条回答
  •  时光取名叫无心
    2020-12-10 04:53

    The "size" of the query results don't matter in this case.

    mysql_fetch_array() generally produces overhead by returning an associative array plus indexed results.

    One should decide on the type of query and the desired results on whether to use mysql_fetch_array() or mysql_fetch_assoc().

    If the overhead is "neglectable" and I'm sure the query succeeds and I know there's only a single result, I occasionally do:

    $q = mysql_query('SELECT `column1`,`column2` FROM `table` WHERE `id` = 123');
    list($column1,$column2) = mysql_fetch_array($q);
    mysql_free_result($q);
    

    For iterative proceedings (like a while loop), this just doesn't cut it. When there's no need for "quick-extracting" results (via a list operator) and the result has to be further processed in code, I always use mysql_fetch_assoc()*.


    * When forced to actually use the quite out-dated procedural data retrieval functions of PHP. There are alternatives.

提交回复
热议问题