mysql_query and print exiting script

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
Clukey
Forum Commoner
Posts: 60
Joined: Fri Apr 21, 2006 9:05 pm

mysql_query and print exiting script

Post by Clukey »

I am trying to execute a print command and then a mysql_query command, but whichever I do the command after it won't execute. For example, if my code is:

Code: Select all

print $buffer;
mysql_query ("UPDATE verify_payments SET downed='0' WHERE ip='".ip2long(gethostbyname($REMOTE_ADDR))."'", $connection);
The print will execute, but the query will not, and the print is stopping the page from loading also. Why is this? Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Have you checked your error logs?
Clukey
Forum Commoner
Posts: 60
Joined: Fri Apr 21, 2006 9:05 pm

Post by Clukey »

no, but they both work if their first, just not thogether.
Clukey
Forum Commoner
Posts: 60
Joined: Fri Apr 21, 2006 9:05 pm

Post by Clukey »

I just checked, and there are no errors in the log
Clukey
Forum Commoner
Posts: 60
Joined: Fri Apr 21, 2006 9:05 pm

Post by Clukey »

Here's the code if it helps:

Code: Select all

$connection = mysql_connect ("localhost", "usename", "password") or die ('I cannot connect to the database because: ' . mysql_error());
  mysql_select_db ("database");
  $result = mysql_query ("SELECT * FROM verify_payments ORDER BY id DESC");
  $row_Recordset1 = mysql_fetch_assoc($result);

  $db_payer_email = $row_Recordset1['name'];
  $db_txn_id = $row_Recordset1['txn_id'];
  $db_item_number = $row_Recordset1['item_number'];
  $db_ip = $row_Recordset1['ip'];
  $db_downed = $row_Recordset1['downed'];

  if ($db_ip = ip2long(gethostbyname($REMOTE_ADDR)) && $db_downed == "1") {

$fname = "file.mxp";
$fpath = "folder1/folder2/$fname";
$fsize = filesize($fpath);
$bufsize = 20000;

if(isset($_SERVER['HTTP_RANGE']))  //Partial download
{
   if(preg_match("/^bytes=(\\d+)-(\\d*)$/", $_SERVER['HTTP_RANGE'], $matches)) { //parsing Range header
       $from = $matches[1];
       $to = $matches[2];
       if(empty($to))
       {
           $to = $fsize - 1;  // -1  because end byte is included 
                               //(From HTTP protocol:
// 'The last-byte-pos value gives the byte-offset of the last byte in the range; that is, the byte positions specified are inclusive')
       }
       $content_size = $to - $from + 1;

       header("HTTP/1.1 206 Partial Content");
       header("Content-Range: $from-$to/$fsize");
       header("Content-Length: $content_size");
       header("Content-Type: application/force-download");
       header("Content-Disposition: attachment; filename=$fname");
       header("Content-Transfer-Encoding: binary");


       if(file_exists($fpath) && $fh = fopen($fpath, "rb"))
       {
           fseek($fh, $from);
           $cur_pos = ftell($fh);
           while($cur_pos !== FALSE && ftell($fh) + $bufsize < $to+1)
           {
               $buffer = fread($fh, $bufsize);
               print $buffer;
               $cur_pos = ftell($fh);
           }

           $buffer = fread($fh, $to+1 - $cur_pos);
           print $buffer;
	mysql_query ("UPDATE verify_payments SET downed='0' WHERE ip='".ip2long(gethostbyname($REMOTE_ADDR))."'", $connection);

           fclose($fh);
       }
       else
       {
           header("HTTP/1.1 404 Not Found");
           exit;
       }
   }
   else
   {
       header("HTTP/1.1 500 Internal Server Error");
       exit;
   }
}
else // Usual download
{
   header("HTTP/1.1 200 OK");
   header("Content-Length: $fsize");
   header("Content-Type: application/force-download");
   header("Content-Disposition: attachment; filename=$fname");
   header("Content-Transfer-Encoding: binary");

   if(file_exists($fpath) && $fh = fopen($fpath, "rb")){
       while($buf = fread($fh, $bufsize))
           print $buf;
	mysql_query ("UPDATE verify_payments SET downed='0' WHERE ip='".ip2long(gethostbyname($REMOTE_ADDR))."'", $connection);
       fclose($fh);
   }
   else
   {
       header("HTTP/1.1 404 Not Found");
   }
}
}
//session_unset();
mysql_free_result($connection);
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

If you remove the print() does the script then work?
(#10850)
Clukey
Forum Commoner
Posts: 60
Joined: Fri Apr 21, 2006 9:05 pm

Post by Clukey »

the only error I got was a bad mysql_free_result reference, which I fixed; but the rest works.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

So the whole script works correctly without errors, but when you add the line "print $buffer;" is stops working?

What if you change that line to something like "print strlen($buffer);" ?
(#10850)
Clukey
Forum Commoner
Posts: 60
Joined: Fri Apr 21, 2006 9:05 pm

Post by Clukey »

arborint wrote:So the whole script works correctly without errors, but when you add the line "print $buffer;" is stops working?

What if you change that line to something like "print strlen($buffer);" ?
The print didn't work, but everything else did.
Clukey
Forum Commoner
Posts: 60
Joined: Fri Apr 21, 2006 9:05 pm

Post by Clukey »

Sorry :oops: ...it's not the print that cancels the script it's the fclose($fh). If I move the mysql_query after it the query doesn't work, if it is before the fclose($fh) the query works. Sorry about that.
Clukey
Forum Commoner
Posts: 60
Joined: Fri Apr 21, 2006 9:05 pm

Post by Clukey »

But I still can't figure out why it isn't working.
Post Reply