hnmcc Posted February 19, 2011 Share Posted February 19, 2011 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. Link to comment https://forums.phpfreaks.com/topic/228216-confused-by-the-data-im-receiving-from-mysql/ Share on other sites More sharing options...
BlueSkyIS Posted February 19, 2011 Share Posted February 19, 2011 mysql_fetch_array() retrieves one row of data. you'll need to loop over the rows to get them all. Link to comment https://forums.phpfreaks.com/topic/228216-confused-by-the-data-im-receiving-from-mysql/#findComment-1176859 Share on other sites More sharing options...
Veteah Posted February 19, 2011 Share Posted February 19, 2011 You'd want to put mysql_fetch_array($sql, MYSQL_NUM) or mysql_fetch_array($sql, MYSQL_ASSOC) Yeah, and loop it. Obviously Link to comment https://forums.phpfreaks.com/topic/228216-confused-by-the-data-im-receiving-from-mysql/#findComment-1176860 Share on other sites More sharing options...
hnmcc Posted February 19, 2011 Author Share Posted February 19, 2011 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? Link to comment https://forums.phpfreaks.com/topic/228216-confused-by-the-data-im-receiving-from-mysql/#findComment-1176869 Share on other sites More sharing options...
Pikachu2000 Posted February 19, 2011 Share Posted February 19, 2011 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']; } Link to comment https://forums.phpfreaks.com/topic/228216-confused-by-the-data-im-receiving-from-mysql/#findComment-1176871 Share on other sites More sharing options...
hnmcc Posted February 19, 2011 Author Share Posted February 19, 2011 Thanks Pikachu. I did say it was obvious, for those whose eyes were rolling... Link to comment https://forums.phpfreaks.com/topic/228216-confused-by-the-data-im-receiving-from-mysql/#findComment-1176877 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.