By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,116 Members | 1,509 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,116 IT Pros & Developers. It's quick & easy.

Download Problem

P: 4
[Mod Edit: this question has been split off of the article: Uploading Files into MySql Database Using PHP]

I can't download.
I keep getting the following error:
Error! Query failed:
Query was empty
Expand|Select|Wrap|Line Numbers
  1.     <?php
  2.     // Connect to the database
  3.     $dbLink = mysql_connect("localhost","root","");
  4. if (!$dbLink)
  5.   {
  6.   die('Could not connect: ' . mysql_error());
  7.   }
  8.  
  9. mysql_select_db("filestorage", $dbLink);
  10.  
  11.     // Query for a list of all existing files
  12.     $sql = 'SELECT `id`, `name`, `mime`, `size`, `created` FROM `file`';
  13.      $result = mysql_query($query,$dbLink)
  14.             or die("Error! Query failed: <pre>". mysql_error($dbLink) ."</pre>");
  15.  
  16.  
  17.     // Check if it was successfull
  18.     if($result) {
  19.         // Make sure there are some files in there
  20.         if($result->num_rows == 0) {
  21.             echo '<p>There are no files in the database</p>';
  22.         }
  23.         else {
  24.             // Print the top of a table
  25.             echo '<table width="100%">
  26.                     <tr>
  27.                         <td><b>Name</b></td>
  28.                         <td><b>Mime</b></td>
  29.                         <td><b>Size (bytes)</b></td>
  30.                         <td><b>Created</b></td>
  31.                         <td><b>&nbsp;</b></td>
  32.                     </tr>';
  33.  
  34.             // Print each file
  35.             while($row = $result->fetch_assoc()) {
  36.                 echo "
  37.                     <tr>
  38.                         <td>{$row['name']}</td>
  39.                         <td>{$row['mime']}</td>
  40.                         <td>{$row['size']}</td>
  41.                         <td>{$row['created']}</td>
  42.                         <td><a href='get_file.php?id={$row['id']}'>Download</a></td>
  43.                     </tr>";
  44.             }
  45.  
  46.             // Close table
  47.             echo '</table>';
  48.         }
  49.  
  50.         // Free the result
  51.         //$result->free();
  52.            mysql_free_result($result);
  53.     }
  54.     else
  55.     {
  56.         echo 'Error! SQL query failed:';
  57.         echo "<pre>{$dbLink->error}</pre>";
  58.     }
  59.  
  60.     // Close the mysql connection
  61.     mysql_close($dbLink);
  62.     ?>
  63.  
  64.  
Nov 2 '11 #1
Share this Question
Share on Google+
1 Reply


100+
P: 1,059
you initialized $sql but you passed $query query was definitely empty
Nov 3 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.