Jump to content

Confused by the data I'm receiving from MySQL


hnmcc

Recommended Posts

Apologies, first off: I only venture into PHP occasionally and my problem is something very very obvious...

 

The data I'm getting back from the script below differs radically from the data recovered by the same SQL applied directly in phpMyAdmin.

 

$sql = mysql_query('SELECT `fid` , `title` , `extension` FROM files WHERE `mid_FK` = 2353 ORDER BY `title`') or die ("Sorry: cannot retrieve files.");

$files_found = mysql_fetch_array($sql);

$num_files_found = count($files_found);

 

The native SQL returns three rows from the table, all with entries in each column.  What my script produces as relevant variables is this.

 

[sql] => Resource id #6
[files_found] => Array
        (
        [0] => 961
        [fid] => 961
        [1] => Abstract of Case
        [title] => Abstract of Case
        [2] => doc
        [extension] => doc
        )

[num_files_found] => 6

 

This is the first row of the SQL results, duplicated.  Whereas what I need are the entries for all three rows.

 

I'm baffled, not least because I copied it out of another script I wrote last time I was doing this sort of thing, which has been working fine ever since.  :confused:

Thanks for these tips.

 

Adding the MYSQL_ASSOC parameter gets rid of the duplication.  Which is good.

 

I've tried looping through the array as below.

 

print "<table>\n";
foreach ($files_found as $key => $value) {
print "<tr><td>$key</td><td>$value</td></tr>\n"; 
}
print '</table>';

 

It shows data from just one row (the first) of the three returned by the SQL.

fid	961
title	Abstract of Case
extension	doc

 

Is my loop wrong, or is the rest of the data not in the array?

mysql_fetch_* only retrieves one row of data at a time. You need to use it in conjunction with a while() loop.

 

while( $array = mysql_fetch_assoc($result) ) {
     echo $array['field'];
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.