Jump to content


Photo

Passing Mysql Query Results Into A Function


  • Please log in to reply
3 replies to this topic

#1 mouli

mouli
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 28 June 2006 - 07:54 AM

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:

$artistlist = mysql_query($query_artistlist, $galleries) or die(mysql_error());
This generates the results and this works fine outside the function.
Then the function looks like this:
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>';
    }
}

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

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 28 June 2006 - 08:03 AM

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?

#3 mouli

mouli
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 28 June 2006 - 08:33 AM

[!--quoteo(post=388755:date=Jun 28 2006, 08:03 PM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Jun 28 2006, 08:03 PM) View Post[/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

$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";

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

Many thanks

mouli

#4 mouli

mouli
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 28 June 2006 - 09:55 PM

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:

$row = mysql_fetch_assoc($artistlist);
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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users