i'm getting an error and i think is an access problem to the class, but i'm not sure and don't know how to solve.
I create a DSN:
require_once ('DB.php');
$dsn = array(
'phptype' => 'mysql',
'username' => 'cmsconex_cms',
'password' => 'y10ba01',
'hostspec' => 'localhost',
'database' => 'cmsconex_cms',
);
$db =& DB::connect($dsn, $options);
if (DB::isError($db)) {
die($db->getMessage());
}
$db->Query("SET CHARACTER SET UTF8");
$db->Query("SET NAMES UTF8");
And in a custom class:
class user {
function get_userdata($field, $value) {
global $db;
$sql = printf ( "SELECT %s FROM usuarios WHERE %s = '%s'", $field, $field, $value );
$resultado = & $db->getAll ( $sql, DB_FETCHMODE_ASSOC );
$valor = $resultado[0][$field];
if ($value == $valor) {
return true;
} else {
return $valor;
}
}
}
The problem is when i execute a query, for example:
SELECT usuario FROM usuarios WHERE usuario = 'test'
I get the error:
SELECT usuario FROM usuarios WHERE usuario = 'test'
Fatal error: Cannot use object of type DB_Error as array in /home/cmsconex/public_html/includes/classes/user.php on line 269
Is strange, beacause if i call this other function i get no error and everyting runs great :
function logSession($usuario_id, $ip) {
global $db;
$ip = ip2long ( $ip );
$sql = "INSERT INTO accesos (id, usuario_id, fecha, pagina, ip) VALUES (0, '$usuario_id', NOW(), '', '$ip')";
$res = & $db->query ( $sql ); // Ejecutar la consulta
}
So i think the problem is when calling a method of DB class with parameters like DB_FETCHMODE_ASSOC
Could anyone show me the light please?
Thnx so muchh.
The Complete error is:
1 DB_Error Object ( [error_message_prefix] => [mode] => 1 [level] => 1024 [code] => -2 [message] => DB Error: syntax error [userinfo] => 51 [nativecode=1064 ** You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '51' at line 1] [backtrace] => Array ( [0] => Array ( [file] => /usr/local/lib/php/DB.php [line] => 966 [function] => PEAR_Error [class] => PEAR_Error [type] => -> [args] => Array ( [0] => DB Error: syntax error [1] => -2 [2] => 1 [3] => 1024 [4] => 51 [nativecode=1064 ** You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '51' at line 1] ) ) [1] => Array ( [file] => /usr/lib/php/PEAR.php [line] => 531 [function] => DB_Error [class] => DB_Error [type] => -> [object] => DB_Error Object RECURSION [args] => Array ( [0] => -2 [1] => 1 [2] => 1024 [3] => 51 [nativecode=1064 ** You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '51' at line 1] ) ) [2] => Array ( [file] => /usr/local/lib/php/DB/common.php [line] => 1903 [function] => raiseError [class] => PEAR [type] => -> [object] => DB_mysql Object ( [phptype] => mysql [dbsyntax] => mysql [features] => Array ( [limit] => alter [new_link] => 4.2.0 [numrows] => 1 [pconnect] => 1 [prepare] => [ssl] => [transactions] => 1 ) [errorcode_map] => Array ( [1004] => -15 [1005] => -15 [1006] => -15 [1007] => -5 [1008] => -17 [1022] => -5 [1044] => -26 [1046] => -14 [1048] => -3 [1049] => -27 [1050] => -5 [1051] => -18 [1054] => -19 [1061] => -5 [1062] => -5 [1064] => -2 [1091] => -4 [1100] => -21 [1136] => -22 [1142] => -26 [1146] => -18 [1216] => -3 [1217] => -3 [1356] => -13 [1451] => -3 [1452] => -3 ) [connection] => Resource id #10 [dsn] => Array ( [phptype] => mysql [dbsyntax] => mysql [username] => cmsconex_cms [password] => y10ba01 [protocol] => [hostspec] => localhost [port] => [socket] => [database] => cmsconex_cms ) [autocommit] => 1 [transaction_opcount] => 0 [_db] => cmsconex_cms [fetchmode] => 1 [fetchmode_object_class] => stdClass [was_connected] => [last_query] => 51 [options] => Array ( [result_buffering] => 500 [persistent] => 1 [ssl] => [debug] => 0 [seqname_format] => %s_seq [autofree] => [portability] => 0 [optimize] => performance ) [last_parameters] => Array ( ) [prepare_tokens] => Array ( ) [prepare_types] => Array ( ) [prepared_queries] => Array ( ) [_last_query_manip] => [_next_query_manip] => [_debug] => [_default_error_mode] => [_default_error_options] => [_default_error_handler] => [_error_class] => DB_Error [_expected_errors] => Array ( ) ) [args] => Array ( [0] => [1] => -2 [2] => [3] => [4] => 51 [nativecode=1064 ** You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '51' at line 1] [5] => DB_Error [6] => 1 ) ) [3] => Array ( [file] => /usr/local/lib/php/DB/mysql.php [line] => 898 [function] => raiseError [class] => DB_common [type] => -> [object] => DB_mysql Object ( [phptype] => mysql [dbsyntax] => mysql [features] => Array ( [limit] => alter [new_link] => 4.2.0 [numrows] => 1 [pconnect] => 1 [prepare] => [ssl] => [transactions] => 1 ) [errorcode_map] => Array ( [1004] => -15 [1005] => -15 [1006] => -15 [1007] => -5 [1008] => -17 [1022] => -5 [1044] => -26 [1046] => -14 [1048] => -3 [1049] => -27 [1050] => -5 [1051] => -18 [1054] => -19 [1061] => -5 [1062] => -5 [1064] => -2 [1091] => -4 [1100] => -21 [1136] => -22 [1142] => -26 [1146] => -18 [1216] => -3 [1217] => -3 [1356] => -13 [1451] => -3 [1452] => -3 ) [connection] => Resource id #10 [dsn] => Array ( [phptype] => mysql [dbsyntax] => mysql [username] => cmsconex_cms [password] => y10ba01 [protocol] => [hostspec] => localhost [port] => [socket] => [database] => cmsconex_cms ) [autocommit] => 1 [transaction_opcount] => 0 [_db] => cmsconex_cms [fetchmode] => 1 [fetchmode_object_class] => stdClass [was_connected] => [last_query] => 51 [options] => Array ( [result_buffering] => 500 [persistent] => 1 [ssl] => [debug] => 0 [seqname_format] => %s_seq [autofree] => [portability] => 0 [optimize] => performance ) [last_parameters] => Array ( ) [prepare_tokens] => Array ( ) [prepare_types] => Array ( ) [prepared_queries] => Array ( ) [_last_query_manip] => [_next_query_manip] => [_debug] => [_default_error_mode] => [_default_error_options] => [_default_error_handler] => [_error_class] => DB_Error [_expected_errors] => Array ( ) ) [args] => Array ( [0] => -2 [1] => [2] => [3] => [4] => 1064 ** You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '51' at line 1 ) ) [4] => Array ( [file] => /usr/local/lib/php/DB/mysql.php [line] => 327 [function] => mysqlRaiseError [class] => DB_mysql [type] => -> [object] => DB_mysql Object ( [phptype] => mysql [dbsyntax] => mysql [fe开发者_Python百科atures] => Array ( [limit] => alter [new_link] => 4.2.0 [numrows] => 1 [pconnect] => 1 [prepare] => [ssl] => [transactions] => 1 ) [errorcode_map] => Array ( [1004] => -15 [1005] => -15 [1006] => -15 [1007] => -5 [1008] => -17 [1022] => -5 [1044] => -26 [1046] => -14 [1048] => -3 [1049] => -27 [1050] => -5 [1051] => -18 [1054] => -19 [1061] => -5 [1062] => -5 [1064] => -2 [1091] => -4 [1100] => -21 [1136] => -22 [1142] => -26 [1146] => -18 [1216] => -3 [1217] => -3 [1356] => -13 [1451] => -3 [1452] => -3 ) [connection] => Resource id #10 [dsn] => Array ( [phptype] => mysql [dbsyntax] => mysql [username] => cmsconex_cms [password] => y10ba01 [protocol] => [hostspec] => localhost [port] => [socket] => [database] => cmsconex_cms ) [autocommit] => 1 [transaction_opcount] => 0 [_db] => cmsconex_cms [fetchmode] => 1 [fetchmode_object_class] => stdClass [was_connected] => [last_query] => 51 [options] => Array ( [result_buffering] => 500 [persistent] => 1 [ssl] => [debug] => 0 [seqname_format] => %s_seq [autofree] => [portability] => 0 [optimize] => performance ) [last_parameters] => Array ( ) [prepare_tokens] => Array ( ) [prepare_types] => Array ( ) [prepared_queries] => Array ( ) [_last_query_manip] => [_next_query_manip] => [_debug] => [_default_error_mode] => [_default_error_options] => [_default_error_handler] => [_error_class] => DB_Error [_expected_errors] => Array ( ) ) [args] => Array ( ) ) [5] => Array ( [file] => /usr/local/lib/php/DB/common.php [line] => 1216 [function] => simpleQuery [class] => DB_mysql [type] => -> [object] => DB_mysql Object ( [phptype] => mysql [dbsyntax] => mysql [features] => Array ( [limit] => alter [new_link] => 4.2.0 [numrows] => 1 [pconnect] => 1 [prepare] => [ssl] => [transactions] => 1 ) [errorcode_map] => Array ( [1004] => -15 [1005] => -15 [1006] => -15 [1007] => -5 [1008] => -17 [1022] => -5 [1044] => -26 [1046] => -14 [1048] => -3 [1049] => -27 [1050] => -5 [1051] => -18 [1054] => -19 [1061] => -5 [1062] => -5 [1064] => -2 [1091] => -4 [1100] => -21 [1136] => -22 [1142] => -26 [1146] => -18 [1216] => -3 [1217] => -3 [1356] => -13 [1451] => -3 [1452] => -3 ) [connection] => Resource id #10 [dsn] => Array ( [phptype] => mysql [dbsyntax] => mysql [username] => cmsconex_cms [password] => y10ba01 [protocol] => [hostspec] => localhost [port] => [socket] => [database] => cmsconex_cms ) [autocommit] => 1 [transaction_opcount] => 0 [_db] => cmsconex_cms [fetchmode] => 1 [fetchmode_object_class] => stdClass [was_connected] => [last_query] => 51 [options] => Array ( [result_buffering] => 500 [persistent] => 1 [ssl] => [debug] => 0 [seqname_format] => %s_seq [autofree] => [portability] => 0 [optimize] => performance ) [last_parameters] => Array ( ) [prepare_tokens] => Array ( ) [prepare_types] => Array ( ) [prepared_queries] => Array ( ) [_last_query_manip] => [_next_query_manip] => [_debug] => [_default_error_mode] => [_default_error_options] => [_default_error_handler] => [_error_class] => DB_Error [_expected_errors] => Array ( ) ) [args] => Array ( [0] => 51 ) ) [6] => Array ( [file] => /usr/local/lib/php/DB/common.php [line] => 1663 [function] => query [class] => DB_common [type] => -> [object] => DB_mysql Object ( [phptype] => mysql [dbsyntax] => mysql [features] => Array ( [limit] => alter [new_link] => 4.2.0 [numrows] => 1 [pconnect] => 1 [prepare] => [ssl] => [transactions] => 1 ) [errorcode_map] => Array ( [1004] => -15 [1005] => -15 [1006] => -15 [1007] => -5 [1008] => -17 [1022] => -5 [1044] => -26 [1046] => -14 [1048] => -3 [1049] => -27 [1050] => -5 [1051] => -18 [1054] => -19 [1061] => -5 [1062] => -5 [1064] => -2 [1091] => -4 [1100] => -21 [1136] => -22 [1142] => -26 [1146] => -18 [1216] => -3 [1217] => -3 [1356] => -13 [1451] => -3 [1452] => -3 ) [connection] => Resource id #10 [dsn] => Array ( [phptype] => mysql [dbsyntax] => mysql [username] => cmsconex_cms [password] => y10ba01 [protocol] => [hostspec] => localhost [port] => [socket] => [database] => cmsconex_cms ) [autocommit] => 1 [transaction_opcount] => 0 [_db] => cmsconex_cms [fetchmode] => 1 [fetchmode_object_class] => stdClass [was_connected] => [last_query] => 51 [options] => Array ( [result_buffering] => 500 [persistent] => 1 [ssl] => [debug] => 0 [seqname_format] => %s_seq [autofree] => [portability] => 0 [optimize] => performance ) [last_parameters] => Array ( ) [prepare_tokens] => Array ( ) [prepare_types] => Array ( ) [prepared_queries] => Array ( ) [_last_query_manip] => [_next_query_manip] => [_debug] => [_default_error_mode] => [_default_error_options] => [_default_error_handler] => [_error_class] => DB_Error [_expected_errors] => Array ( ) ) [args] => Array ( [0] => 51 ) ) [7] => Array ( [file] => /home/cmsconex/public_html/includes/classes/user.php [line] => 266 [function] => getAll [class] => DB_common [type] => -> [object] => DB_mysql Object ( [phptype] => mysql [dbsyntax] => mysql [features] => Array ( [limit] => alter [new_link] => 4.2.0 [numrows] => 1 [pconnect] => 1 [prepare] => [ssl] => [transactions] => 1 ) [errorcode_map] => Array ( [1004] => -15 [1005] => -15 [1006] => -15 [1007] => -5 [1008] => -17 [1022] => -5 [1044] => -26 [1046] => -14 [1048] => -3 [1049] => -27 [1050] => -5 [1051] => -18 [1054] => -19 [1061] => -5 [1062] => -5 [1064] => -2 [1091] => -4 [1100] => -21 [1136] => -22 [1142] => -26 [1146] => -18 [1216] => -3 [1217] => -3 [1356] => -13 [1451] => -3 [1452] => -3 ) [connection] => Resource id #10 [dsn] => Array ( [phptype] => mysql [dbsyntax] => mysql [username] => cmsconex_cms [password] => y10ba01 [protocol] => [hostspec] => localhost [port] => [socket] => [database] => cmsconex_cms ) [autocommit] => 1 [transaction_opcount] => 0 [_db] => cmsconex_cms [fetchmode] => 1 [fetchmode_object_class] => stdClass [was_connected] => [last_query] => 51 [options] => Array ( [result_buffering] => 500 [persistent] => 1 [ssl] => [debug] => 0 [seqname_format] => %s_seq [autofree] => [portability] => 0 [optimize] => performance ) [last_parameters] => Array ( ) [prepare_tokens] => Array ( ) [prepare_types] => Array ( ) [prepared_queries] => Array ( ) [_last_query_manip] => [_next_query_manip] => [_debug] => [_default_error_mode] => [_default_error_options] => [_default_error_handler] => [_error_class] => DB_Error [_expected_errors] => Array ( ) ) [args] => Array ( [0] => 51 [1] => 2 ) ) [8] => Array ( [file] => /home/cmsconex/public_html/includes/classes/user.php [line] => 250 [function] => get_userdata [class] => user [type] => -> [object] => user Object ( ) [args] => Array ( [0] => usuario [1] => test ) ) [9] => Array ( [file] => /home/cmsconex/public_html/gestion/usuarios-nuevo.php [line] => 39 [function] => username_exists [class] => user [type] => -> [object] => user Object ( ) [args] => Array ( [0] => test ) ) ) [callback] => ) 1
getAll
returns a DB_Error object when it fails.
You have to print to see what the error is.
According to the doc, to retrieve the error just do:
if (PEAR::isError($resultado)) {
die($resultado->getMessage());
}
getAll
needs an array as second parameter like:
$resultado = & $db->getAll ( $sql, array(), DB_FETCHMODE_ASSOC );
精彩评论