Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
issue with printing a table in php/mysqli

So basically my code isnt working here. The confusing part is that at a bare minimum it should be printing the table header, which it is not doing.

here is my function

public function getUsers(){
    global $connectstr_dbhost, $connectstr_dbname, $connectstr_dbpassword, $connectstr_dbusername;
    $link=mysqli_connect($connectstr_dbhost, $connectstr_dbusername, $connectstr_dbpassword,$connectstr_dbname);
    $sql = "SELECT * FROM `users`";
    $result = mysqli_query($link, $sql);



    echo ("
         <table border='1'>
         <tr>
         <th>ID</th>
         <th>First Name</th>
         <th>Last Name</th>
         <th>Username</th>
         <th>Email</th>
         </tr>

    ");

    while($row = mysqli_fetch_array($result))
    {
        echo(
            "<tr>" .
            "<td>" . $row['id']. "</td>".
            "<td>" . $row['fname'] . "</td>".
            "<td>" . $row['lname'] . "</td>".
            "<td>" . $row['username'] . "</td>".
            "<td>" . $row['email'] . "</td>".
            "</tr>"

        );
    }


    echo "</table>";
}

Pretty new to php and I feel as if the answer is really simple, any assistance would be appreciated.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Maybe is that `user` over user. Try and see if resolve. If not try this code:

[TRY Save the content of the function in another file and just make an echo to see if the function is called]

public function getUsers(){
    global $connectstr_dbhost, $connectstr_dbname, $connectstr_dbpassword, $connectstr_dbusername;
    $link = mysqli_connect($connectstr_dbhost, $connectstr_dbusername, $connectstr_dbpassword,$connectstr_dbname) or die('Não foi possível conectar');
    $sql = "SELECT * FROM users";
    $result = $link->query($sql);

    echo "
         <table border='1'>
         <tr>
         <th>ID</th>
         <th>First Name</th>
         <th>Last Name</th>
         <th>Username</th>
         <th>Email</th>
         </tr>

    ";

    while($row = $result->fetch_assoc()) {
        echo "<tr>" .
                "<td>" . $row['id']. "</td>".
                "<td>" . $row['fname'] . "</td>".
                "<td>" . $row['lname'] . "</td>".
                "<td>" . $row['username'] . "</td>".
                "<td>" . $row['email'] . "</td>".
            "</tr>";
    }

    echo "</table>";
}
over 4 years ago · Santiago Trujillo Report

0

Figured it out, the problem was I had not imported the database class.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!