Hi

videompz.rf.gd
When I run this code for excuting Data from database nothing showing only

<?php // Database connection setup (replace with your actual credentials) $hostname = "sql3"**.infinityfree.com"; $username = "if0_*****"; $password = "*******"; $database = "if0_34751184_mydatabase1"; // Create a database connection $db = new mysqli($hostname, $username, $password, $database); // Check if the connection was successful if ($db->connect_error) { die('Database connection failed: ' . $db->connect_error); } // Create a folder to store news articles if it doesn't exist $folderPath = '../web/page'; if (!file_exists($folderPath)) { mkdir($folderPath, 0777, true); // Create the folder recursively with full permissions } // Define a SQL query to retrieve news data $sql = "SELECT id, title, content FROM pages"; // Execute the query $result = $db->query($sql); // Check if the query was successful if (!$result) { die('Error in SQL query: ' . $db->error); } // Fetch news data into an array $newsData = array(); while ($row = $result->fetch_assoc()) { $newsData[] = $row; // Store each news article as a file $fileName = $folderPath . '/' . $row['id'] . '.html'; // You can use different file extensions if needed file_put_contents($fileName, $row['content']); } // Close the database connection $db->close(); ?> <!DOCTYPE html> <html> <head> <title>News Portal</title> <!-- Favicon --> <link rel="icon" href="favicon.ico" type="image/x-icon" /> </head> <body> <!-- Your HTML code here --> <div class="container"> <h2 style="color: #333;">Generated News Pages:</h2> <ul id="itemList" style="list-style: none; padding: 0;"> <?php // Loop through the retrieved news data and generate news items foreach ($newsData as $newsItem) { $pagePath = "../web/page" . $newsItem['id'] . '.html'; // Adjust the path and file extension as needed $pageTitle = $newsItem['title']; // Output each news item echo '<li>'; echo '<a href="' . $pagePath . '" style="text-decoration: none; color: #007BFF; transition: color 0.3s;">' . $pageTitle . '</a>'; echo '</li>'; } ?> </ul> </div> <!-- Your JavaScript code here --> </body> </html>

Hello

Next time you paste code, please format it. Viewing code in a single line for something this long is just not possible.

Please enable error messages on your site and reload it.

7 Likes

Hi Videosmpz,

Assuming everything else other than the code works properly, the code has a missing case where the result set is basically empty and the code lets it through.

<?php
// Database connection setup (replace with your actual credentials)
$hostname = "sql3**.infinityfree.com";
$username = "if0_*****";
$password = "*******";
$database = "if0_34751184_mydatabase1";

// Create a database connection
$db = new mysqli($hostname, $username, $password, $database);

// Check if the connection was successful
if ($db->connect_error) {
	die('Database connection failed: ' . $db->connect_error);
}
// Create a folder to store news articles if it doesn't exist
$folderPath = '../web/page';
if (!file_exists($folderPath)) {
	mkdir($folderPath, 0777, true);

	// Create the folder recursively with full permissions
}
// Define a SQL query to retrieve news data
$sql = "SELECT id, title, content FROM pages";

// Execute the query
$result = $db->query($sql);

// Check if the query was successful
if (!$result) {
	die('Error in SQL query: ' . $db->error);
}
// Fetch news data into an array
$newsData = array();
while (
	$row = $result->fetch_assoc()
) {
	$newsData[] = $row;

	// Store each news article as a file
	$fileName = $folderPath . '/' . $row['id'] . '.html';

	// You can use different file extensions if needed
	file_put_contents($fileName, $row['content']);
}
// Close the database connection
$db->close();

The line if (!$result) { would allow [] to pass as an empty array is essentially true, and the die statement won’t be triggered.

However since there’s nothing and the while condition only runs if there’s something to loop on, the whole while loop has no effect if the $result is empty or has nothing to loop, i.e. [].

And the code ended without outputting anything.

Next time please format your code.

In case you have more info or error messages, then we can check further based on that.

5 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.