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:

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.