1

I have this code, i would like to print a table with content. It prints table but the columns are empty... What am I missing? I cannot see the mistake..

function show_autovetture($data){

    $con = $data;               //PASSO CONNESSIONE
    $rows = array();            // PREPARO ARRAY

    $stmt = mysqli_stmt_init($con); // INIZIALIZZO LA CONNESSIONE

    mysqli_stmt_prepare($stmt,'SELECT
                        autovetture.id,
                        autovetture.name
                        FROM autovetture
                        ORDER BY id DESC');
                        or die(mysqli_error($con)); // QUERY INSERIMENTO DATI

    mysqli_stmt_execute($stmt);                    // ESEGUO LA QUERY

    mysqli_stmt_bind_result($stmt,$rows['id'],$rows['name']);

    $html = "";
    $html .= "<div class='container'>";
    $html .= "<div class='div_box_5'>";
    $html .= "<div class='row'>";
    $html .= "<div class='panel panel-default'>";
    $html .= "<div class='panel-heading'>";
    $html .= "<h4>";
    $html .= "Elenco Ditte Autovetture"; 
    $html .= "</h4>";
    $html .= "</div> <!-- end panel-heading -->";
    $html .= "<table class='table table-fixed'>";
    $html .= "<thead>";
    $html .= "<tr>";
    $html .= "<th class='col-xs-2'>#</th><th class='col-xs-8'>Nome</th><th class='col-xs-2'>Modifica</th>";
    $html .= "</tr>";
    $html .= "</thead>";
    $html .= "<tbody>";
    while($rows=mysqli_stmt_fetch($stmt)){
            $html .= "<tr>";
            $html .= "<td class='col-xs-2'>$rows[id]</td><td class='col-xs-8'>$rows[name]</td><td class='col-xs-2'><a href='edit_autovet.php?id=$rows[id]'><span class='glyphicon glyphicon-pencil'></span></a></td>";
            $html .= "</tr>"; 
    }
    $html .= "</tbody>";
    $html .= "</table>";
    $html .= "</div> <!-- end panel panel-default -->";
    $html .= "</div> <!-- end row -->";
    $html .= "</div> <!-- end div_box_5 -->";
    $html .= "</div> <!-- end container -->";


    mysqli_stmt_close($stmt);                                       // CHIUDO LO STATEMENT
    mysqli_close($con);                                             // CHIUDO CONNESSIONE   

    return $html;

}

Where is the error?

3
  • read how to check for mysqli errors Commented May 15, 2017 at 22:30
  • the problem is that i have no errors on screen.. how can I check them? Commented May 15, 2017 at 22:34
  • 1
    I found that mysqli had somewhat of a learning curve but once you get it, it is very robust. You can use php.net/mysqli_error mysqli_error($link) Commented May 16, 2017 at 2:54

1 Answer 1

2

The results are already bound when you bind them with mysqli_stmt_bind_result($stmt,$rows['id'],$rows['name']);, and they will be in those variables even when you loop - the contents of the variable will be updated for each iteration of the loop while fetching - so you just want to loop with mysqli_stmt_fetch() as the argument, without assigning to a variable, like this

while (mysqli_stmt_fetch($stmt)) {

instead of

while($rows=mysqli_stmt_fetch($stmt)){

Fetching data is a bit different with using a statement than fetching using a mysqli_fetch_*() method.

You're currently overwriting the $rows variable, making it a boolean (true/false) or null when you run it like you currently are. See the manual of mysqli_stmt_fetch() for the return-values.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you saved my day!, stupid mistake! thank you so much :)
Happy to help! :-) you can also use a regular query here, as you're not binding any placeholders (variables or user-input) in the query. But there's nothing wrong with preparing, it's just a little more work :-) Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.