Announcement

Collapse
No announcement yet.

More efficiency?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • More efficiency?

    Is there any way to make the mysql_fetch_array() function more efficient? If you have 20 or 30 values in a table and you want to pull each one out and assign it to a variable with the same name you have to type each one out and it takes forever.....
    Is there any other way?!
    Thanks,

  • #2
    Here is a solution a buddy came up with:

    <?
    $dbname = "";
    $dbhost = "";
    $dbuser = "";
    $dbpass = "";

    $db = mysql_connect($dbhost, $dbuser, $dbpass);
    mysql_select_db($dbname, $db);
    $fields = mysql_list_fields($dbname, "listings", $db);

    for ($i = 0; $i < mysql_num_fields($fields); $i++) {
    $fieldnames[] = mysql_field_name($fields, $i);
    }


    $result = mysql_query("SELECT * from listings LIMIT 10");
    $i = 0;
    while ($row = mysql_fetch_array($result)) {
    while(list($key,$val) = each ($fieldnames)) {
    $newarray[$i][$val] = $row[$val];
    }
    reset($fieldnames);
    $i++;
    }



    ?>

    And to access you would just loop through the array and call the fieldnames by their fieldname


    <?
    for ($i = 0; $i < count($newarray); $i++) {
    print $newarray[$i]["Agency"]."<br>";
    }
    ?>

    Comment

    widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
    Working...
    X