Created
June 29, 2016 16:00
-
-
Save jhyland87/b583149c22aaf9bdee9023118fd5eb72 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 | |
$sql = <<<EOF | |
SET @ship_id = (SELECT ship_id FROM orders WHERE ship_id IS NOT NULL ORDER BY ship_id LIMIT 1); | |
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); | |
$stmt = $conn->prepare($sql); | |
$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