473,395 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Retrieving (Data + Fieldnames) from database to array?

Hi,

I'd like to poll a database and get the table contents, as well as the field
names from that table. I've been to php.net but this time I cannot find
something helpful.

I can get the data, and retrieve it into an array noproblem. [1]

I can get the data+fieldnames and put it into a list. [2]

But I cannot seem to be able to fill an array with the data *and* the
fieldnames. If you check both chunks of code, you will se what I'm doing.

Any hint? Any URL? Thank you in advance.

---------------------------------------------------
[1]
// Build SQL query
$query = "SELECT * FROM $table ORDER BY $sort_by";
$result = mysql_query($query) or do_error('list_AETitles()::mysql_query()',
'mysql query failed', mysql_error());
// Make sure there's something to show

// Call a function to build the table headers
insert_table_headers();

// Show results
while($row = mysql_fetch_array($result))
{
print "<TR ><TD ><A HREF=\"edit.php?id=$row[id]\">#$row[id]</a></TD>";
$high > $row[id] ? $high = $high : $high = $row[id];
for ($i=1; $i <= $columns; $i++)
{
print "<TD >$row[$i]</TD>";
}
print "</TR>\n";
}
print '</TABLE><BR>';
---------------------------------------------------
[2]
// Build SQL query
$query = "SELECT * FROM $table ORDER BY $sort_by";
$result = mysql_query($query) or do_error('list_AETitles()::mysql_query()',
'mysql query failed', mysql_error());
// Make sure there's something to show
$row = mysql_fetch_array($result, MYSQL_ASSOC);

// Show results
print '<table>';
while ($ary = mysql_fetch_assoc($result))
{
print '<tr>';
// $key is the column name. $val is the value
while (list($key,$val) = each($ary))
{
print '<TD class="'.$tblcolor.'">' .$val.'</TD>';
}
print '</tr>';
}
print '</table><br>';

--
Josep
Jul 16 '05 #1
3 11316

"Josep" <jo***@easternrad.com> wrote in message
news:0P******************@newsread1.prod.itd.earth link.net...
Hi,

I'd like to poll a database and get the table contents, as well as the field names from that table. I've been to php.net but this time I cannot find
something helpful.

I can get the data, and retrieve it into an array noproblem. [1]

I can get the data+fieldnames and put it into a list. [2]

But I cannot seem to be able to fill an array with the data *and* the
fieldnames. If you check both chunks of code, you will se what I'm doing.

Any hint? Any URL? Thank you in advance.

---------------------------------------------------
[1]
// Build SQL query
$query = "SELECT * FROM $table ORDER BY $sort_by";
$result = mysql_query($query) or do_error('list_AETitles()::mysql_query()', 'mysql query failed', mysql_error());
// Make sure there's something to show

// Call a function to build the table headers
insert_table_headers();

// Show results
while($row = mysql_fetch_array($result))
{
print "<TR ><TD ><A HREF=\"edit.php?id=$row[id]\">#$row[id]</a></TD>";
$high > $row[id] ? $high = $high : $high = $row[id];
for ($i=1; $i <= $columns; $i++)
{
print "<TD >$row[$i]</TD>";
}
print "</TR>\n";
}
print '</TABLE><BR>';
---------------------------------------------------
[2]
// Build SQL query
$query = "SELECT * FROM $table ORDER BY $sort_by";
$result = mysql_query($query) or do_error('list_AETitles()::mysql_query()', 'mysql query failed', mysql_error());
// Make sure there's something to show
$row = mysql_fetch_array($result, MYSQL_ASSOC);

// Show results
print '<table>';
while ($ary = mysql_fetch_assoc($result))
{
print '<tr>';
// $key is the column name. $val is the value
while (list($key,$val) = each($ary))
{
print '<TD class="'.$tblcolor.'">' .$val.'</TD>';
}
print '</tr>';
}
print '</table><br>';

--
Josep


I'm not sure where you're having a problem but does this help?
(ie use of "mysql_field_name")

PRINT "<table>\n";

if ($row = mysql_fetch_array($result)) {
$iflds = mysql_num_fields($result) - 1;
print " <TR>\n";
for ($i = 0; $i <= $iflds; $i++){
print " <TH>".mysql_field_name($result, $i)."</TH>\n";
}
print " </TR>\n";
do {
print " <TR>\n";
for ($i = 0; $i <= $iflds; $i++){
print " <TD>$row[$i]</TD>\n";
}
print " </TR>\n";
} while($row = mysql_fetch_array($result));
} else {print "<font color=red>Sorry, no records were
found</font>";}
PRINT "</table>\n";
}
Jul 16 '05 #2
2metre va escriure:
I'm not sure where you're having a problem but does this help?
(ie use of "mysql_field_name")


doh!

Thank you very much 2metre! It was under my nose but I had my eyes closed (or
almost).

--
Josep
Jul 16 '05 #3
Josep sikyal:
Hi,

I'd like to poll a database and get the table contents, as well as the field
names from that table. I've been to php.net but this time I cannot find
something helpful.
I know that this problem was already solved, but the solution seems
unnecessarily complex to me. In reality, I don't understand what your
problem is, since you've stated it rather oddly, and I think that you
already had your solution but didn't know it.

I can get the data, and retrieve it into an array noproblem. [1]

I can get the data+fieldnames and put it into a list. [2]

But I cannot seem to be able to fill an array with the data *and* the
fieldnames. If you check both chunks of code, you will se what I'm doing.
I don't understand how this problem is different from [2]. If you use
mysql_fetch_assoc(), you'll get an array of fieldname => data pairs--isn't
this exactly what you want?

[2]
// Build SQL query
$query = "SELECT * FROM $table ORDER BY $sort_by";
$result = mysql_query($query) or do_error('list_AETitles()::mysql_query()',
'mysql query failed', mysql_error());
// Make sure there's something to show
$row = mysql_fetch_array($result, MYSQL_ASSOC);

// Show results
print '<table>';
while ($ary = mysql_fetch_assoc($result))
{
print '<tr>';
// $key is the column name. $val is the value
while (list($key,$val) = each($ary))
{
print '<TD class="'.$tblcolor.'">' .$val.'</TD>';
}


It's more readable to say

foreach ($ary as $field => $val) {
// Print table element
}

Within that loop you have $field as the name of the current column--what
else do you need?

If you want to print a header row with the names of fields, you can do
something like this at the beginning of your table:

print "<tr>"
while ($field = mysql_fetch_field($result)) {
print "<th>$field->name</th>";
}
print "</tr>";

This is somewhat similar to what the other poster wrote.
Jesse S. Bangs ja****@u.washington.edu

Jul 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: 'bonehead | last post by:
Greetings, I'd like to figure out some syntax for retrieving the data from a table when I don't know all the of field names. What I do know are, the name of the table, the names of the primary...
4
by: Little PussyCat | last post by:
Hello, I have had a request, one of our tables is used as a report and I have been asked that all fieldnames for months have dashes in them, like Jan-05 instead of Jan05 and so on... Now what...
6
by: Dean Slindee | last post by:
Does anybody have an actual example of retrieving an Image data type column from a SQL Server table using a dataset (not a datareader)? I would like to see the statements that would move the...
1
by: meltedown | last post by:
This is a function that returns the names a table field on a pgsql database $skips is simply the field names that will be left out of the result. This function acts differently on two different...
0
by: Andy | last post by:
Hi All. I'm working for a company that has set out a guideline for retrieving data from a database. Nobody can explain to me the reason for the following. When retrieving a set of records...
11
by: Nemisis | last post by:
Hi everyone, sorry if this post gets really long, i just wanna make sure i fully explain what i am trying to do. I am new to OOP and .net 2.0, so if this is obvious, i am sorry. I have wrote a...
9
ADezii
by: ADezii | last post by:
One question which pops up frequently here at TheScripts is: 'How do I retrieve data from a Recordset once I've created it?' One very efficient, and not that often used approach, is the GetRows()...
3
ADezii
by: ADezii | last post by:
Last Tip, we demonstrated the technique for retrieving data from a DAO Recordset, and placing it into a 2-dimensional Array using the GetRows() Method. This week, we will cover the same exact Method...
1
by: inlovewithmusic | last post by:
Hi , I am trying to write a piece of funtionality that will store a string array into into a database and then later retrieve it for viewing later. I wrote the funtionality to insert the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.