Created
June 29, 2016 16:03
-
-
Save jhyland87/3de37904e8294aecf1c90ce4bae45498 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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