Skip to content

Instantly share code, notes, and snippets.

@jhyland87
Created June 29, 2016 16:03
Show Gist options
  • Save jhyland87/3de37904e8294aecf1c90ce4bae45498 to your computer and use it in GitHub Desktop.
Save jhyland87/3de37904e8294aecf1c90ce4bae45498 to your computer and use it in GitHub Desktop.
<?php
$set_var = <<<EOF
SET @ship_id = (SELECT ship_id FROM orders WHERE ship_id IS NOT NULL ORDER BY ship_id LIMIT 1);
EOF;
$use_var = <<<EOF
SELECT
id,
user_id,
COALESCE(ship_id, @ship_id) AS ship_id
FROM
orders;
EOF;
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->prepare($set_var)->execute();
$stmt = $conn->prepare($use_var);
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach($stmt->fetchAll() as $k=>$v) {
print_r($v);
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo PHP_EOL;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment