Page 1 of 1

printing fetched values

Posted: Tue Mar 03, 2009 9:15 pm
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?

Re: printing fetched values

Posted: Tue Mar 03, 2009 9:30 pm
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

Re: printing fetched values

Posted: Tue Mar 03, 2009 9:35 pm
by drschwartz
I figured out that I should be using mysql_fetch_array if I want an associative array.

Thanks,
David

Re: printing fetched values

Posted: Tue Mar 03, 2009 10:16 pm
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

Re: printing fetched values

Posted: Tue Mar 03, 2009 10:39 pm
by drschwartz
Are there any advantages to one over the other?

Thanks,
David

Re: printing fetched values

Posted: Tue Mar 03, 2009 11:16 pm
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......);
 

Re: printing fetched values

Posted: Wed Mar 04, 2009 12:04 am
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

Re: printing fetched values

Posted: Wed Mar 04, 2009 12:27 am
by susrisha