/third_party/adodb/tests/test4.php

https://github.com/viglesiasce/testlink · PHP · 143 lines · 79 code · 33 blank · 31 comment · 10 complexity · a2aed8fae0e949ea4fbddb8c56b64b63 MD5 · raw file

  1. <?php
  2. /**
  3. * @version V4.50 6 July 2004 (c) 2000-2009 John Lim (jlim#natsoft.com). All rights reserved.
  4. * Released under both BSD license and Lesser GPL library license.
  5. * Whenever there is any discrepancy between the two licenses,
  6. * the BSD license will take precedence.
  7. *
  8. * Set tabs to 4 for best viewing.
  9. *
  10. * Latest version is available at http://php.weblogs.com
  11. *
  12. * Test GetUpdateSQL and GetInsertSQL.
  13. */
  14. error_reporting(E_ALL);
  15. function testsql()
  16. {
  17. include('../adodb.inc.php');
  18. include('../tohtml.inc.php');
  19. global $ADODB_FORCE_TYPE;
  20. //==========================
  21. // This code tests an insert
  22. $sql = "
  23. SELECT *
  24. FROM ADOXYZ WHERE id = -1";
  25. // Select an empty record from the database
  26. #$conn = ADONewConnection("mssql"); // create a connection
  27. #$conn->PConnect("", "sa", "natsoft", "northwind"); // connect to MySQL, testdb
  28. $conn = ADONewConnection("mysql"); // create a connection
  29. $conn->PConnect("localhost", "root", "", "test"); // connect to MySQL, testdb
  30. #$conn = ADONewConnection('oci8po');
  31. #$conn->Connect('','scott','natsoft');
  32. if (PHP_VERSION >= 5) {
  33. $connstr = "mysql:dbname=northwind";
  34. $u = 'root';$p='';
  35. $conn = ADONewConnection('pdo');
  36. $conn->Connect($connstr, $u, $p);
  37. }
  38. //$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
  39. $conn->debug=1;
  40. $conn->Execute("delete from adoxyz where lastname like 'Smi%'");
  41. $rs = $conn->Execute($sql); // Execute the query and get the empty recordset
  42. $record = array(); // Initialize an array to hold the record data to insert
  43. if (strpos($conn->databaseType,'mysql')===false) $record['id'] = 751;
  44. $record["firstname"] = 'Jann';
  45. $record["lastname"] = "Smitts";
  46. $record["created"] = time();
  47. $insertSQL = $conn->GetInsertSQL($rs, $record);
  48. $conn->Execute($insertSQL); // Insert the record into the database
  49. if (strpos($conn->databaseType,'mysql')===false) $record['id'] = 752;
  50. // Set the values for the fields in the record
  51. $record["firstname"] = 'anull';
  52. $record["lastname"] = "Smith\$@//";
  53. $record["created"] = time();
  54. if (isset($_GET['f'])) $ADODB_FORCE_TYPE = $_GET['f'];
  55. //$record["id"] = -1;
  56. // Pass the empty recordset and the array containing the data to insert
  57. // into the GetInsertSQL function. The function will process the data and return
  58. // a fully formatted insert sql statement.
  59. $insertSQL = $conn->GetInsertSQL($rs, $record);
  60. $conn->Execute($insertSQL); // Insert the record into the database
  61. $insertSQL2 = $conn->GetInsertSQL($table='ADOXYZ', $record);
  62. if ($insertSQL != $insertSQL2) echo "<p><b>Walt's new stuff failed</b>: $insertSQL2</p>";
  63. //==========================
  64. // This code tests an update
  65. $sql = "
  66. SELECT *
  67. FROM ADOXYZ WHERE lastname=".$conn->Param('var'). " ORDER BY 1";
  68. // Select a record to update
  69. $varr = array('var'=>$record['lastname'].'');
  70. $rs = $conn->Execute($sql,$varr); // Execute the query and get the existing record to update
  71. if (!$rs || $rs->EOF) print "<p><b>No record found!</b></p>";
  72. $record = array(); // Initialize an array to hold the record data to update
  73. // Set the values for the fields in the record
  74. $record["firstName"] = "Caroline".rand();
  75. //$record["lasTname"] = ""; // Update Caroline's lastname from Miranda to Smith
  76. $record["creAted"] = '2002-12-'.(rand()%30+1);
  77. $record['num'] = '';
  78. // Pass the single record recordset and the array containing the data to update
  79. // into the GetUpdateSQL function. The function will process the data and return
  80. // a fully formatted update sql statement.
  81. // If the data has not changed, no recordset is returned
  82. $updateSQL = $conn->GetUpdateSQL($rs, $record);
  83. $conn->Execute($updateSQL,$varr); // Update the record in the database
  84. if ($conn->Affected_Rows() != 1)print "<p><b>Error1 </b>: Rows Affected=".$conn->Affected_Rows().", should be 1</p>";
  85. $record["firstName"] = "Caroline".rand();
  86. $record["lasTname"] = "Smithy Jones"; // Update Caroline's lastname from Miranda to Smith
  87. $record["creAted"] = '2002-12-'.(rand()%30+1);
  88. $record['num'] = 331;
  89. $updateSQL = $conn->GetUpdateSQL($rs, $record);
  90. $conn->Execute($updateSQL,$varr); // Update the record in the database
  91. if ($conn->Affected_Rows() != 1)print "<p><b>Error 2</b>: Rows Affected=".$conn->Affected_Rows().", should be 1</p>";
  92. $rs = $conn->Execute("select * from ADOXYZ where lastname like 'Sm%'");
  93. //adodb_pr($rs);
  94. rs2html($rs);
  95. $record["firstName"] = "Carol-new-".rand();
  96. $record["lasTname"] = "Smithy"; // Update Caroline's lastname from Miranda to Smith
  97. $record["creAted"] = '2002-12-'.(rand()%30+1);
  98. $record['num'] = 331;
  99. $conn->AutoExecute('ADOXYZ',$record,'UPDATE', "lastname like 'Sm%'");
  100. $rs = $conn->Execute("select * from ADOXYZ where lastname like 'Sm%'");
  101. //adodb_pr($rs);
  102. rs2html($rs);
  103. }
  104. testsql();
  105. ?>