简体   繁体   中英

I want fetch MySQLi table info into an HTML table using PHP, but mysqli_fetch_array query returns null

the title explains everything. I also tried using mysqli_fetch_assoc but that returns null also! connection to database isn't the problem since it's done and the "user" have all the permission granted. Please take a look at code and help me out if you can...

<?php
    require 'connect.php';
    $sql = mysqli_query("select * from 'info' order by id desc");
    ?>
    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>List</title>
        <link rel="stylesheet" href="member_list.css">
    </head>
    <body>

    <h1>Users</h1>
    <table class="rwd-table">
        <tr>
            <th>Number</th>
            <th>Name</th>
            <th>Family</th>
        </tr>
        <?php
            while($row = mysqli_fetch_assoc($sql)):
            $i= 1;
            ?>
        <tr>
            <td data-th="Number"><?php echo $i; ?></td>
            <td data-th="Name"><?php echo $row['name']; ?></td>
            <td data-th="Family"><?php echo $row['family']; ?></td>
        </tr>
        <?php
        $i++;
        endwhile; ?>
    </table>
    </body>
    </html>

And here is the connect file:

<?php
/**
 * Created by PhpStorm.
 * User: reza
 * Date: 2019-02-22
 * Time: 18:57
 */

session_start();
ob_start();

$host = "localhost";
$username = "db2u";
$password = "1234";
$database = "db2";

$conn = mysqli_connect("$host" , "$username" , "$password" , "$database");

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

Pass $conn in mysqli_query and also table name should be in backtick (`), key with tild key on keyboard, not in single quote.

$sql = mysqli_query($conn, "select * from `info` order by id desc");

Hope it helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM