Hi, am trying to display the filename after upload but can't display, instead error message "There are no files in the database" is displayed. Please advise. Thanks.

    <?php
    // Connect to the database
    $dbLink = new mysqli('localhost','user','','pq');
    if(mysqli_connect_errno()) {
        die("MySQL connection failed: ". mysqli_connect_error());
    }
    // Get the ID
    // Query for a list of all existing files

    $progressid=$row['Progressid'];
    //echo $progressid;
    $id= $row['id'];
    echo $id;
    $sql = 'SELECT `id`, `name`, `mime`, `size`, `created`,`Progressid` FROM `tfile` where `id`=$row['id']';    
    $result = $dbLink->query($sql);

     // Check if it was successfull
    if($result) {
        // Make sure there are some files in there
        if($result->num_rows == 0) {
            echo '<p>There are no files in the database</p>';
        }
        else {
            // Print the top of a table
            echo '<table width="100%">
                    <tr>
                        <td><b>Name</b></td>
                    </tr>';

            // Print each file
            while($row = $result->fetch_assoc()) {
                echo "
                    <tr>
                        <td><a href='get_file.php?id={$row['id']}'>{$row['name']}</td>
                    </tr>";
            }

            // Close table
            echo '</table>';
        }

        // Free the result
        $result->free();
    }
    else
    {
        echo 'Error! SQL query failed:';
        echo "<pre>{$dbLink->error}</pre>";
    }

    // Close the mysql connection
    $dbLink->close();
    ?>

Dani AI

Generated

and are right to look at how the SQL string is built; 's editor suggestion is also useful for spotting these mistakes. The French message "Erreur de syntaxe près de '' à la ligne 1" normally means the server received an incomplete statement — for example a WHERE clause that ends up as WHERE id = with no value. That happens when the value used in the query is empty (often because the variable was never set) or when interpolation failed.

Quick checklist to resolve it:

  • Confirm where $row is coming from. Any assignment that reads $row['id'] must happen after a successful fetch. If $row is not set yet, remove those early lines and obtain the id explicitly (for example from the request) before building the query.
  • Don’t rely on variable interpolation inside single quotes. Use a prepared statement or build the SQL after the ID is known and validated.
  • Debug by printing or logging the actual SQL string that will be sent to MySQL (or log the raw ID) so the query can be inspected before execution.
  • Fix HTML output: ensure the file link tag is closed and escape filenames before output to avoid broken HTML or XSS.

Minimal sanity-check example (debugging only — not the full fetch/display code):

$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
if ($id <= 0) { echo 'No valid id supplied.'; exit; }
error_log("Will query with id=$id");

After that, use a parameterised query (prepared statement) to run the lookup and, when printing the filename, wrap it with htmlspecialchars() and include a proper closing </a> for the link. During development enable mysqli errors with mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); and check $dbLink->error to get the exact server-side message.

Recommended Answers

All 6 Replies

$sql = 'SELECT id, name, mime, size, created,Progressid FROM tfile where id=$row['id']';

That should give an error. Try this:

$sql = "SELECT `id`, `name`, `mime`, `size`, `created`, `Progressid` FROM `tfile` where `id` = {$row['id']}";
Member Avatar for Member #269073

you have included the variable within single quotes on line 14 $row['id']

$sql = 'SELECT id, name, mime, size, created, Progressid FROM tfile where id = '.$row['id'];
Member Avatar for Member #120589

@Sophia - get an editor or IDE that checks code and automatically flags errors.

Hi, tried both of your suggestions but still can't display the filename. The message display "Erreur de syntaxe pr�s de '' � la ligne 1". Please advise. Thanks.

Member Avatar for Member #120589

Syntax error on line 1? Doesn't sound as though it's anything to do with the SQL statement.

Member Avatar for Member #269073

is there any more code before the one above as line 1 is <?php and that looks fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.