normally to get my items from mysql to an array i use this code

while($row=mysqli_fetch_array($result))
{
$array[] = array('id' => $row['id'] ect...)
}

anyway I am using some OOP in my code so now I don't know what the row names from mysql are named to use in the while loop so how do I get the row names and then put them in the while loop array.

Recommended Answers

All 7 Replies

I assume that with "row names" you mean the column names.
You can get them with the functions mysql_field_name and mysql_field_count:

while ($object = mysql_fetch_object($result) {
  for ($i = 0; $i < mysql_field_count($result); $i++) {
    $field_name = mysql_field_name($result, $i);
    $field = $object->$field_name;
    ...
  }
}
Member Avatar for diafol

Depends which library you're using. Some have their own custom methods and properties for mysql. You using PDO or something else?

so that would get all my field names and I could then use them? how then could I export that array into my code to use?

Member Avatar for diafol

You mention mysql but are using code for mysqli. Which is it?

mysqli stands for mysql improved its just a better way to connect to mysql with php that's all

Member Avatar for diafol

Quite right, Ignore a senile old codger. :) I got it mixed up with something else. ANyway, can you post your code here? Even if you've oop-ified it.

Member Avatar for diafol

Been racking my brains - Got it at last -SQLite was the darned thing I wthought you were using. Feeling stupid now. Mental block. Anyway, haven't used mysqli before, will have a look at it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.