printing fetched values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
drschwartz
Forum Newbie
Posts: 12
Joined: Fri Feb 27, 2009 8:49 am

printing fetched values

Post by drschwartz »

So, I'm trying to create a html table based on data drawn from a database. I want to format each table cell and so intend to specify each separately.

Here's the code snippet:

Code: Select all

 
// Until there are no rows in the result set, fetch a row into
// the $row array and ...
while ($row = @ mysql_fetch_row($result))
{
// ... start a TABLE row ...
print "\n<tr>";
 
// ... and print out each of the attributes in that row as a
// separate TD (Table Data).
print "\n\t<td width='20'> {$row["part_id"]} </td>";
print "\n\t<td width='40'> {$row["household_rep"]} </td>";
print "\n\t<td width='50'> {$row["street_address"]} </td>";
print "\n\t<td width='20'> {$row["city"]} </td>";
print "\n\t<td width='10'> {$row["state"]} </td>";
print "\n\t<td width='20'> {$row["zipcode"]} </td>";
print "\n\t<td width='30'> {$row["phone"]} </td>";
print "\n\t<td width='30'> {$row["email"]} </td>";
 
// Finish the row
print "\n</tr>";
}
 
// Then, finish the table
print "\n</table>\n";
 
While this seems to be what's suggested in the text I'm using, I get the following errors:

Notice: Undefined index: part_id in C:\wamp\www\report.php on line 29
Notice: Undefined index: household_rep in C:\wamp\www\report.php on line 30
Notice: Undefined index: street_address in C:\wamp\www\report.php on line 31
Notice: Undefined index: city in C:\wamp\www\report.php on line 32
Notice: Undefined index: state in C:\wamp\www\report.php on line 33
...etc

These are the lines where the specific attribute values are printed.

Any ideas what the problem might be?
drschwartz
Forum Newbie
Posts: 12
Joined: Fri Feb 27, 2009 8:49 am

Re: printing fetched values

Post by drschwartz »

FYI, I saw the nested double-quotes and changed the inner quote to single-quotes. I'm getting the same result, however.

TIA,
David
drschwartz
Forum Newbie
Posts: 12
Joined: Fri Feb 27, 2009 8:49 am

Re: printing fetched values

Post by drschwartz »

I figured out that I should be using mysql_fetch_array if I want an associative array.

Thanks,
David
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: printing fetched values

Post by susrisha »

I figured out that I should be using mysql_fetch_array if I want an associative array.
You can also use mysql_fetch_assoc
drschwartz
Forum Newbie
Posts: 12
Joined: Fri Feb 27, 2009 8:49 am

Re: printing fetched values

Post by drschwartz »

Are there any advantages to one over the other?

Thanks,
David
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: printing fetched values

Post by susrisha »

Its the data that is got that matters.

The mysql_fetch_assoc returns only the associative array that you require.
The mysql_fetch_array returns both the associative as well as the normal array.
example:

Code: Select all

 
$fetch_array=([0]=>firstfieldvalue,[firstfieldname]=>firstfieldvalue,.....);
 
$fetch_assoc = ([firstfieldname]=>firstFieldValue......);
 
drschwartz
Forum Newbie
Posts: 12
Joined: Fri Feb 27, 2009 8:49 am

Re: printing fetched values

Post by drschwartz »

No difference in overhead or speed? I would imagine that there might be an overhead issue given the result of mysql_fetch_array is more complex.

David
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: printing fetched values

Post by susrisha »

Post Reply