Calling a Stored Procedure from PHP

<?php
$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "learnsql";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "CALL GetAllProducts()";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<pre>";
        print_r($row['productLine']);
        echo "</pre>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>
Kết quả:
Motorcycles
Classic Cars
Motorcycles
Motorcycles
Classic Cars
Classic Cars
Classic Cars
Classic Cars
Trucks and Buses
Motorcycles
Classic Cars
Classic Cars
Classic Cars
Classic Cars
Trucks and Buses
Classic Cars
Trucks and Buses
Classic Cars
Vintage Cars
Vintage Cars
Classic Cars
Planes
Vintage Cars
Classic Cars
Classic Cars
Classic Cars
Vintage Cars

Xem cách tạo hàm GetAllProducts() bài trước ở đây :))) https://learnsql.gitbook.io/lernmysql/welcome-to-the-mysql-tutorial/getting-started-with-mysql-stored-procedures

Link tham khảo:
https://www.sitepoint.com/stored-procedures-mysql-php/
https://viblo.asia/p/pdo-trong-php-khai-niem-va-nhung-thao-tac-co-ban-57rVRq59R4bP

Last updated