prepare() in mdb2 returning error

WebserviceMX
WebserviceMX used Ask the Experts™
on
im trying to use prepare() mdb2 pear statement this way:

$sth = $mdb2->prepare('INSERT INTO numbers (number) VALUES (?)', array('integer'), MDB2_PREPARE_MANIP);
$sth->execute(1);

and its returning this error message:

Fatal error: Call to a member function prepare() on a non-object

I don´t know what could be happening
thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
That just means that $mdb2 has not been created successfully yet. Is there code before this that initializes the connection?
Commented:
This means that $mdb2 is not referring to an open database connection.

Have you connected to the database?
somthing like this: somewhere?

$mdb2 =& MDB2::connect($dsn, $options);

Did the connect occur, but fail?

Author

Commented:
sorry it was the connection, now i´m connectig successfully, but now i got this error:

Fatal error: Call to undefined method MDB2_Error::execute()

here is my method code
	public function insert2TMPTable($data){
		global $mdb2;
		
		
		$sth = $mdb2->prepare(sprintf("INSERT INTO %s VALUES(
			:usuario_nombre,
			:usuario_apPaterno,
			:usuario_apMaterno,
			:usuario_numEmpleado,
			:usuario_nivelAcceso_id,
			:usuario_token,
			:usuario_direccionGeneral_clave,
			:usuario_clavePuesto,
			:usuario_claveDepartamento,
			:usuario_fechaNacimiento,
			:usuario_sexo,
			:usuario_claveEmpresa,
			:usuario_ciudadusu_id,
			:usuario_ubicacion,
			:usuario_nombramiento,
			:usuario_email,
			:usuario_fechaIngresoBanco,
			:usuario_enteredYet,
			:usuario_horario
			)",$this->usuariosTMPTable));
		
		foreach ($data as $v){
		$affected = $sth->execute($v);
			if (PEAR::isError($affected)) {
				die($affected->getMessage());
			}
		}
		
	}

Open in new window

Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Commented:
What happens if you run:
print_r($sth);
before the foreach() line.
Commented:
As per the first comment, even though it's a global - very naughty. It's not been instantiated prior to this point of executuion.

Author

Commented:
using print_r($sth); i could see the error

Native message: Column count doesn't match value count at row 1

now i have fixed it declaring the table fields first but now
MDB2 Error: unknown error

i´ve posted the print_r results below

thanks very much for your help
	public function insert2TMPTable($data){
		global $mdb2;
		
		
		$sth = $mdb2->prepare(sprintf("INSERT INTO %s 
		(
			usuario_nombre,
			usuario_apPaterno,
			usuario_apMaterno,
			usuario_numEmpleado,
			usuario_nivelAcceso_id,
			usuario_token,
			usuario_direccionGeneral_clave,
			usuario_clavePuesto,
			usuario_claveDepartamento,
			usuario_fechaNacimiento,
			usuario_sexo,
			usuario_claveEmpresa,
			usuario_ciudadusu_id,
			usuario_ubicacion,
			usuario_nombramiento,
			usuario_email,
			usuario_fechaIngresoBanco,
			usuario_enteredYet,
			usuario_horario)
		VALUES(
			:usuario_nombre,
			:usuario_apPaterno,
			:usuario_apMaterno,
			:usuario_numEmpleado,
			:usuario_nivelAcceso_id,
			:usuario_token,
			:usuario_direccionGeneral_clave,
			:usuario_clavePuesto,
			:usuario_claveDepartamento,
			:usuario_fechaNacimiento,
			:usuario_sexo,
			:usuario_claveEmpresa,
			:usuario_ciudadusu_id,
			:usuario_ubicacion,
			:usuario_nombramiento,
			:usuario_email,
			:usuario_fechaIngresoBanco,
			:usuario_enteredYet,
			:usuario_horario
			)",$this->usuariosTMPTable));
		
		echo '<pre>';
		print_r($sth);
		echo '</pre>';
		foreach ($data as $v){
		$affected = $sth->execute($v);
			if (PEAR::isError($affected)) {
				die($affected->getMessage());
			}
		}
		
	}
 
////////////// print_r results
 
MDB2_Statement_mysql Object
(
    [db] => MDB2_Driver_mysql Object
        (
            [string_quoting] => Array
                (
                    [start] => '
                    [end] => '
                    [escape] => \
                    [escape_pattern] => \
                )
 
            [identifier_quoting] => Array
                (
                    [start] => `
                    [end] => `
                    [escape] => `
                )
 
            [sql_comments] => Array
                (
                    [0] => Array
                        (
                            [start] => -- 
                            [end] => 
 
                            [escape] => 
                        )
 
                    [1] => Array
                        (
                            [start] => #
                            [end] => 
 
                            [escape] => 
                        )
 
                    [2] => Array
                        (
                            [start] => /*
                            [end] => */
                            [escape] => 
                        )
 
                )
 
            [start_transaction] => 1
            [varchar_max_length] => 65532
            [db_index] => 1
            [dsn] => Array
                (
                    [phptype] => mysql
                    [dbsyntax] => mysql
                    [username] => root
                    [password] => wetservits
                    [protocol] => tcp
                    [hostspec] => webserver.local
                    [port] => 
                    [socket] => 
                    [database] => 
                    [mode] => 
                )
 
            [connected_dsn] => Array
                (
                    [phptype] => mysql
                    [dbsyntax] => mysql
                    [username] => root
                    [password] => wetservits
                    [protocol] => tcp
                    [hostspec] => webserver.local
                    [port] => 
                    [socket] => 
                    [database] => 
                    [mode] => 
                )
 
            [connection] => Resource id #19
            [opened_persistent] => 1
            [database_name] => streamedia
            [connected_database_name] => streamedia
            [connected_server_info] => 5.0.45
            [supported] => Array
                (
                    [sequences] => emulated
                    [indexes] => 1
                    [affected_rows] => 1
                    [summary_functions] => 1
                    [order_by_text] => 1
                    [transactions] => 1
                    [savepoints] => 1
                    [current_id] => emulated
                    [limit_queries] => 1
                    [LOBs] => 1
                    [replace] => 1
                    [sub_selects] => 1
                    [auto_increment] => 1
                    [primary_key] => 1
                    [result_introspection] => 1
                    [prepared_statements] => 1
                    [identifier_quoting] => 1
                    [pattern_escaping] => 1
                    [new_link] => 1
                )
 
            [options] => Array
                (
                    [ssl] => 
                    [field_case] => 0
                    [disable_query] => 
                    [result_class] => MDB2_Result_%s
                    [buffered_result_class] => MDB2_BufferedResult_%s
                    [result_wrap_class] => 
                    [result_buffering] => 1
                    [fetch_class] => stdClass
                    [persistent] => 1
                    [debug] => 0
                    [debug_handler] => MDB2_defaultDebugOutput
                    [debug_expanded_output] => 
                    [default_text_field_length] => 4096
                    [lob_buffer_length] => 8192
                    [log_line_break] => 
 
                    [idxname_format] => %s_idx
                    [seqname_format] => %s_seq
                    [savepoint_format] => MDB2_SAVEPOINT_%s
                    [statement_format] => MDB2_STATEMENT_%1$s_%2$s
                    [seqcol_name] => sequence
                    [quote_identifier] => 
                    [use_transactions] => 1
                    [decimal_places] => 2
                    [portability] => 127
                    [modules] => Array
                        (
                            [ex] => Extended
                            [dt] => Datatype
                            [mg] => Manager
                            [rv] => Reverse
                            [na] => Native
                            [fc] => Function
                        )
 
                    [emulate_prepared] => 
                    [datatype_map] => Array
                        (
                        )
 
                    [datatype_map_callback] => Array
                        (
                        )
 
                    [nativetype_map_callback] => Array
                        (
                        )
 
                    [default_table_type] => 
                )
 
            [wildcards] => Array
                (
                    [0] => %
                    [1] => _
                )
 
            [as_keyword] =>  AS 
            [warnings] => Array
                (
                )
 
            [debug_output] => 
            [in_transaction] => 
            [nested_transaction_counter] => 
            [has_transaction_error] => 
            [offset] => 0
            [limit] => 0
            [phptype] => mysql
            [dbsyntax] => mysql
            [last_query] => PREPARE MDB2_STATEMENT_mysql_565cb168cc11aad62632c715df28215c FROM 'INSERT INTO usuario_empty \n		(\n			usuario_nombre,\n			usuario_apPaterno,\n			usuario_apMaterno,\n			usuario_numEmpleado,\n			usuario_nivelAcceso_id,\n			usuario_token,\n			usuario_direccionGeneral_clave,\n			usuario_clavePuesto,\n			usuario_claveDepartamento,\n			usuario_fechaNacimiento,\n			usuario_sexo,\n			usuario_claveEmpresa,\n			usuario_ciudadusu_id,\n			usuario_ubicacion,\n			usuario_nombramiento,\n			usuario_email,\n			usuario_fechaIngresoBanco,\n			usuario_enteredYet,\n			usuario_horario)\n		VALUES(\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?\n			)'
            [fetchmode] => 2
            [modules] => Array
                (
                    [Datatype] => MDB2_Driver_Datatype_mysql Object
                        (
                            [valid_default_values] => Array
                                (
                                    [text] => 
                                    [boolean] => 1
                                    [integer] => 0
                                    [decimal] => 0
                                    [float] => 0
                                    [timestamp] => 1970-01-01 00:00:00
                                    [time] => 00:00:00
                                    [date] => 1970-01-01
                                    [clob] => 
                                    [blob] => 
                                )
 
                            [lobs] => Array
                                (
                                )
 
                            [db_index] => 1
                        )
 
                )
 
            [destructor_registered] => 1
            [_debug] => 
            [_default_error_mode] => 
            [_default_error_options] => 
            [_default_error_handler] => 
            [_error_class] => PEAR_Error
            [_expected_errors] => Array
                (
                )
 
            [datatype] => MDB2_Driver_Datatype_mysql Object
                (
                    [valid_default_values] => Array
                        (
                            [text] => 
                            [boolean] => 1
                            [integer] => 0
                            [decimal] => 0
                            [float] => 0
                            [timestamp] => 1970-01-01 00:00:00
                            [time] => 00:00:00
                            [date] => 1970-01-01
                            [clob] => 
                            [blob] => 
                        )
 
                    [lobs] => Array
                        (
                        )
 
                    [db_index] => 1
                )
 
            [loaded_version_modules] => Array
                (
                    [0] => datatype
                )
 
        )
 
    [statement] => MDB2_STATEMENT_mysql_565cb168cc11aad62632c715df28215c
    [query] => PREPARE MDB2_STATEMENT_mysql_565cb168cc11aad62632c715df28215c FROM 'INSERT INTO usuario_empty \n		(\n			usuario_nombre,\n			usuario_apPaterno,\n			usuario_apMaterno,\n			usuario_numEmpleado,\n			usuario_nivelAcceso_id,\n			usuario_token,\n			usuario_direccionGeneral_clave,\n			usuario_clavePuesto,\n			usuario_claveDepartamento,\n			usuario_fechaNacimiento,\n			usuario_sexo,\n			usuario_claveEmpresa,\n			usuario_ciudadusu_id,\n			usuario_ubicacion,\n			usuario_nombramiento,\n			usuario_email,\n			usuario_fechaIngresoBanco,\n			usuario_enteredYet,\n			usuario_horario)\n		VALUES(\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?,\n			?\n			)'
    [result_types] => Array
        (
        )
 
    [types] => Array
        (
        )
 
    [values] => Array
        (
        )
 
    [limit] => 0
    [offset] => 0
    [is_manip] => 
    [positions] => Array
        (
            [498] => usuario_nombre
            [504] => usuario_apPaterno
            [510] => usuario_apMaterno
            [516] => usuario_numEmpleado
            [522] => usuario_nivelAcceso_id
            [528] => usuario_token
            [534] => usuario_direccionGeneral_clave
            [540] => usuario_clavePuesto
            [546] => usuario_claveDepartamento
            [552] => usuario_fechaNacimiento
            [558] => usuario_sexo
            [564] => usuario_claveEmpresa
            [570] => usuario_ciudadusu_id
            [576] => usuario_ubicacion
            [582] => usuario_nombramiento
            [588] => usuario_email
            [594] => usuario_fechaIngresoBanco
            [600] => usuario_enteredYet
            [606] => usuario_horario
        )
 
)

Open in new window

Author

Commented:
sorry, it was my mistake the $data array wasnt associative
in my print_r returned array it posted my mysql access data .. im changing it for security reasons jaja

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial