Php lấy dữ liệu từ mysql và hiển thị trong bảng

Có một vài phương pháp sử dụng mà bạn có thể sử dụng lấy dữ liệu từ cơ sở dữ liệu trong PHP và hiển thị nó vào bảng HTML. Trong hướng dẫn này, chúng tôi đã giải thích phương pháp sau để lấy dữ liệu từ cơ sở dữ liệu trong PHP và hiển thị nó trong bảng HTML

  • Chương trình cơ bản sử dụng Loop
  • Sử dụng Ajax
  • Sử dụng bảng dữ liệu

1] Chương trình cơ bản sử dụng Loop

 0]{
    }else{
        $msg = "No Record found";
    }
}
?>




    
    
    
    HTML and PHP code


    Display user list using HTML and PHP
    
    
        
            
                Name
                Email
                Status
                Registrating Date
            
        
        
            
                
                    
                    
                    
                    
                
            
        
    


đầu ra

2] Sử dụng Ajax

Mã tệp HTML




    
    
    
    HTML and PHP code


    Display user list using HTML and PHP
    
    
        
            
                Name
                Email
                Registrating Date
            
        
        
            
        
    





Mã tệp PHP

 0]{
        $result_array = array[];
        while[$row = mysqli_fetch_assoc[$result]]{
            array_push[$result_array, $row];
        }

    }

    echo json_encode[$result_array];

}
?>

đầu ra

3] Sử dụng Bảng dữ liệu

Mã tệp HTML





    
    
    
    HTML and PHP code
    



    Display user list using HTML and PHP
    
    
        
            
                Name
                Email
                Registrating Date
            
        
        

        
    








Mã tệp PHP

 0]{
        $result_array = array[];
        while[$row = mysqli_fetch_assoc[$result]]{
            array_push[$result_array, $row];
        }

    }

    $results = ["sEcho" => 1,
        	"iTotalRecords" => count[$result_array],
        	"iTotalDisplayRecords" => count[$result_array],
        	"aaData" => $result_array ];

    echo json_encode[$results];

}
?>

đầu ra

Đến đây bạn đã học cách tạo cơ sở dữ liệu và bảng cũng như chèn dữ liệu. Bây giờ là lúc lấy dữ liệu đã chèn trong hướng dẫn trước. Câu lệnh SQL

 0]{
        $result_array = array[];
        while[$row = mysqli_fetch_assoc[$result]]{
            array_push[$result_array, $row];
        }

    }

    echo json_encode[$result_array];

}
?>
1 được sử dụng để chọn các bản ghi từ các bảng cơ sở dữ liệu. Cú pháp cơ bản của nó như sau

CHỌN cột1_name, cột2_name, cộtN_name TỪ tên_bảng;

Hãy tạo một truy vấn SQL bằng cách sử dụng câu lệnh

 0]{
        $result_array = array[];
        while[$row = mysqli_fetch_assoc[$result]]{
            array_push[$result_array, $row];
        }

    }

    echo json_encode[$result_array];

}
?>
1, sau đó chúng ta sẽ thực hiện truy vấn SQL này thông qua việc chuyển nó tới hàm PHP
 0]{
        $result_array = array[];
        while[$row = mysqli_fetch_assoc[$result]]{
            array_push[$result_array, $row];
        }

    }

    echo json_encode[$result_array];

}
?>
3 để lấy dữ liệu bảng

Xem xét bảng cơ sở dữ liệu người của chúng tôi có các bản ghi sau

+----+------------+-----------+----------------------+
| id | first_name | last_name | email                |
+----+------------+-----------+----------------------+
|  1 | Peter      | Parker    | peterparker@mail.com |
|  2 | John       | Rambo     | johnrambo@mail.com   |
|  3 | Clark      | Kent      | clarkkent@mail.com   |
|  4 | John       | Carter    | johncarter@mail.com  |
|  5 | Harry      | Potter    | harrypotter@mail.com |
+----+------------+-----------+----------------------+

Mã PHP trong ví dụ sau chọn tất cả dữ liệu được lưu trữ trong bảng người [sử dụng ký tự dấu hoa thị [

 0]{
        $result_array = array[];
        while[$row = mysqli_fetch_assoc[$result]]{
            array_push[$result_array, $row];
        }

    }

    echo json_encode[$result_array];

}
?>
4] thay cho tên cột sẽ chọn tất cả dữ liệu trong bảng]

Thí dụ

PDO hướng đối tượng thủ tục

Tải xuống

 0]{
        echo "";
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        while[$row = mysqli_fetch_array[$result]]{
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        }
        echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
        // Free result set
        mysqli_free_result[$result];
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
 
// Close connection
mysqli_close[$link];
?>
connect_error];
}
 
// Attempt select query execution
$sql = "SELECT * FROM persons";
if[$result = $mysqli->query[$sql]]{
    if[$result->num_rows > 0]{
        echo "";
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        while[$row = $result->fetch_array[]]{
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        }
        echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
        // Free result set
        $result->free[];
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
 
// Close connection
$mysqli->close[];
?>
setAttribute[PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION];
} catch[PDOException $e]{
    die["ERROR: Could not connect. " . $e->getMessage[]];
}
 
// Attempt select query execution
try{
    $sql = "SELECT * FROM persons";   
    $result = $pdo->query[$sql];
    if[$result->rowCount[] > 0]{
        echo "";
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        while[$row = $result->fetch[]]{
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        }
        echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
        // Free result set
        unset[$result];
    } else{
        echo "No records matching your query were found.";
    }
} catch[PDOException $e]{
    die["ERROR: Could not able to execute $sql. " . $e->getMessage[]];
}
 
// Close connection
unset[$pdo];
?>

Giải thích Mã [Kiểu thủ tục]

Trong ví dụ trên, dữ liệu trả về bởi hàm

 0]{
        $result_array = array[];
        while[$row = mysqli_fetch_assoc[$result]]{
            array_push[$result_array, $row];
        }

    }

    echo json_encode[$result_array];

}
?>
3 được lưu trữ trong biến
 0]{
        $result_array = array[];
        while[$row = mysqli_fetch_assoc[$result]]{
            array_push[$result_array, $row];
        }

    }

    echo json_encode[$result_array];

}
?>
6. Mỗi khi gọi
 0]{
        echo "";
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        while[$row = mysqli_fetch_array[$result]]{
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        }
        echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
        // Free result set
        mysqli_free_result[$result];
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
 
// Close connection
mysqli_close[$link];
?>
0, nó sẽ trả về hàng tiếp theo từ tập kết quả dưới dạng một mảng. Vòng lặp while được sử dụng để lặp qua tất cả các hàng trong tập kết quả. Cuối cùng, giá trị của trường riêng lẻ có thể được truy cập từ hàng bằng cách chuyển chỉ mục trường hoặc tên trường đến biến
 0]{
        echo "";
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        while[$row = mysqli_fetch_array[$result]]{
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        }
        echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
        // Free result set
        mysqli_free_result[$result];
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
 
// Close connection
mysqli_close[$link];
?>
1 như
 0]{
        echo "";
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        while[$row = mysqli_fetch_array[$result]]{
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        }
        echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
        // Free result set
        mysqli_free_result[$result];
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
 
// Close connection
mysqli_close[$link];
?>
2 hoặc
 0]{
        echo "";
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        while[$row = mysqli_fetch_array[$result]]{
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        }
        echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
        // Free result set
        mysqli_free_result[$result];
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
 
// Close connection
mysqli_close[$link];
?>
3,
 0]{
        echo "";
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        while[$row = mysqli_fetch_array[$result]]{
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        }
        echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
        // Free result set
        mysqli_free_result[$result];
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
 
// Close connection
mysqli_close[$link];
?>
4 hoặc
 0]{
        echo "";
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        while[$row = mysqli_fetch_array[$result]]{
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        }
        echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
        // Free result set
        mysqli_free_result[$result];
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
 
// Close connection
mysqli_close[$link];
?>
5,
 0]{
        echo "";
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        while[$row = mysqli_fetch_array[$result]]{
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        }
        echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
        // Free result set
        mysqli_free_result[$result];
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
 
// Close connection
mysqli_close[$link];
?>
6 hoặc
 0]{
        echo "";
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        while[$row = mysqli_fetch_array[$result]]{
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        }
        echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
        // Free result set
        mysqli_free_result[$result];
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
 
// Close connection
mysqli_close[$link];
?>
7 và
 0]{
        echo "";
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        while[$row = mysqli_fetch_array[$result]]{
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        }
        echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
        // Free result set
        mysqli_free_result[$result];
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
 
// Close connection
mysqli_close[$link];
?>
8 hoặc
 0]{
        echo "";
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        while[$row = mysqli_fetch_array[$result]]{
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        }
        echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
        // Free result set
        mysqli_free_result[$result];
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
 
// Close connection
mysqli_close[$link];
?>
9

Nếu bạn muốn sử dụng vòng lặp

connect_error];
}
 
// Attempt select query execution
$sql = "SELECT * FROM persons";
if[$result = $mysqli->query[$sql]]{
    if[$result->num_rows > 0]{
        echo "";
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        while[$row = $result->fetch_array[]]{
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        }
        echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
        // Free result set
        $result->free[];
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
 
// Close connection
$mysqli->close[];
?>
0, bạn có thể lấy giá trị bộ đếm vòng lặp hoặc số hàng mà truy vấn trả về bằng cách chuyển biến
 0]{
        $result_array = array[];
        while[$row = mysqli_fetch_assoc[$result]]{
            array_push[$result_array, $row];
        }

    }

    echo json_encode[$result_array];

}
?>
6 cho hàm
connect_error];
}
 
// Attempt select query execution
$sql = "SELECT * FROM persons";
if[$result = $mysqli->query[$sql]]{
    if[$result->num_rows > 0]{
        echo "";
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        while[$row = $result->fetch_array[]]{
            echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            echo "";
        }
        echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
        // Free result set
        $result->free[];
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
 
// Close connection
$mysqli->close[];
?>
2. Giá trị bộ đếm vòng lặp này xác định số lần vòng lặp sẽ chạy

Làm cách nào để lấy dữ liệu từ cơ sở dữ liệu trong PHP và hiển thị trong bảng?

Truy xuất hoặc tìm nạp dữ liệu từ cơ sở dữ liệu trong PHP .
CHỌN [các] tên_cột TỪ_tên_bảng
$query = mysql_query["select * from tablename", $connection];
$connection = mysql_connect["localhost", "root", ""];
$db = mysql_select_db["công ty", kết nối $];
$query = mysql_query["chọn * từ nhân viên", $connection];

Làm cách nào để tìm nạp dữ liệu vào bảng từ cơ sở dữ liệu MySQL trong PHP?

Dữ liệu có thể được tìm nạp từ các bảng MySQL bằng cách thực thi câu lệnh SQL SELECT thông qua hàm mysql_query của PHP . Bạn có một số tùy chọn để lấy dữ liệu từ MySQL. Tùy chọn được sử dụng thường xuyên nhất là sử dụng hàm mysql_fetch_array[]. Hàm này trả về hàng dưới dạng một mảng kết hợp, một mảng số hoặc cả hai.

Làm cách nào chúng ta có thể lấy dữ liệu từ cơ sở dữ liệu và hiển thị trong bảng HTML?

Sử dụng các bước sau để tìm nạp/truy xuất dữ liệu từ cơ sở dữ liệu trong php và hiển thị trong bảng html. .
Bước 1 – Khởi động máy chủ web Apache
Bước 2 – Tạo dự án PHP
Bước 3 – Thực thi truy vấn SQL để tạo bảng
Bước 4 – Tạo tệp kết nối cơ sở dữ liệu MySQL phpmyadmin
Bước 5 – Tạo tệp PHP tìm nạp dữ liệu từ cơ sở dữ liệu

Làm cách nào chúng tôi có thể truy xuất dữ liệu từ các bảng trong MySQL?

Cách truy xuất dữ liệu từ một bảng duy nhất .
CHỌN – các cột trong tập kết quả
TỪ – đặt tên cho [các] bảng cơ sở mà kết quả sẽ được truy xuất từ ​​đó
WHERE – chỉ định bất kỳ điều kiện nào cho tập kết quả [bộ lọc]
ORDER BY – đặt cách sắp xếp tập kết quả
GIỚI HẠN – đặt số lượng hàng được trả về

Chủ Đề