Search code examples
phpmysqlmysqliprepared-statement

`SELECT` Query not working for prepared statement


I am trying to show all of the users in the users column of my database but nothing is showing up. I want to have a ranking of users based on the number of likes they get. My query looks fine to me but it's not showing anything. No warnings, errors or the output I am calling for. Can someone please tell me what I'm doing wrong ? I've checked all the answers for about an hour, then I tried to use the prepared statements that I already have. But nothing works.

This is the whole page:

<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
error_reporting(-1);

require 'includes/header.php';

$stmt = $con->prepare('SELECT username, num_likes FROM users WHERE username = ? ORDER BY num_likes DESC ');
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($username, $num_likes);

// output data of each row
while ($stmt -> fetch()) {
    
    echo '<div id="rcorners2">';
            
    echo '<p id="p2">' . $username . '</p>';

    echo '<p id="p4">' . $num_likes . '</p>';   
    
    echo'</div>';
    echo'</br>';

}

?>

<!DOCTYPE html>
<html>
<head>
<title>User's Ranks</title>
<style type="text/css">
    
#rcorners2 {
    border-radius: 5px;
    border: 2px solid #8c8c8c;
    padding: 20px;
    width: 500px;
    word-wrap: break-word;
    height: 200px;
    overflow-x: hidden;
    margin-left: 350px;
}
#p2 {
    float:left;
    color: #000000;
    margin-top: -10;
    font-size: 20px;
}
#p4 {
    float:left;
    color: #000000;
    margin-top: -1%;
    font-size: 20px;
}
</style>
</head>
<body>

</body>
</html>

Solution

  • You are also ouputting the result of the query before you output the HTML <!DOCTYPE html> and other base parts of the HTML Document. So even if you get any rows they will not appear in the document

    If you output your info in the Document <body> it will show on the page

    <!DOCTYPE html>
    <html>
    <head>
    <title>User's Ranks</title>
    <style type="text/css">
        
    #rcorners2 {
        border-radius: 5px;
        border: 2px solid #8c8c8c;
        padding: 20px;
        width: 500px;
        word-wrap: break-word;
        height: 200px;
        overflow-x: hidden;
        margin-left: 350px;
    }
    #p2 {
        float:left;
        color: #000000;
        margin-top: -10;
        font-size: 20px;
    }
    #p4 {
        float:left;
        color: #000000;
        margin-top: -1%;
        font-size: 20px;
    }
    </style>
    </head>
    <body>
    <?php
    
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    error_reporting(-1);
    require 'includes/header.php';
    
    $stmt = $con->prepare('SELECT username, num_likes 
                            FROM users WHERE username = ? 
                            ORDER BY num_likes DESC ');
    $stmt->bind_param('s', $username);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($username, $num_likes);
    
    // output data of each row
    while ($stmt -> fetch()) {
        echo '<div id="rcorners2">';
        echo '<p id="p2">' . $username . '</p>';
        echo '<p id="p4">' . $num_likes . '</p>';   
        echo'</div>';
        echo'</br>';
    }
    ?>
    </body>
    </html>
    

    You also say you want ALL your users to dispay, so the query may also need changing to get all users where yours gets only one.

    $result = $con->query('SELECT username, num_likes 
                            FROM users
                            ORDER BY num_likes DESC ');
    
    // output data of each row
    while ($row = $result->fetch_assoc()) {
        echo '<div id="rcorners2">';
            echo '<p id="p2">' . $row['username'] . '</p>';
            echo '<p id="p4">' . $row['num_likes'] . '</p>';   
        echo'</div>';
        echo'</br>';