Tag: PHP

Fetch data from Google Sheet and show in HTML using PHP

 This documentation explains how to use the provided PHP code to fetch data from Google Sheets and display it in a custom layout using HTML/CSS. Setup Instructions 1. Get Your Google Sheet ID Open your Google Sheet in a web browser Look at the URL in the address bar – it will look like: https://docs.google.com/spreadsheets/d/BmzaSyBzQ0cRTrbf_vxrB75nh8AoV3BtawPiiCQ/edit#gid=0 […]

Read More

Block HTtrack or any other web scrapper

 There are several web scrappers tools are available by which your website can be downloaded as static files. The simplest way to block these web scrappers is to add the following blocking bots code in your website .htaccess file. ##Block bad bots RewriteEngine On RewriteCond %{HTTP_USER_AGENT} ^BlackWidow [OR] RewriteCond %{HTTP_USER_AGENT} ^Bot mailto:[email protected] [OR] RewriteCond %{HTTP_USER_AGENT} […]

Read More

PHP script to download file from specific folder

 Below is a simple example of a PHP script that allows you to download file from a specific folder. You can link to this PHP file with the file name as a parameter. Create a file named download.php and add the following code: <?php // Specify the folder where your files are stored $folderPath = […]

Read More

How to upload image and PDF file using PHP

 Here is a PHP script that allows users to upload image and PDF files with a maximum size of 5 MB. The uploaded files will be renamed using the current timestamp: <?php if ($_SERVER[“REQUEST_METHOD”] == “POST” && isset($_FILES[“file”])) { $allowedExtensions = array(“jpg”, “jpeg”, “png”, “pdf”); $maxFileSize = 5 * 1024 * 1024; // 5 MB […]

Read More

Get total of MySQL column and show highest totals

 To get the total of a MySQL field and then select the three highest totals from the table, you can use the following PHP script: <?php // Assuming you have already established a MySQL connection // Retrieve the total of a field and select the three highest totals $servername = “localhost”; $username = “your_username”; $password […]

Read More

Export MySQL data into CSV using PHP

 To fetch data from a MySQL database and export it to CSV using PHP, you can follow these steps: <?php // Database connection details $servername = “localhost”; $username = “your_username”; $password = “your_password”; $dbname = “your_database_name”; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die(“Connection failed: ” […]

Read More

Print total of a variable inside while loop outside of the loop

 To accumulate values from a MySQL query inside a while loop and then calculate the total value outside the loop, you can use a variable to store the total value while iterating through the records. Here’s an example in PHP: <?php // Your database connection parameters $servername = “localhost”; $username = “your_username”; $password = “your_password”; […]

Read More

Import CSV data into MySQL using PHP

 Here’s a detailed example of how to import data from a CSV file into a MySQL database using PHP. The script processes each row one by one, displays a success message for each successfully inserted row, and stops the process if any error occurs, showing the error message. Prerequisites: Ensure you have a MySQL database […]

Read More

PHP function to get difference between dates

Below is a PHP function that calculates and displays the difference in days between two dates. This function will take two date strings as inputs, convert them to DateTime objects, and then calculate the difference in days. Here’s how you can define and use the function: Explanation: Creating DateTime Objects: new DateTime($date1): This converts the […]

Read More