PageRenderTime 66ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/test/zend/bad/ext/mysqli/tests/mysqli_select_db.php

http://github.com/facebook/hiphop-php
PHP | 89 lines | 60 code | 22 blank | 7 comment | 22 complexity | 22dfa04ddf77a9bacfc2e5c1dd7a4c3b MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?php
  2. require_once("connect.inc");
  3. $test_table_name = 'test_mysqli_select_db_table_1'; require_once("table.inc");
  4. $tmp = NULL;
  5. $link = NULL;
  6. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  7. printf("[003] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
  8. $host, $user, $db, $port, $socket);
  9. /* does not make too much sense, unless we have access to at least one more database than $db */
  10. if (!mysqli_select_db($link, $db))
  11. printf("[005] Cannot select DB %s, [%d] %s\n", $db, mysqli_errno($link), mysqli_error($link));
  12. if (!$res = mysqli_query($link, "SELECT DATABASE() AS dbname"))
  13. printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  14. if (!$row = mysqli_fetch_assoc($res))
  15. printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  16. if ($row['dbname'] !== (string)$db)
  17. printf("[008] Expecting database '%s', found '%s'\n", $db, $row['dbname']);
  18. mysqli_free_result($res);
  19. if (mysqli_select_db($link, 'mysql')) {
  20. // Yippie, a second database to play with - that's great because mysqli_select_db
  21. // ($db) was done by mysqli__connect() already and the previous test_mysqli_select_db_table_1
  22. // was quite useless
  23. if (!$res = mysqli_query($link, "SELECT DATABASE() AS dbname"))
  24. printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  25. if (!$row = mysqli_fetch_assoc($res))
  26. printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  27. if (strtolower($row['dbname']) !== 'mysql')
  28. printf("[011] Expecting database 'mysql', found '%s'\n", $row['dbname']);
  29. mysqli_free_result($res);
  30. }
  31. if (!$link->select_db($db))
  32. printf("[012] Failed to set '%s' as current DB; [%d] %s\n", $link->errno, $link->error);
  33. if (!$res = mysqli_query($link, "SELECT DATABASE() AS dbname"))
  34. printf("[013] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  35. if (!$row = mysqli_fetch_assoc($res))
  36. printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  37. $current_db = $row['dbname'];
  38. mysqli_report(MYSQLI_REPORT_OFF);
  39. mysqli_select_db($link, 'I can not imagine that this database exists');
  40. mysqli_report(MYSQLI_REPORT_ERROR);
  41. ob_start();
  42. mysqli_select_db($link, 'I can not imagine that this database exists');
  43. $output = ob_get_contents();
  44. ob_end_clean();
  45. if (!stristr($output, "1049") && !stristr($output, "1044") && !stristr($output, "1045")) {
  46. /* Error: 1049 SQLSTATE: 42000 (ER_BAD_DB_ERROR) Message: Unknown database '%s' */
  47. /* Error: 1044 SQLSTATE: 42000 (ER_DBACCESS_DENIED_ERROR) Message: Access denied for user '%s'@'%s' to database '%s' */
  48. /* Error: 1045 SQLSTATE: 28000 (ER_ACCESS_DENIED_ERROR) Message: Access denied for user '%s'@'%s' (using password: %s) */
  49. echo $output;
  50. }
  51. if (!$res = mysqli_query($link, "SELECT DATABASE() AS dbname"))
  52. printf("[015] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  53. if (!$row = mysqli_fetch_assoc($res))
  54. printf("[016] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  55. if (strtolower($row['dbname']) != strtolower($current_db))
  56. printf("[017] Current DB should not change if set fails\n");
  57. if (!$res = $link->query("SELECT id FROM test_mysqli_select_db_table_1 WHERE id = 1"))
  58. printf("[018] [%d] %s\n");
  59. $row = $res->fetch_assoc();
  60. $res->free();
  61. mysqli_close($link);
  62. if (NULL !== ($tmp = mysqli_select_db($link, $db)))
  63. printf("[017] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  64. print "done!\n";
  65. ?>
  66. <?php error_reporting(0); ?>
  67. <?php $test_table_name = 'test_mysqli_select_db_table_1'; require_once("clean_table.inc"); ?>