1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> |
2 |
<HTML lang="en"> |
3 |
<HEAD> |
4 |
<TITLE>PHP Test</TITLE> |
5 |
<META name="author" content="TeamFXML"> |
6 |
<META name="copyright" content="Team FXML"> |
7 |
<META name="description" content="This page description."> |
8 |
<META name="keywords" content="insert, your, keywords"> |
9 |
</HEAD> |
10 |
<BODY> |
11 |
<? |
12 |
//This is the ODBC Socket Server class PHP client class with sample usage |
13 |
// at bottom. |
14 |
// (c) 1999 Team FXML |
15 |
// Released into the public domain for version 0.90 of ODBC Socket Server |
16 |
// http://odbc.linuxbox.com/ |
17 |
|
18 |
class ODBCSocketServer { |
19 |
var $sHostName; //name of the host to connect to |
20 |
var $nPort; //port to connect to |
21 |
var $sConnectionString; //connection string to use |
22 |
|
23 |
//function to parse the SQL |
24 |
function ExecSQL($sSQL) { |
25 |
|
26 |
$fToOpen = fsockopen($this->sHostName, $this->nPort, &$errno, &$errstr, 30); |
27 |
if (!$fToOpen) |
28 |
{ |
29 |
//contruct error string to return |
30 |
$sReturn = "<?xml version=\"1.0\"?>\r\n<result state=\"failure\">\r\n<error>$errstr</error>\r\n</result>\r\n"; |
31 |
} |
32 |
else |
33 |
{ |
34 |
//construct XML to send |
35 |
//search and replace HTML chars in SQL first |
36 |
$sSQL = HTMLSpecialChars($sSQL); |
37 |
$sSend = "<?xml version=\"1.0\"?>\r\n<request>\r\n<connectionstring>$this->sConnectionString</connectionstring>\r\n<sql>$sSQL</sql>\r\n</request>\r\n"; |
38 |
//write request |
39 |
fputs($fToOpen, $sSend); |
40 |
//now read response |
41 |
while (!feof($fToOpen)) |
42 |
{ |
43 |
$sReturn = $sReturn . fgets($fToOpen, 128); |
44 |
} |
45 |
fclose($fToOpen); |
46 |
} |
47 |
return $sReturn; |
48 |
} |
49 |
}//class |
50 |
|
51 |
//Here is the code that uses this class. First we create the class |
52 |
$oTest = new ODBCSocketServer; |
53 |
|
54 |
//Set the Hostname, port, and connection string |
55 |
// $oTest->sHostName = "hbreyer2.pliva.hr"; |
56 |
$oTest->sHostName = "tkcpdc.pliva.hr"; |
57 |
$oTest->nPort = 9628; |
58 |
$oTest->sConnectionString = "DSN=Imenik;UID=admin;PWD=admin"; |
59 |
|
60 |
//now exec the SQL |
61 |
$sResult = $oTest->ExecSQL("SELECT * FROM g1"); |
62 |
|
63 |
//now format and print the results. Subsititute in your code here! |
64 |
//We will use the PHP XML Parser module to parse the result :) |
65 |
//to use the parser, remember to compile PHP with the --with-xml option |
66 |
//see the PHP manual for more info and specific examples |
67 |
|
68 |
//For this example, we will print the results in table form |
69 |
//and then print the raw XML string |
70 |
|
71 |
//Here will be the XML handlers we will use |
72 |
|
73 |
//Handler for starting elements |
74 |
function startElement($parser, $name, $attribs) |
75 |
{ |
76 |
if (strtolower($name) == "row") |
77 |
{ |
78 |
//handler for the row element |
79 |
print "<tr>"; |
80 |
} |
81 |
if (strtolower($name) == "column") |
82 |
{ |
83 |
//handler for the column |
84 |
print "<td>"; |
85 |
} |
86 |
if (strtolower($name) == "error") |
87 |
{ |
88 |
//handler for the error |
89 |
print "<tr><td>"; |
90 |
} |
91 |
if (strtolower($name) == "result") |
92 |
{ |
93 |
print "Table of Results: <br><table border=1>"; |
94 |
} |
95 |
} |
96 |
|
97 |
//handler for the end of elements |
98 |
function endElement($parser, $name) |
99 |
{ |
100 |
if (strtolower($name) == "row") |
101 |
{ |
102 |
//handler for the row element |
103 |
print "</tr>"; |
104 |
} |
105 |
if (strtolower($name) == "column") |
106 |
{ |
107 |
//handler for the column |
108 |
print "</td>"; |
109 |
} |
110 |
if (strtolower($name) == "error") |
111 |
{ |
112 |
//handler for the error |
113 |
print "</td></tr>"; |
114 |
} |
115 |
if (strtolower($name) == "result") |
116 |
{ |
117 |
print "</table> <br>End Of Results<br>"; |
118 |
} |
119 |
} |
120 |
|
121 |
//handler for character data |
122 |
function characterData($parser, $data) |
123 |
{ |
124 |
print "$data"; |
125 |
} |
126 |
|
127 |
|
128 |
//parse the XML |
129 |
$xml_parser = xml_parser_create(); |
130 |
xml_set_element_handler($xml_parser, "startElement", "endElement"); |
131 |
xml_set_character_data_handler($xml_parser, "characterData"); |
132 |
|
133 |
if (!xml_parse($xml_parser, $sResult)) { |
134 |
die(sprintf("XML error: %s at line %d", |
135 |
xml_error_string(xml_get_error_code($xml_parser)), |
136 |
xml_get_current_line_number($xml_parser))); |
137 |
} |
138 |
else |
139 |
{ |
140 |
echo("Successful XML parse. "); |
141 |
} |
142 |
|
143 |
print "Raw data results: <br>"; |
144 |
xml_parser_free($xml_parser); |
145 |
|
146 |
$sResult = HtmlSpecialChars($sResult); |
147 |
echo("<pre>"); |
148 |
echo($sResult); |
149 |
echo("</pre>"); |
150 |
|
151 |
?> |
152 |
|
153 |
</BODY> |
154 |
</HTML> |
155 |
|