Needed to make a simple php script with mysql in it, so I decided to use PDO because… well peer pressure 😐 so heres a basic PHP PDO MYsql script. The idea is I have a table called table with a field called field in a database called somedatabase. We have 1000 rows of data in said table and we need to pick a random result from the 1000 rows. This is just 1 way of doing it, it isnt the most optimized, neither is it the cleanest. but this will give you a basic idea of how to connect to the DB and get a result. you can expand this of course to do very tricky queries.
// Add a user variable for PDO, mysql user
$user='root';
// Add a password for PDO, mysql password
$password='password';
// Setup the mysql connection info
$dsn = 'mysql:dbname=somedatabase;host=127.0.0.1';
// Wrap the actual connection to mysql in try so if it doesn't work you can catch the exception and echo the message.
try {
$dbh = new PDO($dsn, $user, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
// Pick a number from 1 - 1000 and assign to $rc
$rc = mt_rand(1, 1000);
// Assign the SQL query selecting the data from our DB / field to variable $sql
$sql = "SELECT field FROM table LIMIT 1 OFFSET $rc";
// run the query and for each one assign to variable $field
foreach ($dbh->query($sql) as $row) {
$field = $row['field'];
}
// clear the variable $dbh and return used memory back to usable pool
$dbh = null;
I want to write more articles concerning the trio of PHP PDO Mysql but I don’t do much with PHP anymore so the chance that I have need of the three at this point is slim.
In this guide, I share a step-by-step method for early adopters to experience Ubuntu 24.04 LTS before its official release. I emphasize caution and provide clear instructions to minimize risks, helping my tech-savvy audience stay ahead of the curve while ensuring a smooth upgrade process.
In Docker, running multiple commands within a container typically requires separate docker exec invocations. However, you can streamline this process using a shell script with a here-document. This technique involves piping a sequence of commands directly into a single docker exec session, significantly enhancing efficiency and reducing complexity. It’s particularly beneficial for tasks requiring sequential execution, making it an ideal choice for automation and deployment workflows in Docker environments.
Learn how to leverage the power of FFMPEG to craft visually captivating image slideshows with accompanying audio tracks. This step-by-step guide demonstrates the command-line usage of FFMPEG to combine a still image and audio file, producing an output video in MP4 format. By carefully adjusting parameters such as frame rate, video codec, preset, and more, you can achieve high-quality results. Whether you’re a content creator, photographer, or simply want to enhance your presentations, this article provides the knowledge and tools to create stunning image slideshows using FFMPEG.