Jump to content

Help Downloading Files from Database


$Three3

Recommended Posts

Hey everyone, I need help with this simple script. Okay, I have files stored in a database that need to be downloaded. Here is the script I have so far:

 

<?php

//Connect to the database
require_once(/mysql_connect.php') ;

$query = "SELECT enrollment_form FROM students where student_id = {$_GET['student']}" ; 
$result = mysqli_query($dbc, $query) or die('There was an error with the query. ' . mysqli_error($dbc)) ;
$num = mysqli_num_rows($result) ;

if ($num == 1) {

//Query was successfull
//Retrieve the file
$data = mysql_result($result, 0, "enrollment_form") ;
Header("Content-type: $data") ; 
echo $data ;

} else {

//The record does not exists
echo '<p>The record does not exists.</p>' ;

}

?>

 

The error I am getting is this:

 

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/content/l/i/n//html/Payments/download_enrollment.php on line 14

 

Warning: Cannot modify header information - headers already sent by (output started at /home/content/l/i/n//html/Payments/download_enrollment.php:14) in /home/content/l/i/n//html/Payments/download_enrollment.php on line 15

 

Any help on this is greatly appreciated.

 

Link to comment
Share on other sites

Hey everyone, I need help with this simple script. Okay, I have files stored in a database that need to be downloaded. Here is the script I have so far:

 

<?php

//Connect to the database
require_once('/mysql_connect.php') ;

$query = "SELECT enrollment_form FROM students where student_id = {$_GET['student']}" ; 
$result = mysqli_query($dbc, $query) or die('There was an error with the query. ' . mysqli_error($dbc)) ;
$num = mysqli_num_rows($result) ;

if ($num == 1) {

//Query was successfull
//Retrieve the file
$data = mysql_result($result, 0, "enrollment_form") ;
Header("Content-type: $data") ; 
echo $data ;

} else {

//The record does not exists
echo '<p>The record does not exists.</p>' ;

}

?>

 

The error I am getting is this:

 

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/content/l/i/n//html/Payments/download_enrollment.php on line 14

 

Warning: Cannot modify header information - headers already sent by (output started at /home/content/l/i/n//html/Payments/download_enrollment.php:14) in /home/content/l/i/n//html/Payments/download_enrollment.php on line 15

 

Any help on this is greatly appreciated.

 

Why use mysql_result? Taken from the PHP manual:

When working on large result sets, you should consider using one of the functions that fetch an entire row (specified below). As these functions return the contents of multiple cells in one function call, they're MUCH quicker than mysql_result().

 

Try mysqli_fetch_row... or better yet mysqli_fetch_assoc

 

while ($row = mysqli_fetch_row($result))
{echo "Name: {$row['1']} Address {$row['2']}";
}

Link to comment
Share on other sites

Hey everyone, I need help with this simple script. Okay, I have files stored in a database that need to be downloaded. Here is the script I have so far:

 

<?php

//Connect to the database
require_once('/mysql_connect.php') ;

$query = "SELECT enrollment_form FROM students where student_id = {$_GET['student']}" ; 
$result = mysqli_query($dbc, $query) or die('There was an error with the query. ' . mysqli_error($dbc)) ;
$num = mysqli_num_rows($result) ;

if ($num == 1) {

//Query was successfull
//Retrieve the file
$data = mysql_result($result, 0, "enrollment_form") ;
Header("Content-type: $data") ; 
echo $data ;

} else {

//The record does not exists
echo '<p>The record does not exists.</p>' ;

}

?>

 

The error I am getting is this:

 

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/content/l/i/n//html/Payments/download_enrollment.php on line 14

 

Warning: Cannot modify header information - headers already sent by (output started at /home/content/l/i/n//html/Payments/download_enrollment.php:14) in /home/content/l/i/n//html/Payments/download_enrollment.php on line 15

 

Any help on this is greatly appreciated.

 

Why use mysql_result? Taken from the PHP manual:

When working on large result sets, you should consider using one of the functions that fetch an entire row (specified below). As these functions return the contents of multiple cells in one function call, they're MUCH quicker than mysql_result().

 

Try mysqli_fetch_row... or better yet mysqli_fetch_assoc

 

while ($row = mysqli_fetch_row($result))
{echo "Name: {$row['1']} Address {$row['2']}";
}

 

Hey thanks for the reply. Okay the code you gave me got rid of the errors but now instead of actually downloading the file, it trys to display it in the web browser. The file that is stored in the database is a PDF. Here is the new code:

 

<?php

//Connect to the database
require_once('/mysql_connect.php') ;

$query = "SELECT enrollment_form FROM students where student_id = {$_GET['student']}" ; 
$result = mysqli_query($dbc, $query) or die('There was an error with the query. ' . mysqli_error($dbc)) ;
$num = mysqli_num_rows($result) ;

if ($num == 1) {

//Query was successfull
//Retrieve the file
while ($row = mysqli_fetch_row($result)) {

	echo $row[0] ;

}

} else {

//The record does not exists
echo '<p>The record does not exists.</p>' ;

}

?>

 

And here is part of the output that it is giving me in the web browser:

 

%PDF-1.4 %°¸º• 1 0 obj<> endobj 5 0 obj<> endobj 6 0 obj<>stream xœ+ä234R0PÐ5P0·1’s¹ô=s \ò¹¹¸o• endstream endobj 9 0 obj 38 endobj 8 0 obj<>stream ÿØÿàJFIF°°ÿÛ„  (1#%(:3=<9387@H\N@DWE78PmQW_bghg>Mqypdx\egc//cB8BccccccccccccccccccccccccccccccccccccccccccccccccccÿÄ¢	 }!1AQa"q2‘¡#B±ÁRÑð$3br‚ %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚáâãäåæçèéêñòóôõö÷øùú	 w!1AQaq"2B‘¡±Á	#3RðbrÑ $4á%ñ&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz‚ƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚâãäåæçèéêòóôõö÷øùúÿÀ3'ØÿÚ?ô ( € ( € (   € ( € ( € (   €€ ( € (  €€ (h € JZJ(h €€ (   € Z( € ((h(h € ( € JZ( € (     € (    €€ ((  € ( € ( € ( € (   € ( €€ ZJZ( € ( € J(  € ((h € ( € (   € ( € ( €€€ ( € ( € ( € ( € ( € ( € ( € ( €€€ ( € JZ( €€€ ( € ( € ((h €€ Z( € ( € ( € ( € JZ((  € (  €€ ((h €€€ ( €€€ ( € JZ( € ( € ( € ( € ( € ( € ( € ( € ( € JZ((h € ( € ( € ( € JZ(   € (   € ( € ( € ( €€€ ( € ( € ( € ( € ( € ( € ( €€ Z( € ( € ( € ( € ( € ( € ( € ( € ( € JZ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € JZ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € (   € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( €€€ ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( €€€ ( € ( € ( € ( € ( € ((h € ( € ( € ( € ( € ( € ((h € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € (   € ( € JZ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € (   € ( € ( € ( € ( € ( € ( € J(h € ( € ( € ( € ((h € ( € ( € ( € ( € ( € (   € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ((h(h   € ( € ((  €€ Z(  €€€€€€€€ ((h € JZ( € ( € (  € Z( €€ Z( € JZ(( €€ ((h €€€ (   € ( € ( € J(h € ( € J(h € ( € JZ((h € JZ( € ( € ((h(h(h €€€ ((h(  €€€ ( € ( € ( € ( €€ Z(   € ( € ( € ( € JZ( € ( € J(h € ( € ( € J(h €€€ ( ÐÐÐ@	@@P@ @@”´P@	@@P@P@	@@%-P@P@P@P@PP@@PPÐ@	@-P@	@@%-”´P@%-%-P@%-P@P@P@	@@P@	@@”´P@P@ @@P@P@P@P@”´P@”´P@P@P@PPÐ@PPÐ@P@P@P@P@P@P@P@P@P@P@P@P@ @@P@P@P@P@PPÐ@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P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@%-P@P@P@P@	@@P@P@P@P@P@ @@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@	@@P@P@P@P@P@P@P@P@P@P@P@P@P@	@-%PÐ@”P@PÐ@%PÐPÐ@	@@%P@@	@´”PÐ@PP@@PPÐ@	@-P@PP@@%-%-P@P@	@-P@PPÐ@P@%-%-%-%-P@P@	@-”´P@P@P@P@P@P@P@P@P@ @@P@P@P@P@PPÐPÐ@P@P@PP@@P@P@	@@	@@	@@P@P@PPÐ@P@P@%PbohZ( € ( € ( € ( € ( € ( € ((  € (   € ( € ( € J(h € ( € (   € (  €€ J(h € ((  € ( € JZ( € ( € ( € ( € ( € ( € (   € (( €€ ((h € ( €€€ ( € JZ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( € ( €€€ ( € ( 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.