This is the code I am using but it keeps giving me this error:

Failed to run query: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':' at line 1

I have tried a easier alternative to doing what I want but I got more errors that left me more confused than this piece of code. Any help will be greatly appreciated!

$query = "SELECT user_id, track_name, contents FROM track where user_id = $uid"; 

    try 
    { 
        // These two statements run the query against your database table. 
        $stmt = $db->prepare($query); 
        $stmt->execute(array()); 
    } 
    catch(PDOException $ex) 
    { 
        // Note: On a production website, you should not output $ex->getMessage(). 
        // It may provide an attacker with helpful information about your code.  
        die("Failed to run query: " . $ex->getMessage()); 
    } 

    // Finally, we can retrieve all of the found rows into an array using fetchAll 
    $rows = $stmt->fetchAll(); 


        ?>

<p> Welcome to tracks :D </p> <table style="padding-bottom:30px; padding-left:10px;s"> 
    <tr> 
        <th>Track Name</th>
        <th>  Contents</th>
    </tr> 
    <?php foreach($rows as $row): ?> 
        <tr> 
            <td><?php echo htmlentities($row['track_name'], ENT_QUOTES, 'UTF-8'); ?></td>
            <td>     <?php echo htmlentities($row['contents'], ENT_QUOTES, 'UTF-8'); ?></td>
        </tr> 
    <?php endforeach; ?> 
</table>

Recommended Answers

All 11 Replies

Try:

$stmt->execute();

Think it doesn't like to bind an empty array.

Did that error hasn't changed :/

Are you sure $uid has a value? The error message shows a colon, where is that coming from?

Yep this is the function that I have coming from a include file

$uid = $_SESSION['user']['id'];

as I want it to echo the stuff thats in the table that is that users.

Hmz. When you output the error message, be sure to include the $query, perhaps if you see that, it's obvious. Also, are you sure your connection succeeded?

I'm not sure I quite understand you can you be more precise please? thanks.

See the exception handler here.

Sorry but wouldn't know how to implement it properly.

Try running the query on a query tool like mySQL Workshop using a valid value for your variable and see if it runs.

add single quotes arround $uid

$query = "SELECT user_id, track_name, contents FROM track where user_id = '$uid'";

Is $uid expected to be a number or a string? Try adding echo $uid; to see what value it outputs. If indeed it's a string, then like chemwile suggested, you would need single quotes in the query.

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.