Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. <?php
  2. $sql = <<<EOF
  3. SET @ship_id = (SELECT ship_id FROM orders WHERE ship_id IS NOT NULL ORDER BY ship_id LIMIT 1);
  4. SELECT
  5. id,
  6. user_id,
  7. COALESCE(ship_id, @ship_id) AS ship_id
  8. FROM
  9. orders;
  10. EOF;
  11.  
  12. $servername = "localhost";
  13. $username = "root";
  14. $password = "root";
  15. $dbname = "test";
  16.  
  17. try {
  18. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  19. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  20. $stmt = $conn->prepare($sql);
  21. $stmt->execute();
  22.  
  23. // set the resulting array to associative
  24. $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
  25. foreach($stmt->fetchAll() as $k=>$v) {
  26. print_r($v);
  27. }
  28. }
  29. catch(PDOException $e) {
  30. echo "Error: " . $e->getMessage();
  31. }
  32. $conn = null;
  33.  
  34. echo PHP_EOL;
  35. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement