1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @version V4.50 6 July 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 | $conn->PConnect("localhost", "root", "", "test"); // connect to MySQL, testdb |
---|
35 | |
---|
36 | //$conn =& ADONewConnection('oci8'); |
---|
37 | //$conn->Connect('','scott','natsoft'); |
---|
38 | //$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; |
---|
39 | |
---|
40 | $conn->debug=1; |
---|
41 | $conn->Execute("delete from adoxyz where lastname like 'Smith%'"); |
---|
42 | |
---|
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 | |
---|
46 | // Set the values for the fields in the record |
---|
47 | $record["firstname"] = 'null'; |
---|
48 | $record["lastname"] = "Smith\$@//"; |
---|
49 | $record["created"] = time(); |
---|
50 | //$record["id"] = -1; |
---|
51 | |
---|
52 | // Pass the empty recordset and the array containing the data to insert |
---|
53 | // into the GetInsertSQL function. The function will process the data and return |
---|
54 | // a fully formatted insert sql statement. |
---|
55 | $insertSQL = $conn->GetInsertSQL($rs, $record); |
---|
56 | |
---|
57 | $conn->Execute($insertSQL); // Insert the record into the database |
---|
58 | |
---|
59 | $insertSQL2 = $conn->GetInsertSQL($table='ADOXYZ', $record); |
---|
60 | if ($insertSQL != $insertSQL2) echo "<p><b>Walt's new stuff failed</b>: $insertSQL2</p>"; |
---|
61 | //========================== |
---|
62 | // This code tests an update |
---|
63 | |
---|
64 | $sql = " |
---|
65 | SELECT * |
---|
66 | FROM ADOXYZ WHERE lastname=".$conn->qstr($record['lastname']). " ORDER BY 1"; |
---|
67 | // Select a record to update |
---|
68 | |
---|
69 | $rs = $conn->Execute($sql); // Execute the query and get the existing record to update |
---|
70 | if (!$rs) print "<p><b>No record found!</b></p>"; |
---|
71 | |
---|
72 | $record = array(); // Initialize an array to hold the record data to update |
---|
73 | |
---|
74 | // Set the values for the fields in the record |
---|
75 | $record["firstName"] = "Caroline".rand(); |
---|
76 | $record["lasTname"] = "Smithy Jones"; // Update Caroline's lastname from Miranda to Smith |
---|
77 | $record["creAted"] = '2002-12-'.(rand()%30+1); |
---|
78 | $record['num'] = 3921; |
---|
79 | // Pass the single record recordset and the array containing the data to update |
---|
80 | // into the GetUpdateSQL function. The function will process the data and return |
---|
81 | // a fully formatted update sql statement. |
---|
82 | // If the data has not changed, no recordset is returned |
---|
83 | $updateSQL = $conn->GetUpdateSQL($rs, $record); |
---|
84 | |
---|
85 | $conn->Execute($updateSQL); // Update the record in the database |
---|
86 | if ($conn->Affected_Rows() != 1)print "<p><b>Error</b>: Rows Affected=".$conn->Affected_Rows().", should be 1</p>"; |
---|
87 | |
---|
88 | $rs = $conn->Execute("select * from adoxyz where lastname like 'Smith%'"); |
---|
89 | adodb_pr($rs); |
---|
90 | rs2html($rs); |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | testsql(); |
---|
95 | ?> |
---|