i want to refresh images on index.php while all images selecting from database kndly help me tell me whats the solution

Dani AI

Generated

Brief, practical guidance for (and answering 's request for an example): choose a random image server-side and output its URL (or serve the blob) on each request, and handle browser caching so a refresh actually fetches a different image.

A simple, scalable server-side pattern (preferable to ORDER BY RAND() on large tables): count rows, pick a random offset, then select that row. Example for index.php (PDO):

<?php
$pdo = new PDO('mysql:host=localhost;dbname=dbname','user','pass',[
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

$count = (int)$pdo->query("SELECT COUNT(*) FROM images")->fetchColumn();
if ($count === 0) exit;

$offset = rand(0, $count - 1);
$sql = "SELECT id, filename FROM images LIMIT 1 OFFSET $offset";
$row = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);

// serve file on disk:
echo '<img src="uploads/' . htmlspecialchars($row['filename'], ENT_QUOTES) . '" alt="">';

// or serve blob through a script and add a cache-busting query:
echo '<img src="serveImage.php?id=' . (int)$row['id'] . '&r=' . time() . '" alt="">';
?>

If images are stored as BLOBs, use a small serveImage.php that sets the correct Content-Type and cache headers:

<?php
// assume $pdo created
$id = (int)$_GET['id'];
$stmt = $pdo->prepare("SELECT mime, data FROM images WHERE id = ?");
$stmt->execute([$id]);
if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  header('Content-Type: ' . $row['mime']);
  header('Cache-Control: no-cache, no-store, must-revalidate');
  header('Pragma: no-cache');
  header('Expires: 0');
  echo $row['data'];
}
?>

Notes and troubleshooting: avoid ORDER BY RAND() on big tables; if IDs have gaps, the COUNT+OFFSET method is safer than random id arithmetic; add a query string (eg. ?r=time()) or no-cache headers to force the browser to fetch a new image; validate filenames/MIME and use prepared statements to prevent injection; for frequent loads, consider caching or precomputing a small set of random IDs to reduce DB work.

Recommended Answers

All 2 Replies

Can you give an example? I'm not quite sure what you mean.

like goole adsense policy add is change on refresh
i want to make same thing where all images is getting from database but i want that image is change on refresh

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.