Passing A Value By Reference Between Scripts - How?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Wayne Herbert
Forum Commoner
Posts: 34
Joined: Tue Apr 29, 2003 3:13 pm
Location: Houston, Texas

Passing A Value By Reference Between Scripts - How?

Post by Wayne Herbert »

I've used a variation of a couple of scripts floating around the net in order to display images stored in a MySql database. Leaving out the database connections, opens, etc., the calling script queries for a set of database records for the page, then uses an <img scr> tag for display which calls another php routine, as in:

$query = "SELECT thumbid, annotation, filename FROM thumbs
WHERE pageno = 1";
(!($result = @ mysql_query ($query, $dbh)));
?>

<h1>Image Database</h1>

<?php
if ($row = @ mysql_fetch_array($result))
{
?>
<center><table BORDER=2 COLS=1 WIDTH="80%" >
<?php
do
{
?>
<tr>
<td><?php echo "{$row["filename"]}";?></td>
<td><?php echo "<img src=\"getdbblob.php?file={$row["thumbid"]}\">";?></td>
</tr>
<?php
} while ($row = @ mysql_fetch_array($result));
?>
</table>
<?php
} // if mysql_fetch_array()
?>

</table></center>
</body>
</html>

Now, the script, getdbblob.php also does a query to get a record on the passed record id.

<?php
ob_start(); // turn output buffering on
$query = "SELECT thumbnail FROM thumbs
WHERE thumbid = $file";
(!($result = @ mysql_query ($query, $dbh)));
$data = @mysql_fetch_array($result);
$image = $data["thumbnail"];
header("Content-Type: image/jpeg");
echo $image;
ob_end_flush();
?>

Now this seems wasteful because every image has to be queried out of the DB when it already exists in the calling script. I'd like to just pass the image data to the getblobdb script so there would be no need for a second query, but passing by value will make a copy of what _could_ be a large amount of data.

I need the format/syntax for passing & retrieving by reference.

TIA
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

I'm a lazy sob and didnt read the whole thing, just wanted to mention some on passing by reference;

You can pass by reference to functions within a script (that includes included/required files ofcourse).

You can not pass any data by reference from one page-load to another (which s what I intepret "script to script" as). The only way to transfer data here is to use sessions or get/post or perhaps a cache-file..

Passing by ref to a function is simple, prefix with the & sign..

Code: Select all

<?php
  function foostuff (&$myval) {
     $myval++;
  }

  $beers = 1;
  foostuff (&$beers);
  // $beers now is two

?>
wouldnt really need both th &'s, which to keep would depend on what functionality you need..
There are some differences regarding references vars, check the manual on that, the major "issue" is that a unset($myval) would only remove the reference and not unset $beer in main...
Post Reply