home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / test4.php < prev    next >
Encoding:
PHP Script  |  2004-03-20  |  2.8 KB  |  90 lines

  1. <?php
  2.  
  3. /** 
  4.  * @version V4.21 20 Mar 2004 (c) 2000-2004 John Lim (jlim@natsoft.com.my). All rights reserved.
  5.  * Released under both BSD license and Lesser GPL library license. 
  6.  * Whenever there is any discrepancy between the two licenses, 
  7.  * the BSD license will take precedence. 
  8.  *
  9.  * Set tabs to 4 for best viewing.
  10.  * 
  11.  * Latest version is available at http://php.weblogs.com
  12.  *
  13.  * Test GetUpdateSQL and GetInsertSQL.
  14.  */
  15.  
  16. error_reporting(E_ALL);
  17. function testsql()
  18. {
  19.  
  20. //define('ADODB_FORCE_NULLS',1);
  21.  
  22. include('../adodb.inc.php');
  23. include('../tohtml.inc.php');
  24.  
  25. //==========================
  26. // This code tests an insert
  27.  
  28. $sql = "
  29. SELECT * 
  30. FROM ADOXYZ WHERE id = -1"; 
  31. // Select an empty record from the database 
  32.  
  33. $conn = &ADONewConnection("mysql");  // create a connection
  34. //$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
  35.  
  36. $conn->debug=1;
  37. $conn->PConnect("localhost", "root", "", "test"); // connect to MySQL, testdb
  38. $conn->Execute("delete from adoxyz where lastname like 'Smith%'");
  39.  
  40. $rs = $conn->Execute($sql); // Execute the query and get the empty recordset
  41. $record = array(); // Initialize an array to hold the record data to insert
  42.  
  43. // Set the values for the fields in the record
  44. $record["firstname"] = 'null';
  45. $record["lastname"] = "Smith\$@//";
  46. $record["created"] = time();
  47. //$record["id"] = -1;
  48.  
  49. // Pass the empty recordset and the array containing the data to insert
  50. // into the GetInsertSQL function. The function will process the data and return
  51. // a fully formatted insert sql statement.
  52. $insertSQL = $conn->GetInsertSQL($rs, $record);
  53.  
  54. $conn->Execute($insertSQL); // Insert the record into the database
  55.  
  56. //==========================
  57. // This code tests an update
  58.  
  59. $sql = "
  60. SELECT * 
  61. FROM ADOXYZ WHERE lastname=".$conn->qstr($record['lastname']); 
  62. // Select a record to update 
  63.  
  64. $rs = $conn->Execute($sql); // Execute the query and get the existing record to update
  65. if (!$rs) print "<p>No record found!</p>";
  66.  
  67. $record = array(); // Initialize an array to hold the record data to update
  68.  
  69. // Set the values for the fields in the record
  70. $record["firstName"] = "Caroline".rand();
  71. $record["lasTname"] = "Smithy Jones"; // Update Caroline's lastname from Miranda to Smith
  72. $record["creAted"] = '2002-12-'.(rand()%30+1);
  73. $record['num'] = 3921;
  74. // Pass the single record recordset and the array containing the data to update
  75. // into the GetUpdateSQL function. The function will process the data and return
  76. // a fully formatted update sql statement.
  77. // If the data has not changed, no recordset is returned
  78. $updateSQL = $conn->GetUpdateSQL($rs, $record);
  79.  
  80. $conn->Execute($updateSQL); // Update the record in the database
  81. print "<p>Rows Affected=".$conn->Affected_Rows()."</p>";
  82.  
  83. $rs = $conn->Execute("select * from adoxyz where lastname like 'Smith%'");
  84. adodb_pr($rs);
  85. rs2html($rs);
  86. }
  87.  
  88.  
  89. testsql();
  90. ?>