如何通过表查看数据库中的行? [重复]

I just want to make a table that let me view the list of rows on my database... But my code won't work, here is my whole code:

$con=mysqli_connect("fd***.biz.nf","1549087_admin","*****","******");
$sql = "SELECT * FROM Solo;";
$myData = mysqli_query($con,$sql);

echo "<table border='1'>
<tr>
<th>Grade/Yr. Level</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Position</th>
<th>Motto</th>
</tr>";

while($row = mysqli_fetch_array($myData, MYSQLI_ASSOC))
{
echo "<tr><td>";
echo $row['gradeyrlevel'];
echo "</tr><td>";
echo $row['firstname'];
echo "</tr><td>";
echo $row['middlename'];
echo "</tr><td>";
echo $row['lastname'];
echo "</tr><td>";
echo $row['age'];
echo "</tr><td>";
echo $row['position'];
echo "</tr><td>";
echo $row['motto'];
echo "</tr><td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);

Please help me.

the error is:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /srv/disk10/1549087/www/rooseveltcollegecainta.co.nf/admin/adminpage/index.php on line 125

line 125 is

while($row = mysqli_fetch_array($myData, MYSQLI_ASSOC))
</div>

While Using mysqli you need to pass the connection variable try using

**$result=mysqli_query($con,$query);**

instead of $result=mysqli_query($query); hope this works

Try this:

$mysqli = new mysqli(HOST,USER,PASS,DATABASE);
if (mysqli_connect_errno()) {
  printf("Connect failed: %s
", mysqli_connect_error());
  exit();
}
$query  = "SELECT * FROM Solo";
$result = $mysqli->query($query);
while($row    = $result->fetch_array(MYSQLI_ASSOC))
{
     echo "<tr>";
  echo "<td>" . $row['gradeyrlevel'] . "</td>";
  echo "<td>" . $row['firstname'] . "</td>";
  echo "<td>" . $row['middlename'] . "</td>";
  echo "<td>" . $row['lastname'] . "</td>";
  echo "<td>" . $row['age'] . "</td>";
   echo "<td>" . $row['position'] . "</td>";
 echo "<td>" . $row['motto'] . "</td>";
 echo "</tr>";
}
    try this............ 
    $con=mysql_connect("host","username","password");
    mysql_select_db('dbname');
    $query="SELECT * FROM SOLO";
    $result=mysql_query($query);

    while($data = mysql_fetch_row($result))
    {
        .
        .
        .
        .
        .

   }

The error means the variable $result is set to false instead of the result of a query. Check if the table name you supplied is correct and does the database user you used to log in has access to it.

You have put a semicolon inside mysqli query so you found this error.you have to use following code

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

As per your other question here, you obviously have your table name wrong (word/letter-case).

In that question, and I quote from what you said: "IT WORKED!! THANK YOU SO MUCH!! :)"

(LINK to comment)

Two possible errors.

1) Mispelled table name. SOLO instead of Solo (which is most likely the case).

2) Extra semi-colon => $sql = "SELECT * FROM Solo;";
Replace with => $sql = "SELECT * FROM Solo";

And if that doesn't work, then double-check the letter case of everything, such as:

  • Table name
  • Column names
  • Including your DB connection
  • Make sure that the columns exist and are named correctly
  • Check Everything

N.B.: Solo is not the same as SOLO or solo


Try this now, but do read the footnotes below, as they contain some important information that will prove to be beneficial in your learning SQL and PHP.

$con=mysqli_connect("fd***.biz.nf","1549087_admin","*****","******");

// Check connection
if (mysqli_connect_errno($con))
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql = "SELECT * FROM `SOLO`"; // added backticks around SOLO table name

if($sql === FALSE) {
die(mysqli_error()); // TODO: better error handling
}

$myData = mysqli_query($con,$sql);

echo "<table border='1'>
<tr>
<th>Grade/Yr. Level</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Position</th>
<th>Motto</th>
</tr>";

while($row = mysqli_fetch_array($myData, MYSQLI_ASSOC))
{
echo "<tr><td>";
echo $row['gradeyrlevel'];
echo "</tr><td>";
echo $row['firstname'];
echo "</tr><td>";
echo $row['middlename'];
echo "</tr><td>";
echo $row['lastname'];
echo "</tr><td>";
echo $row['age'];
echo "</tr><td>";
echo $row['position'];
echo "</tr><td>";
echo $row['motto'];
echo "</tr><td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);

Footnotes:

Suggested tutorials: (although there are many others that can be found on the Web)

and of course, the PHP and MySQL manuals: