Skip to content

Instantly share code, notes, and snippets.

@subrotoice
Last active August 23, 2022 09:32
Show Gist options
  • Save subrotoice/e8045f06cf2b11fa40fbbae9b849472c to your computer and use it in GitHub Desktop.
Save subrotoice/e8045f06cf2b11fa40fbbae9b849472c to your computer and use it in GitHub Desktop.
<?php
//Example array.
$array = array('Ireland', 'England', 'Wales', 'Northern Ireland', 'Scotland');
//Encode the array into a JSON string.
$encodedString = json_encode($array);
//Save the JSON string to a text file.
file_put_contents('myfile.txt', $encodedString);
//Retrieve the data from our text file.
$fileContents = file_get_contents('myfile.txt');
//Convert the JSON string back into an array.
$decoded = json_decode($fileContents, true);
//The end result.
var_dump($decoded);
# Simplified---------------------------------------------------
//Save the JSON string to a text file.
$time = date("H:i:s A, d M Y", time());
file_put_contents('myfile.txt', $time);
//Retrieve the data from our text file.
$fileContents = file_get_contents('myfile.txt');
//The end result.
var_dump($fileContents);
# Simplified using DB
$conn = mysqli_connect("localhost", "root", "", "test");
// Check connection
if($conn === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt insert query execution
$time = date("H:i:s A, d M Y", time());
$sql = "INSERT INTO student (time) VALUES ('$time')";
if(mysqli_query($conn, $sql)){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
// Close connection
mysqli_close($conn);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment