1

I'm running a pretty basic script for a project a work, I have a database with a load of contacts in and I need the script to pull through the results and display a basic table, I had the script working but all of a sudden its now not pulling through any database info this is the code i'm using:

<?php
   $con=mysqli_connect("localhost","root","password","main");
   // Check connection
   if (mysqli_connect_errno())
     {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }

   $result = mysqli_query($con,"SELECT * FROM users");

   echo "<table border='1'>".
        "<tr><th>ID</th>".
        "<th>Rating</th>".
        "<th>Name</th>".
        "<th>Discipline</th>".
        "<th>Rate</th>".
        "<th>Location</th>".
        "<th>Number</th>".
        "<th>Email</th>".
        "</tr>";

  while($row = mysqli_fetch_array($result));
  {
    echo "<tr>";
    echo "<td>" . $row['ID'] . "</td>";
    echo "<td>" . $row['Rating'] . "</td>";
    echo "<td>" . $row['Name'] . "</td>";
    echo "<td>" . $row['Discipline'] . "</td>";
    echo "<td>" . $row['Rate'] . "</td>";
    echo "<td>" . $row['Location'] . "</td>";
    echo "<td>" . $row['Number'] . "</td>";
    echo "<td>" . $row['Email'] . "</td>";
    echo "</tr>";
  }
  echo "</table>";

  mysqli_close($con);

?>

if anyone any point me in the right direction that would be really great thank you.

EDIT: released I'd copied an older code that I was debugging with and didn't include the fetch_array.

Mûhámmàd Yäsår K
  • 1,402
  • 10
  • 22
Mouseman85
  • 31
  • 5
  • Did someone try to register with the username `TRUNCATE users` ? – CD001 Dec 16 '15 at 09:41
  • where is the while loop or fetch row? since $row is coming from where? – Sandeep J Patel Dec 16 '15 at 09:47
  • In term of debugging, I've tried to get the code to just list one item from database i.e just names, I've tried a more basic database with only 2 rows, I've tried different code completely, but couldn't get this to work the same way. I've also changed database info to make it fail on connection to test that it is actually connecting, and this does display error info that it can't connect to unknown database etc – Mouseman85 Dec 16 '15 at 09:52

3 Answers3

3

i see very small error in your code,

while loop dont work when you add semicolon.

e.g. from your code.

while($row = mysqli_fetch_array($result))

Thanks Amit

Amit Shah
  • 1,264
  • 1
  • 9
  • 19
0
$result = mysqli_query($con,"SELECT * FROM users");

In the above you're telling it to use the connection to a database and then select some data from a table in your database, however, you have not told it what to do IF the query fails, try the following:

$result = mysqli_query($con,"SELECT * FROM users") or die(mysqli_error($con));
0

To help debug in development mode:

Display php errors: How do I get PHP errors to display?

Get Mysqli to throw exceptions: How to make mysqli throw exceptions using MYSQLI_REPORT_STRICT?

Can you connect to your mysql database with another client and run queries there:

$ mysql -uusername -p databasename
mysql> SELECT * FROM users;
Community
  • 1
  • 1
Progrock
  • 6,765
  • 1
  • 16
  • 25