Skip to content

Instantly share code, notes, and snippets.

View Petah's full-sized avatar

David Neilsen Petah

  • Hamilton, New Zealand
View GitHub Profile
@Petah
Petah / mysqli-poll.php
Last active August 5, 2021 21:59
MySQL Poll
<?php
$queryConnection = new \mysqli($host, $username, $password, $database);
$killConnection = new \mysqli($host, $username, $password, $database);
// Execute the query
$queryConnection->query($sql, MYSQLI_ASYNC | MYSQLI_USE_RESULT);
// Poll for results
$start = microtime(true);
do {
@Petah
Petah / prime.php
Created July 7, 2021 03:39
Prime drag race
<?php
$passes = 0; //Init passes
$sieveSize = 1000000; //Set sieve size
$runTime = 10; //The amount of seconds the script should be running for
$stopTime = microtime(true) + $runTime;
$q = sqrt($sieveSize);
while (microtime(true) < $stopTime) {
$factor = 3;
$rawbits = [];
<?php
use PHPUnit\Framework\TestListener;
class OutputListener implements TestListener
{
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
{
echo PHP_EOL;
echo PHP_EOL . 'ERROR: ' . get_class($test) . ' ' . $test->getName();
echo PHP_EOL;
const chromium = require('chrome-aws-lambda');
const puppeteer = require('puppeteer-core');
const crypto = require('crypto');
const AWS = require('aws-sdk');
const hashResult = (data) => {
const hashDigest = crypto.createHash('md5');
hashDigest.update(data);
return hashDigest.digest('hex');
};
@Petah
Petah / database.php
Created June 3, 2018 12:21
basic php
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'dbuser', 'dbpassword');
@Petah
Petah / DbMigrate.php
Created July 4, 2016 07:56
Simple Migrate
<?php
namespace MyProject\Cli\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class DbMigrate extends \Symfony\Component\Console\Command\Command
{
<?php
use Ramsey\Uuid\Uuid;
/**
* Helper class for dealing with input data (e.g. data sent over HTTP, API responses,
* deserialized data from storage etc). When accessing items this class automatically
* checks that it exists and casts the value to the expected type. If it does not exist a default value is returned.
*/
class InputData implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonSerializable
{
@Petah
Petah / find-passwords.php
Last active February 4, 2016 09:38
find-passwords.php
<?php
$url = 'http://datatables.yajrabox.com/fluent/basic-object-data?draw=1&columns%5B0%5D%5Bdata%5D=id&columns%5B0%5D%5Bname%5D=id&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=name&columns%5B1%5D%5Bname%5D=name&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=true&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=email&columns%5B2%5D%5Bname%5D=email&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=true&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=created_at&columns%5B3%5D%5Bname%5D=created_at&columns%5B3%5D%5Bsearchable%5D=true&columns%5B3%5D%5Borderable%5D=true&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B4%5D%5Bdata%5D=updated_at&columns%5B4%5D%5Bname%5D=updated_at&columns%5
@Petah
Petah / tag.php
Last active December 16, 2015 02:54
<?php
class Tag {
private $name;
private $attributes = [];
private $content = [];
public function __construct($name) {
$this->name = $name;
}
<?php
$errors = [
'name' => null,
'email' => null,
'message' => null,
];
if (!empty($_POST)) {
if (!isset($_POST['name'])) {
$errors['name'] = 'Your name is required';
} elseif (strlen($_POST['name']) > 100) {