Skip to content

Instantly share code, notes, and snippets.

<?php // best practice: always have <?php at the top
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="account"; // Database name
// Connect to server and select databse.
$db = new mysqli($host, $username, $password, $db_name);
<?php // best practice: always have <?php at the top
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="account"; // Database name
$tbl_name="member"; // Table name
// Connect to server and select databse.
mysql_connect('localhost', 'root', 'root')or die("cannot connect");
<?php
if ($x > 5) {
$y = "1";
} else {
$y = false;
}
if ($y) {
echo "2";
<?php
sleep(10);
$fp = fopen("finished requests.txt", 'a+');
fwrite($fp, "at " . date("H:i:s") . "\n");
fclose($fp);
@dennisdegryse
dennisdegryse / gist:286adf5aac49f1d6b066
Last active August 29, 2015 14:13
Operator precedence
<?php
$x = 10;
echo $x > 5 ? "1" : false ? "2" : "3";
// ================================ => = echo construct expression
// + + => o1 = ternary operator at root
// -------------------- => o1.x1 = first operand of o1
// + + => o1.x1.o1 = ternary operator at root of o1.x1
// ------ => o1.x1.o1.x1 = first operand of o1.x1.o1
// + => o1.x1.o1.x1.o1 = binary operator at root of o1.x1.o1.x1
// == => o1.x1.o1.x1.o1.x1 = first operand of o1.x1.o1.x1.o1 = 10
<?php
class Item extends Data {
public function getSelectCategory($category_name) {
$category_name = "%$category_name%";
$SQL = "SELECT category_id, category_name
FROM category
@dennisdegryse
dennisdegryse / gist:c24a78a21b3b81f1f3e6
Created January 18, 2015 18:25
Select ? random records
SELECT title
FROM table AS t1
JOIN (SELECT (RAND() * (SELECT MAX(id) FROM table)) AS id) AS t2
WHERE t1.id >= t2.id
ORDER BY t1.id ASC
LIMIT ?;
@dennisdegryse
dennisdegryse / gist:9db5b34acb7efc8c8a69
Created January 18, 2015 16:09
HTTP Response sink (using JQuery)
function ajax(url, settings) {
var completeHandler = undefined;
if (settings.complete) {
completeHandler = settings.complete;
}
settings['complete'] = function(data, category) {
ajaxResponseTrap(data, category);
@dennisdegryse
dennisdegryse / imagescalecrop.php
Last active August 29, 2015 14:13
Scaling and cropping an image to the specified size using PHP GD
<?php
/**
* Loads an image into a GD resource and scales/crops it to the given target size.
*
* @param string $filename The file name of the image.
* @param string $mime The mime type of the image.
* @param string $target_size The target size.
*
* @throws Exception When the image MimeType is invalid.
*
@dennisdegryse
dennisdegryse / imagedata.php
Created January 15, 2015 12:09
Getting image data from GD safely
<?php
/**
* Gets the image data from a GD image resource.
*
* @param resource $image The image resource.
* @param string $format The image format.
*
* @throws Exception When an illegal format was specified.
*