$Three3 Posted March 12, 2010 Share Posted March 12, 2010 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 https://forums.phpfreaks.com/topic/194983-help-downloading-files-from-database/ Share on other sites More sharing options...
cs.punk Posted March 12, 2010 Share Posted March 12, 2010 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 https://forums.phpfreaks.com/topic/194983-help-downloading-files-from-database/#findComment-1025102 Share on other sites More sharing options...
$Three3 Posted March 12, 2010 Author Share Posted March 12, 2010 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@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Ð@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 https://forums.phpfreaks.com/topic/194983-help-downloading-files-from-database/#findComment-1025210 Share on other sites More sharing options...
$Three3 Posted March 12, 2010 Author Share Posted March 12, 2010 Anybody have an idea on what might make this work? Thanks Link to comment https://forums.phpfreaks.com/topic/194983-help-downloading-files-from-database/#findComment-1025342 Share on other sites More sharing options...
XeNoMoRpH1030 Posted March 12, 2010 Share Posted March 12, 2010 You probably need to specify a header so the browser knows what it is. Right now, it's being treated as plain text. header('Content-type: application/pdf'); Link to comment https://forums.phpfreaks.com/topic/194983-help-downloading-files-from-database/#findComment-1025360 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.