test4.php 4.6 KB

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