denoteone Posted January 7, 2010 Share Posted January 7, 2010 I have a function that gets info from a DB and assigns the data to a variable. I then want to use the variable on the page that called the function. something like Function in my common.php file: function popUpData($type,$id){ $sql = ("SELECT dr_node.title AS Title, dr_content_type_readmore.field_readmore_text_value AS Body FROM dr_content_type_readmore, dr_node WHERE dr_content_type_readmore.nid = dr_node.nid And dr_content_type_readmore.nid = '$id' And dr_node.type = 'readmore' ORDER BY Title ASC"); $result = mysql_query($sql) or die ("Error in query: $query. ".mysql_error()); while($row = mysql_fetch_assoc($result)){ $title = $row['Title'] ; $story = $row['Body'] ; } } And here is the code on the page: <html> <head> <?PHP include("includes/common.php"); $db = new connection_tim(); $type = $_GET['type']; $id = $_GET['id']; popUpData($type,$id); ?> </head> <body> <div id="header"><?PHP echo $title; ?></div> <div id="body"><?PHP echo $story; ?></div> </body> </html> Any help would be awesome. Not sure what I am missing. Link to comment https://forums.phpfreaks.com/topic/187614-return-values-to-page-from-function/ Share on other sites More sharing options...
lemmin Posted January 7, 2010 Share Posted January 7, 2010 Your variables are only in the scope of your popUpData() function. Try having that function return all of the information. <?php function popUpData($type,$id){ [...] return $result; } while($row = mysql_fetch_assoc($result)){ [...] ?> Link to comment https://forums.phpfreaks.com/topic/187614-return-values-to-page-from-function/#findComment-990522 Share on other sites More sharing options...
denoteone Posted January 7, 2010 Author Share Posted January 7, 2010 mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource got an error. Link to comment https://forums.phpfreaks.com/topic/187614-return-values-to-page-from-function/#findComment-990533 Share on other sites More sharing options...
denoteone Posted January 7, 2010 Author Share Posted January 7, 2010 Should I be able to return the result of the sql query so that I can loop through and assign the values on the page that called the function? Link to comment https://forums.phpfreaks.com/topic/187614-return-values-to-page-from-function/#findComment-990545 Share on other sites More sharing options...
denoteone Posted January 7, 2010 Author Share Posted January 7, 2010 $result = popUpData($type,$id); while($row = mysql_fetch_assoc($result)){ $title = $row['Title'] ; $story = $row['Body'] ; } Link to comment https://forums.phpfreaks.com/topic/187614-return-values-to-page-from-function/#findComment-990550 Share on other sites More sharing options...
lemmin Posted January 8, 2010 Share Posted January 8, 2010 That should work fine. I can't see a reason for it to give that error with what you have posted. Can you post more of the code? Link to comment https://forums.phpfreaks.com/topic/187614-return-values-to-page-from-function/#findComment-991282 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.