Jump to content

Passing Mysql Query Results Into A Function


mouli

Recommended Posts

I have a function that displays query results in rows of a given number of cells. It was written by Peter Cole so many thanks to him. The page has a query, the results of which are used by the function but I cant get the function read the array at all.
After structuring an sql query to the database I end up with this:

[code]
$artistlist = mysql_query($query_artistlist, $galleries) or die(mysql_error());
[/code]
This generates the results and this works fine outside the function.
Then the function looks like this:
[code]
function display_table(){
//make variables available outside function
global $totalRows_artistlist,$artistlist,$thumbrows,$thumbcols,$picvalign;

//loop for rows
for($r=1;$r<=$thumbrows;$r++){
    print '<tr>';
    //loop for columns
    for($c=1;$c<=$thumbcols;$c++){
        print '<td>';
            //break out array of values from query row
            $row = mysql_fetch_array($artistlist);
            $cell_gallery=$row['gallery'];
            $cell_artist_firstname=ucfirst($row['artist_firstname']);
            $cell_artist_lastname=ucfirst($row['artist_lastname']);
            $cell_position=$row['position'];
            
        print '<table width="100%">';
        print '<tr>';
        print "<td valign=$picvalign><div align=\"center\"><a href=\"intro_artists.php?gallery=$cell_gallery\"><img src=\"getdata.php?image=thumb&position=$cell_position\" border=\"0\" ></a></div>
                          </td>";
        print '</tr>';
        print '<tr>';
        print '<td valign="top"><div align="center">
                              <a href="intro_artists.php?gallery=$cell_gallery"><p> $cell_artist_firstname $cell_artist_lastname</p></a>
                            </div>
                          </td>';
        print '</tr>';
        print '<tr>';
        print '<td valign="top"><img src="images/spacer.gif" width="1"  height="<?php echo SPACERHEIGHT ?>"></td>';
        print '</tr>';
        print '</table>';
        print '</td>';
        }
    print '</tr>';
    }
}
[/code]

It draws the table etc but has no data so I'm guessing that the problem lies with the way that I'm handling the results array $artistlist.
Testing the contents of the variables $cell_gallery etc convinces me that it is failing to assign the array values to the variables below the comment "//break out array of values from query row".

Any ideas much appreciated. Thanks for your time.

mouli
[!--quoteo(post=388755:date=Jun 28 2006, 08:03 PM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Jun 28 2006, 08:03 PM) [snapback]388755[/snapback][/div][div class=\'quotemain\'][!--quotec--]
For starters Ive jsut got to say.. this is pretty sloppy coding / use a functions but anyway...

How are you checking your query actually returned results?
[/quote]

The sloppy coding is probably my own adaptation of the original function. I'd be happy to learn how it is sloppy so I can improve on it.
The query works because if i assign values from the results array outside the function and echo the variables I can confirm that the array is not empty as follows

[code]
$row = mysql_fetch_array($artistlist);
            $cell_gallery=$row['gallery'];
            $cell_artist_firstname=ucfirst($row['artist_firstname']);
            $cell_artist_lastname=ucfirst($row['artist_lastname']);
            $cell_position=$row['position'];
            print "gallery is $cell_gallery";
            print "firstname is $cell_artist_firstname";
            print "last name is $cell_artist_lastname";
            print "position is $cell_position";
[/code]

This code works outside the function but not inside the function??

Many thanks

mouli
Further experimentation has shown that its not the function thats the problem. If I construct the table outside the function it doen't work either. On the page there is another instance of using the contents of the query result and if I remove that then the table works so my question is .... why can I not use a statement like:

[code]
$row = mysql_fetch_assoc($artistlist);
[/code]
more than once in a page. Is it that the statement leaves $artistlist empty??

I might post this question as a new topic as well.

Many thanks

mouli

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.