循环表联系人并创建多个文件


PHP Looping table contacts and creating multiple files

我有一些代码,我访问数据库,并从一个名为snippets的表中获取所有数据。

一切都很好,除了我现在需要创建一个新的html文件,并保存它与一个名为code的字段的内容。

所以基本上每个"code"字段都被保存为一个新的html文件。

下面是当前代码:

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT code FROM snippets";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        //save each code record as a new html page
    }
} else {
    echo "0 results";
}
$conn->close();

我该怎么做呢?

试试这个…

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        // if you have id field in your record then try this way
        file_put_contents($row['id'].".html", $row); // 1 full path with name 2 your data
    }
} else {
    echo "0 results";
}