php-src/ext/mysqli/mysqli_nonapi.c

224 lines
6.7 KiB
C
Raw Normal View History

2003-02-12 00:45:53 +00:00
/*
+----------------------------------------------------------------------+
2003-03-03 22:36:47 +00:00
| PHP Version 5 |
2003-02-12 00:45:53 +00:00
+----------------------------------------------------------------------+
2003-03-03 22:36:47 +00:00
| Copyright (c) 1997-2003 The PHP Group |
2003-02-12 00:45:53 +00:00
+----------------------------------------------------------------------+
| This source file is subject to version 3.0 of the PHP license, |
2003-02-12 00:45:53 +00:00
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_0.txt. |
2003-02-12 00:45:53 +00:00
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Georg Richter <georg@php.net> |
+----------------------------------------------------------------------+
$Id$
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <signal.h>
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_mysqli.h"
2003-03-15 22:51:49 +00:00
/* {{{ proto resource mysqli_connect([string hostname [,string username [,string passwd [,string dbname [,int port [,string socket]]]]]])
Open a connection to a mysql server */
2003-02-12 00:45:53 +00:00
PHP_FUNCTION(mysqli_connect)
{
2003-03-08 23:33:12 +00:00
MYSQL *mysql;
MYSQLI_RESOURCE *mysqli_resource;
PR_MYSQL *prmysql = NULL;
zval *object = getThis();
char *hostname = NULL, *username=NULL, *passwd=NULL, *dbname=NULL, *socket=NULL;
unsigned int hostname_len, username_len, passwd_len, dbname_len, socket_len;
unsigned int port=0;
struct timeval starttime;
2003-02-12 00:45:53 +00:00
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ssssls", &hostname, &hostname_len, &username, &username_len,
&passwd, &passwd_len, &dbname, &dbname_len, &port, &socket, &socket_len) == FAILURE) {
return;
}
/* TODO: safe mode handling */
if (PG(sql_safe_mode)){
} else {
if (!passwd) {
passwd = MyG(default_pw);
if (!username){
username = MyG(default_user);
if (!hostname) {
hostname = MyG(default_host);
}
}
}
}
mysql = mysql_init(NULL);
2003-03-08 23:33:12 +00:00
if (MyG(profiler)){
gettimeofday(&starttime, NULL);
}
2003-02-12 00:45:53 +00:00
if (mysql_real_connect(mysql,hostname,username,passwd,dbname,port,socket,0) == NULL) {
/* Save error messages */
2003-02-14 18:27:20 +00:00
php_mysqli_set_error(mysql_errno(mysql), (char *) mysql_error(mysql) TSRMLS_CC);
2003-02-14 16:49:09 +00:00
2003-02-12 00:45:53 +00:00
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", mysql_error(mysql));
/* free mysql structure */
mysql_close(mysql);
RETURN_FALSE;
}
php_mysqli_set_error(mysql_errno(mysql), (char *) mysql_error(mysql) TSRMLS_CC);
2003-02-12 00:45:53 +00:00
2003-03-08 23:33:12 +00:00
if (MyG(profiler)) {
prmysql = (PR_MYSQL *)MYSQLI_PROFILER_NEW(prmain, MYSQLI_PR_MYSQL, 0);
2003-03-08 23:33:12 +00:00
php_mysqli_profiler_timediff(starttime, &prmysql->header.elapsedtime);
MYSQLI_PROFILER_STARTTIME(prmysql);
prmysql->hostname = estrdup(hostname);
prmysql->username = estrdup(username);
prmysql->thread_id = mysql->thread_id;
}
mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
mysqli_resource->ptr = (void *)mysql;
mysqli_resource->prinfo = prmysql;
if (!object) {
2003-03-08 23:33:12 +00:00
MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_link_class_entry);
} else {
2003-03-08 23:33:12 +00:00
((mysqli_object *) zend_object_store_get_object(object TSRMLS_CC))->ptr = mysqli_resource;
}
2003-02-12 00:45:53 +00:00
}
/* }}} */
2003-09-16 19:45:22 +00:00
/* {{{ proto int mysqli_connect_errno()
Returns the numerical value of the error message from last connect command */
PHP_FUNCTION(mysqli_connect_errno)
{
RETURN_LONG(MyG(error_no));
}
/* }}} */
/* {{{ proto string mysqli_connect_error()
Returns the text of the error message from previous MySQL operation */
PHP_FUNCTION(mysqli_connect_error)
{
RETURN_STRING(MyG(error_msg),1);
}
/* }}} */
2003-06-22 06:16:47 +00:00
/* {{{ proto array mysqli_fetch_array (object result [,int resulttype])
2003-03-15 22:51:49 +00:00
Fetch a result row as an associative array, a numeric array, or both */
2003-02-12 00:45:53 +00:00
PHP_FUNCTION(mysqli_fetch_array)
{
php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
2003-02-12 00:45:53 +00:00
}
/* }}} */
2003-06-22 06:16:47 +00:00
/* {{{ proto array mysqli_fetch_assoc (object result)
2003-03-15 22:51:49 +00:00
Fetch a result row as an associative array */
2003-02-12 00:45:53 +00:00
PHP_FUNCTION(mysqli_fetch_assoc)
{
php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQLI_ASSOC, 0);
2003-02-12 00:45:53 +00:00
}
/* }}} */
/* {{{ proto array mysqli_fetch_object (object result [, string class_name [, NULL|array ctor_params]])
2003-03-15 22:51:49 +00:00
Fetch a result row as an object */
2003-02-12 00:45:53 +00:00
PHP_FUNCTION(mysqli_fetch_object)
{
php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQLI_ASSOC, 1);
2003-02-12 00:45:53 +00:00
}
/* }}} */
2003-06-22 06:16:47 +00:00
/* {{{ proto resource mysqli_query(object link, string query [,int resultmode])
2003-03-15 22:51:49 +00:00
Send a MySQL query */
2003-07-28 10:23:36 +00:00
PHP_FUNCTION(mysqli_query)
{
2003-03-08 23:33:12 +00:00
MYSQL *mysql;
zval *mysql_link;
MYSQLI_RESOURCE *mysqli_resource;
MYSQL_RES *result;
PR_MYSQL *prmysql;
PR_QUERY *prquery;
PR_RESULT *prresult;
char *query = NULL;
unsigned int query_len;
unsigned int resultmode = 0;
struct timeval starttime;
2003-02-12 00:45:53 +00:00
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|l", &mysql_link, mysqli_link_class_entry, &query, &query_len, &resultmode) == FAILURE) {
return;
}
2003-03-08 23:33:12 +00:00
MYSQLI_FETCH_RESOURCE(mysql, MYSQL*, prmysql, PR_MYSQL *, &mysql_link, "mysqli_link");
/* profiling information */
if (MyG(profiler)) {
prquery = (PR_QUERY *)MYSQLI_PROFILER_NEW(prmysql, MYSQLI_PR_QUERY, 1);
prquery->explain.query = my_estrdup(query);
if (!strncasecmp("select", query, 6)){
if (!(MYSQLI_PROFILER_EXPLAIN(&prquery->explain, &prquery->header, mysql, query))) {
RETURN_FALSE;
}
2003-03-03 22:36:47 +00:00
}
}
2003-03-08 23:33:12 +00:00
if (mysql_real_query(mysql, query, query_len)) {
RETURN_FALSE;
}
if (MyG(profiler)) {
MYSQLI_PROFILER_ELAPSEDTIME(prquery);
prquery->insertid = mysql_insert_id(mysql);
prquery->affectedrows = mysql_affected_rows(mysql);
2003-02-12 00:45:53 +00:00
}
if (!mysql_field_count(mysql)) {
2003-06-06 08:57:51 +00:00
RETURN_TRUE;
2003-02-12 00:45:53 +00:00
}
2003-03-08 23:33:12 +00:00
/* profiler result information */
if (MyG(profiler)) {
gettimeofday(&starttime, NULL);
prresult = (PR_RESULT *)MYSQLI_PROFILER_NEW(prquery, MYSQLI_PR_RESULT, 1);
2003-09-05 19:27:26 +00:00
} else {
prresult = NULL;
2003-03-08 23:33:12 +00:00
}
2003-02-12 00:45:53 +00:00
result = (resultmode == MYSQLI_USE_RESULT) ? mysql_use_result(mysql) : mysql_store_result(mysql);
2003-03-08 23:33:12 +00:00
if (result && MyG(profiler)) {
MYSQLI_PROFILER_ELAPSEDTIME(prresult);
prresult->rows = result->row_count;
prresult->columns = result->field_count;
}
2003-02-12 00:45:53 +00:00
if (!result) {
RETURN_FALSE;
2003-03-03 22:36:47 +00:00
}
2003-03-08 23:33:12 +00:00
mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
mysqli_resource->ptr = (void *)result;
mysqli_resource->prinfo = (void *)prresult;
MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_result_class_entry);
2003-02-12 00:45:53 +00:00
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/