0

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>
5
Contributors
11
Replies
51
Views
4 Years
Discussion Span
Last Post by EvolutionFallen
0

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.

0

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?

Edited by pritaeas

0

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

0

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

0

add single quotes arround $uid

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

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.