1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Author: Georg Richter <georg@php.net> |
16 : +----------------------------------------------------------------------+
17 :
18 : $Id: mysqli_nonapi.c 272374 2008-12-31 11:17:49Z sebastian $
19 : */
20 :
21 : #ifdef HAVE_CONFIG_H
22 : #include "config.h"
23 : #endif
24 :
25 : #include <signal.h>
26 :
27 : #include "php.h"
28 : #include "php_ini.h"
29 : #include "ext/standard/info.h"
30 : #include "php_mysqli.h"
31 :
32 : /* {{{ proto object mysqli_connect([string hostname [,string username [,string passwd [,string dbname [,int port [,string socket]]]]]])
33 : Open a connection to a mysql server */
34 : PHP_FUNCTION(mysqli_connect)
35 191 : {
36 191 : MY_MYSQL *mysql = NULL;
37 191 : MYSQLI_RESOURCE *mysqli_resource = NULL;
38 191 : zval *object = getThis();
39 191 : char *hostname = NULL, *username=NULL, *passwd=NULL, *dbname=NULL, *socket=NULL;
40 191 : unsigned int hostname_len = 0, username_len = 0, passwd_len = 0, dbname_len = 0, socket_len = 0;
41 191 : long port=0;
42 :
43 191 : if (getThis() && !ZEND_NUM_ARGS()) {
44 3 : RETURN_NULL();
45 : }
46 :
47 188 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ssssls", &hostname, &hostname_len, &username, &username_len,
48 : &passwd, &passwd_len, &dbname, &dbname_len, &port, &socket, &socket_len) == FAILURE) {
49 0 : return;
50 : }
51 :
52 188 : if (!socket_len) {
53 188 : socket = NULL;
54 : }
55 :
56 : /* TODO: safe mode handling */
57 188 : if (PG(sql_safe_mode)){
58 : } else {
59 188 : if (!socket_len || !socket) {
60 188 : socket = MyG(default_socket);
61 : }
62 188 : if (!port) {
63 93 : port = MyG(default_port);
64 : }
65 188 : if (!passwd) {
66 0 : passwd = MyG(default_pw);
67 : }
68 188 : if (!username){
69 0 : username = MyG(default_user);
70 : }
71 188 : if (!hostname) {
72 0 : hostname = MyG(default_host);
73 : }
74 : }
75 :
76 188 : if (object && instanceof_function(Z_OBJCE_P(object), mysqli_link_class_entry TSRMLS_CC)) {
77 26 : mysqli_resource = ((mysqli_object *) zend_object_store_get_object(object TSRMLS_CC))->ptr;
78 26 : if (mysqli_resource && mysqli_resource->ptr &&
79 : mysqli_resource->status >= MYSQLI_STATUS_INITIALIZED)
80 : {
81 0 : mysql = (MY_MYSQL*)mysqli_resource->ptr;
82 0 : php_clear_mysql(mysql);
83 0 : if (mysql->mysql) {
84 0 : mysql_close(mysql->mysql);
85 0 : mysql->mysql = NULL;
86 : }
87 : }
88 : }
89 188 : if (!mysql) {
90 188 : mysql = (MY_MYSQL *) ecalloc(1, sizeof(MY_MYSQL));
91 : }
92 :
93 188 : if (!(mysql->mysql = mysql_init(NULL))) {
94 0 : efree(mysql);
95 0 : RETURN_FALSE;
96 : }
97 :
98 : #ifdef HAVE_EMBEDDED_MYSQLI
99 : if (strcmp(hostname, ":embedded")) {
100 : unsigned int external=1;
101 : mysql_options(mysql->mysql, MYSQL_OPT_USE_REMOTE_CONNECTION, (char *)&external);
102 : } else {
103 : hostname[0] = '\0';
104 : mysql_options(mysql->mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, 0);
105 : }
106 : #endif
107 :
108 188 : if (!socket) {
109 188 : socket = MyG(default_socket);
110 : }
111 :
112 188 : if (mysql_real_connect(mysql->mysql,hostname,username,passwd,dbname,port,socket,CLIENT_MULTI_RESULTS) == NULL) {
113 : /* Save error messages */
114 :
115 1 : php_mysqli_throw_sql_exception( mysql->mysql->net.sqlstate, mysql->mysql->net.last_errno TSRMLS_CC,
116 : "%s", mysql->mysql->net.last_error);
117 :
118 1 : php_mysqli_set_error(mysql_errno(mysql->mysql), (char *) mysql_error(mysql->mysql) TSRMLS_CC);
119 :
120 : /* free mysql structure */
121 1 : mysql_close(mysql->mysql);
122 1 : efree(mysql);
123 1 : RETURN_FALSE;
124 : }
125 :
126 : /* clear error */
127 187 : php_mysqli_set_error(mysql_errno(mysql->mysql), (char *) mysql_error(mysql->mysql) TSRMLS_CC);
128 :
129 187 : mysql->mysql->reconnect = MyG(reconnect);
130 :
131 : /* set our own local_infile handler */
132 187 : php_set_local_infile_handler_default(mysql);
133 :
134 187 : if (!mysqli_resource) {
135 187 : mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
136 187 : mysqli_resource->ptr = (void *)mysql;
137 : }
138 187 : mysqli_resource->status = MYSQLI_STATUS_VALID;
139 :
140 349 : if (!object || !instanceof_function(Z_OBJCE_P(object), mysqli_link_class_entry TSRMLS_CC)) {
141 162 : MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_link_class_entry);
142 : } else {
143 25 : ((mysqli_object *) zend_object_store_get_object(object TSRMLS_CC))->ptr = mysqli_resource;
144 : }
145 : }
146 : /* }}} */
147 :
148 : /* {{{ proto int mysqli_connect_errno(void)
149 : Returns the numerical value of the error message from last connect command */
150 : PHP_FUNCTION(mysqli_connect_errno)
151 1 : {
152 1 : RETURN_LONG(MyG(error_no));
153 : }
154 : /* }}} */
155 :
156 : /* {{{ proto string mysqli_connect_error(void)
157 : Returns the text of the error message from previous MySQL operation */
158 : PHP_FUNCTION(mysqli_connect_error)
159 1 : {
160 1 : if (MyG(error_msg)) {
161 1 : RETURN_STRING(MyG(error_msg),1);
162 : } else {
163 0 : RETURN_NULL();
164 : }
165 : }
166 : /* }}} */
167 :
168 : /* {{{ proto mixed mysqli_fetch_array (object result [,int resulttype])
169 : Fetch a result row as an associative array, a numeric array, or both */
170 : PHP_FUNCTION(mysqli_fetch_array)
171 0 : {
172 0 : php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
173 0 : }
174 : /* }}} */
175 :
176 : /* {{{ proto mixed mysqli_fetch_assoc (object result)
177 : Fetch a result row as an associative array */
178 : PHP_FUNCTION(mysqli_fetch_assoc)
179 12 : {
180 12 : php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQLI_ASSOC, 0);
181 12 : }
182 : /* }}} */
183 :
184 : /* {{{ proto mixed mysqli_fetch_object (object result [, string class_name [, NULL|array ctor_params]])
185 : Fetch a result row as an object */
186 : PHP_FUNCTION(mysqli_fetch_object)
187 4 : {
188 4 : php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQLI_ASSOC, 1);
189 4 : }
190 : /* }}} */
191 :
192 : /* {{{ proto bool mysqli_multi_query(object link, string query)
193 : Binary-safe version of mysql_query() */
194 : PHP_FUNCTION(mysqli_multi_query)
195 4 : {
196 : MY_MYSQL *mysql;
197 : zval *mysql_link;
198 4 : char *query = NULL;
199 : unsigned int query_len;
200 :
201 4 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &mysql_link, mysqli_link_class_entry, &query, &query_len) == FAILURE) {
202 0 : return;
203 : }
204 4 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
205 :
206 4 : MYSQLI_ENABLE_MQ;
207 4 : if (mysql_real_query(mysql->mysql, query, query_len)) {
208 : char s_error[MYSQL_ERRMSG_SIZE], s_sqlstate[SQLSTATE_LENGTH+1];
209 : unsigned int s_errno;
210 0 : MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
211 :
212 : /* we have to save error information, cause
213 : MYSQLI_DISABLE_MQ will reset error information */
214 0 : strcpy(s_error, mysql_error(mysql->mysql));
215 0 : strcpy(s_sqlstate, mysql_sqlstate(mysql->mysql));
216 0 : s_errno = mysql_errno(mysql->mysql);
217 :
218 0 : MYSQLI_DISABLE_MQ;
219 :
220 : /* restore error information */
221 0 : strcpy(mysql->mysql->net.last_error, s_error);
222 0 : strcpy(mysql->mysql->net.sqlstate, s_sqlstate);
223 0 : mysql->mysql->net.last_errno = s_errno;
224 :
225 0 : RETURN_FALSE;
226 : }
227 4 : RETURN_TRUE;
228 : }
229 : /* }}} */
230 :
231 : /* {{{ proto mixed mysqli_query(object link, string query [,int resultmode]) */
232 : PHP_FUNCTION(mysqli_query)
233 212 : {
234 : MY_MYSQL *mysql;
235 : zval *mysql_link;
236 : MYSQLI_RESOURCE *mysqli_resource;
237 : MYSQL_RES *result;
238 212 : char *query = NULL;
239 : unsigned int query_len;
240 212 : unsigned long resultmode = MYSQLI_STORE_RESULT;
241 :
242 212 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|l", &mysql_link, mysqli_link_class_entry, &query, &query_len, &resultmode) == FAILURE) {
243 0 : return;
244 : }
245 :
246 212 : if (!query_len) {
247 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty query");
248 0 : RETURN_FALSE;
249 : }
250 212 : if (resultmode != MYSQLI_USE_RESULT && resultmode != MYSQLI_STORE_RESULT) {
251 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid value for resultmode");
252 0 : RETURN_FALSE;
253 : }
254 :
255 212 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL*, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
256 :
257 211 : MYSQLI_DISABLE_MQ;
258 :
259 211 : if (mysql_real_query(mysql->mysql, query, query_len)) {
260 7 : MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
261 7 : RETURN_FALSE;
262 : }
263 :
264 204 : if (!mysql_field_count(mysql->mysql)) {
265 : /* no result set - not a SELECT */
266 180 : if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
267 0 : php_mysqli_report_index(query, mysql->mysql->server_status TSRMLS_CC);
268 : }
269 180 : RETURN_TRUE;
270 : }
271 :
272 24 : result = (resultmode == MYSQLI_USE_RESULT) ? mysql_use_result(mysql->mysql) : mysql_store_result(mysql->mysql);
273 :
274 24 : if (!result) {
275 0 : php_mysqli_throw_sql_exception(mysql->mysql->net.sqlstate, mysql->mysql->net.last_errno TSRMLS_CC,
276 : "%s", mysql->mysql->net.last_error);
277 0 : RETURN_FALSE;
278 : }
279 :
280 24 : if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
281 0 : php_mysqli_report_index(query, mysql->mysql->server_status TSRMLS_CC);
282 : }
283 :
284 24 : mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
285 24 : mysqli_resource->ptr = (void *)result;
286 24 : mysqli_resource->status = MYSQLI_STATUS_VALID;
287 24 : MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_result_class_entry);
288 : }
289 : /* }}} */
290 :
291 : /* {{{ proto object mysqli_get_warnings(object link) */
292 : PHP_FUNCTION(mysqli_get_warnings)
293 0 : {
294 : MY_MYSQL *mysql;
295 : zval *mysql_link;
296 : MYSQLI_RESOURCE *mysqli_resource;
297 : MYSQLI_WARNING *w;
298 :
299 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
300 0 : return;
301 : }
302 0 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL*, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
303 :
304 0 : if (mysql_warning_count(mysql->mysql)) {
305 0 : w = php_get_warnings(mysql->mysql);
306 : } else {
307 0 : RETURN_FALSE;
308 : }
309 0 : mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
310 0 : mysqli_resource->ptr = mysqli_resource->info = (void *)w;
311 0 : MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_warning_class_entry);
312 : }
313 : /* }}} */
314 :
315 : /* {{{ proto object mysqli_stmt_get_warnings(object link) */
316 : PHP_FUNCTION(mysqli_stmt_get_warnings)
317 0 : {
318 : MY_STMT *stmt;
319 : zval *stmt_link;
320 : MYSQLI_RESOURCE *mysqli_resource;
321 : MYSQLI_WARNING *w;
322 :
323 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &stmt_link, mysqli_stmt_class_entry) == FAILURE) {
324 0 : return;
325 : }
326 0 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT*, &stmt_link, "mysqli_stmt", 1);
327 :
328 0 : if (mysql_warning_count(stmt->stmt->mysql)) {
329 0 : w = php_get_warnings(stmt->stmt->mysql);
330 : } else {
331 0 : RETURN_FALSE;
332 : }
333 0 : mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
334 0 : mysqli_resource->ptr = mysqli_resource->info = (void *)w;
335 0 : MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_warning_class_entry);
336 : }
337 : /* }}} */
338 :
339 : #ifdef HAVE_MYSQLI_SET_CHARSET
340 : /* {{{ proto bool mysqli_set_charset(object link, string csname)
341 : sets client character set */
342 : PHP_FUNCTION(mysqli_set_charset)
343 5 : {
344 : MY_MYSQL *mysql;
345 : zval *mysql_link;
346 5 : char *cs_name = NULL;
347 : unsigned int len;
348 :
349 5 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &mysql_link, mysqli_link_class_entry, &cs_name, &len) == FAILURE) {
350 0 : return;
351 : }
352 5 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL*, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
353 :
354 4 : if (mysql_set_character_set(mysql->mysql, cs_name)) {
355 2 : RETURN_FALSE;
356 : }
357 2 : RETURN_TRUE;
358 : }
359 : /* }}} */
360 : #endif
361 :
362 : #ifdef HAVE_MYSQLI_GET_CHARSET
363 : /* {{{ proto object mysqli_get_charset(object link)
364 : returns a character set object */
365 : PHP_FUNCTION(mysqli_get_charset)
366 0 : {
367 : MY_MYSQL *mysql;
368 : zval *mysql_link;
369 : MY_CHARSET_INFO cs;
370 :
371 0 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
372 0 : return;
373 : }
374 0 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL*, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
375 :
376 0 : object_init(return_value);
377 :
378 0 : mysql_get_character_set_info(mysql->mysql, &cs);
379 :
380 0 : add_property_string(return_value, "charset", (cs.name) ? (char *)cs.csname : "", 1);
381 0 : add_property_string(return_value, "collation",(cs.name) ? (char *)cs.name : "", 1);
382 0 : add_property_string(return_value, "comment", (cs.comment) ? (char *)cs.comment : "", 1);
383 0 : add_property_string(return_value, "dir", (cs.dir) ? (char *)cs.dir : "", 1);
384 0 : add_property_long(return_value, "min_length", cs.mbminlen);
385 0 : add_property_long(return_value, "max_length", cs.mbmaxlen);
386 0 : add_property_long(return_value, "number", cs.number);
387 0 : add_property_long(return_value, "state", cs.state);
388 : }
389 : /* }}} */
390 : #endif
391 :
392 : /*
393 : * Local variables:
394 : * tab-width: 4
395 : * c-basic-offset: 4
396 : * End:
397 : * vim600: noet sw=4 ts=4 fdm=marker
398 : * vim<600: noet sw=4 ts=4
399 : */
|