diff --git a/include/mysql.h b/include/mysql.h
index 5b3037d9261cc8b1779dbc6a1a80af85866afcb3..ad5250303857a28221270da51602f150d1aba402 100644
--- a/include/mysql.h
+++ b/include/mysql.h
@@ -77,6 +77,8 @@ extern char *mysql_unix_port;
 #define IS_NUM_FIELD(f)	 ((f)->flags & NUM_FLAG)
 #define INTERNAL_NUM_FIELD(f) (((f)->type <= FIELD_TYPE_INT24 && ((f)->type != FIELD_TYPE_TIMESTAMP || (f)->length == 14 || (f)->length == 8)) || (f)->type == FIELD_TYPE_YEAR)
 
+#define HAVE_DEPRECATED_411_API 1
+
 typedef struct st_mysql_field {
   char *name;                 /* Name of column */
   char *org_name;             /* Original column name, if an alias */ 
@@ -579,40 +581,60 @@ typedef struct st_mysql_methods
 #endif
 } MYSQL_METHODS;
 
+#ifdef HAVE_DEPRECATED_411_API
+/* Deprecated calls (since MySQL 4.1.2) */
+
+/* Use mysql_stmt_init + mysql_stmt_prepare instead */
 MYSQL_STMT * STDCALL mysql_prepare(MYSQL * mysql, const char *query,
 				   unsigned long length);
-int STDCALL mysql_execute(MYSQL_STMT * stmt);
-unsigned long STDCALL mysql_param_count(MYSQL_STMT * stmt);
-my_bool STDCALL mysql_bind_param(MYSQL_STMT * stmt, MYSQL_BIND * bnd);
-my_bool STDCALL mysql_bind_result(MYSQL_STMT * stmt, MYSQL_BIND * bnd);
+#define mysql_execute mysql_stmt_execute
+#define mysql_fetch mysql_stmt_fetch
+#define mysql_fetch_column mysql_stmt_fetch_column
+#define mysql_bind_param mysql_stmt_bind_param
+#define mysql_bind_result mysql_stmt_bind_result
+#define mysql_param_count mysql_stmt_param_count
+#define mysql_param_result mysql_stmt_param_metadata
+#define mysql_get_metadata mysql_stmt_result_metadata
+#define mysql_send_long_data mysql_stmt_send_long_data
+
+#endif /* HAVE_DEPRECATED_411_API */
+
+MYSQL_STMT * STDCALL mysql_stmt_init(MYSQL *mysql);
+int STDCALL mysql_stmt_prepare(MYSQL_STMT *stmt, const char *query,
+                               unsigned long length);
+int STDCALL mysql_stmt_execute(MYSQL_STMT *stmt);
+int STDCALL mysql_stmt_fetch(MYSQL_STMT *stmt);
+int STDCALL mysql_stmt_fetch_column(MYSQL_STMT *stmt, MYSQL_BIND *bind, 
+                                    unsigned int column,
+                                    unsigned long offset);
+int STDCALL mysql_stmt_store_result(MYSQL_STMT *stmt);
+unsigned long STDCALL mysql_stmt_param_count(MYSQL_STMT * stmt);
+my_bool STDCALL mysql_stmt_bind_param(MYSQL_STMT * stmt, MYSQL_BIND * bnd);
+my_bool STDCALL mysql_stmt_bind_result(MYSQL_STMT * stmt, MYSQL_BIND * bnd);
 my_bool STDCALL mysql_stmt_close(MYSQL_STMT * stmt);
 my_bool STDCALL mysql_stmt_reset(MYSQL_STMT * stmt);
 my_bool STDCALL mysql_stmt_free_result(MYSQL_STMT *stmt);
+my_bool STDCALL mysql_stmt_send_long_data(MYSQL_STMT *stmt, 
+                                          unsigned int param_number,
+                                          const char *data, 
+                                          unsigned long length);
+MYSQL_RES *STDCALL mysql_stmt_result_metadata(MYSQL_STMT *stmt);
+MYSQL_RES *STDCALL mysql_stmt_param_metadata(MYSQL_STMT *stmt);
 unsigned int STDCALL mysql_stmt_errno(MYSQL_STMT * stmt);
 const char *STDCALL mysql_stmt_error(MYSQL_STMT * stmt);
 const char *STDCALL mysql_stmt_sqlstate(MYSQL_STMT * stmt);
-my_bool STDCALL mysql_commit(MYSQL * mysql);
-my_bool STDCALL mysql_rollback(MYSQL * mysql);
-my_bool STDCALL mysql_autocommit(MYSQL * mysql, my_bool auto_mode);
-int	STDCALL mysql_fetch(MYSQL_STMT *stmt);
-int STDCALL mysql_fetch_column(MYSQL_STMT *stmt, MYSQL_BIND *bind, 
-                               unsigned int column,
-                               unsigned long offset);
-my_bool STDCALL mysql_send_long_data(MYSQL_STMT *stmt, 
-				     unsigned int param_number,
-				     const char *data, 
-				     unsigned long length);
-MYSQL_RES *STDCALL mysql_get_metadata(MYSQL_STMT *stmt);
-MYSQL_RES *STDCALL mysql_param_result(MYSQL_STMT *stmt);
-my_ulonglong STDCALL mysql_stmt_affected_rows(MYSQL_STMT *stmt);
-int STDCALL mysql_stmt_store_result(MYSQL_STMT *stmt);
-my_bool STDCALL mysql_more_results(MYSQL *mysql);
-int STDCALL mysql_next_result(MYSQL *mysql);
 MYSQL_ROW_OFFSET STDCALL mysql_stmt_row_seek(MYSQL_STMT *stmt, 
                                              MYSQL_ROW_OFFSET offset);
 MYSQL_ROW_OFFSET STDCALL mysql_stmt_row_tell(MYSQL_STMT *stmt);
 void STDCALL mysql_stmt_data_seek(MYSQL_STMT *stmt, my_ulonglong offset);
 my_ulonglong STDCALL mysql_stmt_num_rows(MYSQL_STMT *stmt);
+my_ulonglong STDCALL mysql_stmt_affected_rows(MYSQL_STMT *stmt);
+
+my_bool STDCALL mysql_commit(MYSQL * mysql);
+my_bool STDCALL mysql_rollback(MYSQL * mysql);
+my_bool STDCALL mysql_autocommit(MYSQL * mysql, my_bool auto_mode);
+my_bool STDCALL mysql_more_results(MYSQL *mysql);
+int STDCALL mysql_next_result(MYSQL *mysql);
 void STDCALL mysql_close(MYSQL *sock);
 
 
diff --git a/libmysql/client_settings.h b/libmysql/client_settings.h
index 5ce0e021782a693deda7cc3c9db5753ef9379570..9d57bdbca0e76be45f68c764ae9dd536655e6a98 100644
--- a/libmysql/client_settings.h
+++ b/libmysql/client_settings.h
@@ -22,7 +22,7 @@ extern my_string	mysql_unix_port;
 			     CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION)
 
 sig_handler pipe_sig_handler(int sig __attribute__((unused)));
-my_bool stmt_close(MYSQL_STMT *stmt, my_bool skip_list, my_bool skip_free);
+my_bool stmt_close(MYSQL_STMT *stmt, my_bool skip_free);
 void read_user_name(char *name);
 my_bool send_file_to_server(MYSQL *mysql, const char *filename);
 
diff --git a/libmysql/libmysql.c b/libmysql/libmysql.c
index e88e34f8925b31bbc12c0eac92bc984a5340992b..c54f2044a3f4f0fe1f2ff160479f3d9510913afc 100644
--- a/libmysql/libmysql.c
+++ b/libmysql/libmysql.c
@@ -89,7 +89,6 @@ static void append_wild(char *to,char *end,const char *wild);
 sig_handler pipe_sig_handler(int sig);
 static ulong mysql_sub_escape_string(CHARSET_INFO *charset_info, char *to,
 				     const char *from, ulong length);
-my_bool stmt_close(MYSQL_STMT *stmt, my_bool skip_list, my_bool skip_free);
 
 static my_bool mysql_client_init= 0;
 static my_bool org_my_init_done= 0;
@@ -1640,43 +1639,88 @@ my_bool cli_read_prepare_result(MYSQL *mysql, MYSQL_STMT *stmt)
   DBUG_RETURN(0);
 }
 
+#ifdef HAVE_DEPRECATED_411_API
+MYSQL_STMT * STDCALL mysql_prepare(MYSQL *mysql, const char *query,
+                                   unsigned long query_length)
+{
+  DBUG_ENTER("mysql_prepare");
+
+  MYSQL_STMT *stmt= mysql_stmt_init(mysql);
+  if (stmt && mysql_stmt_prepare(stmt, query, query_length))
+  {
+    stmt_close(stmt, 0);
+    DBUG_RETURN(0);
+  }
+  DBUG_RETURN(stmt);
+}
+#endif
+
+/*
+  Allocate memory and init prepared statement structure
+  SYNOPSIS
+    mysql_stmt_init()
+    mysql connection handle
+  RETURN VALUE
+    statement structure upon success and NULL if out of
+    memory
+*/
+
+MYSQL_STMT * STDCALL
+mysql_stmt_init(MYSQL *mysql)
+{
+  MYSQL_STMT *stmt;
+  DBUG_ENTER("mysql_stmt_init");
+
+  if (!(stmt= (MYSQL_STMT *) my_malloc(sizeof(MYSQL_STMT),
+                                       MYF(MY_WME | MY_ZEROFILL))))
+  {
+    set_mysql_error(mysql, CR_OUT_OF_MEMORY, unknown_sqlstate);
+    DBUG_RETURN(0);
+  }
+
+  init_alloc_root(&stmt->mem_root,8192,0);
+  mysql->stmts= list_add(mysql->stmts, &stmt->list);
+  stmt->list.data= stmt;
+  stmt->state= MY_ST_UNKNOWN;
+  stmt->mysql= mysql;
+
+  DBUG_RETURN(stmt);
+}
 
 /*
-  Prepare the query and return the new statement handle to
-  caller.
+  Prepare server side statement with query.
 
   Also update the total parameter count along with resultset
   metadata information by reading from server
 */
 
 
-MYSQL_STMT *STDCALL
-mysql_prepare(MYSQL  *mysql, const char *query, ulong length)
+int STDCALL
+mysql_stmt_prepare(MYSQL_STMT *stmt, const char *query, ulong length)
 {
-  MYSQL_STMT  *stmt;
-  DBUG_ENTER("mysql_prepare");
+  MYSQL *mysql= stmt->mysql;
+  DBUG_ENTER("mysql_stmt_prepare");
+
   DBUG_ASSERT(mysql != 0);
 
-  if (!(stmt= (MYSQL_STMT *) my_malloc(sizeof(MYSQL_STMT),
-				       MYF(MY_WME | MY_ZEROFILL))) ||
-      !(stmt->query= my_strdup_with_length((byte *) query, length, MYF(0))))
+  /* In case we reprepare this handle with another statement */
+  free_root(&stmt->mem_root, MYF(0));
+
+  /* stmt->query is never used yet */
+  if (!(stmt->query= strmake_root(&stmt->mem_root, query, length)))
   {
-    my_free((gptr) stmt, MYF(MY_ALLOW_ZERO_PTR));
-    set_mysql_error(mysql, CR_OUT_OF_MEMORY, unknown_sqlstate);
-    DBUG_RETURN(0);
+    set_stmt_error(stmt, CR_OUT_OF_MEMORY, unknown_sqlstate);
+    DBUG_RETURN(1);
   }
   if (simple_command(mysql, COM_PREPARE, query, length, 1))
   {
-    stmt_close(stmt, 1, 0);
-    DBUG_RETURN(0);
+    set_stmt_errmsg(stmt, mysql->net.last_error, mysql->net.last_errno,
+                    mysql->net.sqlstate);
+    DBUG_RETURN(1);
   }
 
-  init_alloc_root(&stmt->mem_root,8192,0);
   if ((*mysql->methods->read_prepare_result)(mysql, stmt))
-  {
-    stmt_close(stmt, 1, 0);
-    DBUG_RETURN(0);
-  }
+    DBUG_RETURN(1);
 
   if (!(stmt->params= (MYSQL_BIND *) alloc_root(&stmt->mem_root,
 						sizeof(MYSQL_BIND)*
@@ -1684,16 +1728,13 @@ mysql_prepare(MYSQL  *mysql, const char *query, ulong length)
                                                  stmt->field_count))))
   {
     set_stmt_error(stmt, CR_OUT_OF_MEMORY, unknown_sqlstate);
-    DBUG_RETURN(0);
+    DBUG_RETURN(1);
   }
   stmt->bind= stmt->params + stmt->param_count;
   stmt->state= MY_ST_PREPARE;
-  stmt->mysql= mysql;
-  mysql->stmts= list_add(mysql->stmts, &stmt->list);
   mysql->status= MYSQL_STATUS_READY;
-  stmt->list.data= stmt;
   DBUG_PRINT("info", ("Parameter count: %ld", stmt->param_count));
-  DBUG_RETURN(stmt);
+  DBUG_RETURN(0);
 }
 
 /*
@@ -1750,10 +1791,10 @@ unsigned int alloc_stmt_fields(MYSQL_STMT *stmt)
 */
 
 MYSQL_RES * STDCALL
-mysql_get_metadata(MYSQL_STMT *stmt)
+mysql_stmt_result_metadata(MYSQL_STMT *stmt)
 {
   MYSQL_RES *result;
-  DBUG_ENTER("mysql_get_metadata");
+  DBUG_ENTER("mysql_stmt_result_metadata");
   
   if (!stmt->field_count || !stmt->fields)
   {
@@ -1778,9 +1819,9 @@ mysql_get_metadata(MYSQL_STMT *stmt)
 */
 
 MYSQL_RES * STDCALL
-mysql_param_result(MYSQL_STMT *stmt)
+mysql_stmt_param_metadata(MYSQL_STMT *stmt)
 {
-  DBUG_ENTER("mysql_param_result");
+  DBUG_ENTER("mysql_stmt_param_metadata");
   
   if (!stmt->param_count)
     DBUG_RETURN(0);
@@ -2079,9 +2120,9 @@ int cli_stmt_execute(MYSQL_STMT *stmt)
   Execute the prepared query
 */
 
-int STDCALL mysql_execute(MYSQL_STMT *stmt)
+int STDCALL mysql_stmt_execute(MYSQL_STMT *stmt)
 {
-  DBUG_ENTER("mysql_execute");
+  DBUG_ENTER("mysql_stmt_execute");
 
   if ((*stmt->mysql->methods->stmt_execute)(stmt))
     DBUG_RETURN(1);
@@ -2099,9 +2140,9 @@ int STDCALL mysql_execute(MYSQL_STMT *stmt)
   Return total parameters count in the statement
 */
 
-ulong STDCALL mysql_param_count(MYSQL_STMT * stmt)
+ulong STDCALL mysql_stmt_param_count(MYSQL_STMT * stmt)
 {
-  DBUG_ENTER("mysql_param_count");
+  DBUG_ENTER("mysql_stmt_param_count");
   DBUG_RETURN(stmt->param_count);
 }
 
@@ -2122,11 +2163,11 @@ static my_bool int_is_null_false= 0;
   Setup the parameter data buffers from application
 */
 
-my_bool STDCALL mysql_bind_param(MYSQL_STMT *stmt, MYSQL_BIND * bind)
+my_bool STDCALL mysql_stmt_bind_param(MYSQL_STMT *stmt, MYSQL_BIND * bind)
 {
   uint count=0;
   MYSQL_BIND *param, *end;
-  DBUG_ENTER("mysql_bind_param");
+  DBUG_ENTER("mysql_stmt_bind_param");
 
   /* Allocated on prepare */
   memcpy((char*) stmt->params, (char*) bind,
@@ -2228,7 +2269,7 @@ my_bool STDCALL mysql_bind_param(MYSQL_STMT *stmt, MYSQL_BIND * bind)
   Send long data in pieces to the server
 
   SYNOPSIS
-    mysql_send_long_data()
+    mysql_stmt_send_long_data()
     stmt			Statement handler
     param_number		Parameter number (0 - N-1)
     data			Data to send to server
@@ -2241,11 +2282,11 @@ my_bool STDCALL mysql_bind_param(MYSQL_STMT *stmt, MYSQL_BIND * bind)
 
 
 my_bool STDCALL
-mysql_send_long_data(MYSQL_STMT *stmt, uint param_number,
+mysql_stmt_send_long_data(MYSQL_STMT *stmt, uint param_number,
 		     const char *data, ulong length)
 {
   MYSQL_BIND *param;
-  DBUG_ENTER("mysql_send_long_data");
+  DBUG_ENTER("mysql_stmt_send_long_data");
   DBUG_ASSERT(stmt != 0);
   DBUG_PRINT("enter",("param no : %d, data : %lx, length : %ld",
 		      param_number, data, length));
@@ -2811,12 +2852,12 @@ static void fetch_result_str(MYSQL_BIND *param, uchar **row)
   Setup the bind buffers for resultset processing
 */
 
-my_bool STDCALL mysql_bind_result(MYSQL_STMT *stmt, MYSQL_BIND *bind)
+my_bool STDCALL mysql_stmt_bind_result(MYSQL_STMT *stmt, MYSQL_BIND *bind)
 {
   MYSQL_BIND *param, *end;
   ulong       bind_count;
   uint        param_count= 0;
-  DBUG_ENTER("mysql_bind_result");
+  DBUG_ENTER("mysql_stmt_bind_result");
   DBUG_ASSERT(stmt != 0);
 
   if (!(bind_count= stmt->field_count) && 
@@ -2965,11 +3006,11 @@ int cli_unbuffered_fetch(MYSQL *mysql, char **row)
   Fetch and return row data to bound buffers, if any
 */
 
-int STDCALL mysql_fetch(MYSQL_STMT *stmt)
+int STDCALL mysql_stmt_fetch(MYSQL_STMT *stmt)
 {
   MYSQL *mysql= stmt->mysql;
   uchar *row;
-  DBUG_ENTER("mysql_fetch");
+  DBUG_ENTER("mysql_stmt_fetch");
 
   stmt->last_fetched_column= 0;			/* reset */
   if (stmt->result_buffered)			/* buffered */
@@ -3025,10 +3066,10 @@ no_data:
   Fetch data for one specified column data
 
   SYNOPSIS
-    mysql_fetch_column()
+    mysql_stmt_fetch_column()
     stmt		Prepared statement handler
     bind		Where data should be placed. Should be filled in as
-			when calling mysql_bind_result()
+			when calling mysql_stmt_bind_result()
     column		Column to fetch (first column is 0)
     ulong offset	Offset in result data (to fetch blob in pieces)
 			This is normally 0
@@ -3037,11 +3078,11 @@ no_data:
     1	error
 */
 
-int STDCALL mysql_fetch_column(MYSQL_STMT *stmt, MYSQL_BIND *bind, 
+int STDCALL mysql_stmt_fetch_column(MYSQL_STMT *stmt, MYSQL_BIND *bind, 
                                uint column, ulong offset)
 {
   MYSQL_BIND *param= stmt->bind+column; 
-  DBUG_ENTER("mysql_fetch_column");
+  DBUG_ENTER("mysql_stmt_fetch_column");
 
   if (!stmt->current_row)
     goto no_data;
@@ -3183,7 +3224,7 @@ int STDCALL mysql_stmt_store_result(MYSQL_STMT *stmt)
   result->fields=	stmt->fields;
   result->field_count=	stmt->field_count;
   stmt->result= result;
-  DBUG_RETURN(0); /* Data buffered, must be fetched with mysql_fetch() */
+  DBUG_RETURN(0); /* Data buffered, must be fetched with mysql_stmt_fetch() */
 }
 
 
@@ -3275,7 +3316,7 @@ my_ulonglong STDCALL mysql_stmt_num_rows(MYSQL_STMT *stmt)
   Close the statement handle by freeing all alloced resources
 
   SYNOPSIS
-    mysql_stmt_close()
+    mysql_stmt_free_result()
     stmt	       Statement handle
     skip_list    Flag to indicate delete from list or not
   RETURN VALUES
@@ -3313,10 +3354,10 @@ my_bool STDCALL mysql_stmt_free_result(MYSQL_STMT *stmt)
 }
 
 
-my_bool stmt_close(MYSQL_STMT *stmt, my_bool skip_list, my_bool skip_free)
+my_bool stmt_close(MYSQL_STMT *stmt, my_bool skip_free)
 {
   MYSQL *mysql;
-  DBUG_ENTER("mysql_stmt_close");
+  DBUG_ENTER("stmt_close");
 
   DBUG_ASSERT(stmt != 0);
   
@@ -3341,10 +3382,8 @@ my_bool stmt_close(MYSQL_STMT *stmt, my_bool skip_list, my_bool skip_free)
   }
   stmt->field_count= 0;
   free_root(&stmt->mem_root, MYF(0));
-  if (!skip_list)
-    mysql->stmts= list_delete(mysql->stmts, &stmt->list);
+  mysql->stmts= list_delete(mysql->stmts, &stmt->list);
   mysql->status= MYSQL_STATUS_READY;
-  my_free((gptr) stmt->query, MYF(MY_WME));
   my_free((gptr) stmt, MYF(MY_WME));
   DBUG_RETURN(0);
 }
@@ -3352,7 +3391,7 @@ my_bool stmt_close(MYSQL_STMT *stmt, my_bool skip_list, my_bool skip_free)
 
 my_bool STDCALL mysql_stmt_close(MYSQL_STMT *stmt)
 {
-  return stmt_close(stmt, 0, 0);
+  return stmt_close(stmt, 0);
 }
 
 /*
diff --git a/sql-common/client.c b/sql-common/client.c
index a22ac22f08a3939c05443ba02af85642f416a238..1425275443ca6fcf31c3ee8100e8022f8d7766d6 100644
--- a/sql-common/client.c
+++ b/sql-common/client.c
@@ -2199,7 +2199,7 @@ void STDCALL mysql_close(MYSQL *mysql)
       for (element= mysql->stmts; element; element= next_element)
       {
         next_element= element->next;
-        stmt_close((MYSQL_STMT *)element->data, 0, 1);
+        stmt_close((MYSQL_STMT *)element->data, 1);
       }
       mysql->stmts= 0;
     }
diff --git a/tests/client_test.c b/tests/client_test.c
index fa0b65c2928a41c4e1a03ea982622fdfd9258a5f..fae8c3cd54d574d35d2fc778a34071a4e8f41fb3 100644
--- a/tests/client_test.c
+++ b/tests/client_test.c
@@ -2963,6 +2963,8 @@ static void test_bind_result_ext()
   fprintf(stdout, "\n data (double) : %f", d_data);
 
   fprintf(stdout, "\n data (str)    : %s(%lu)", szData, szLength);
+
+  bData[bLength]= '\0';                         /* bData is binary */
   fprintf(stdout, "\n data (bin)    : %s(%lu)", bData, bLength);
 
 
@@ -3939,6 +3941,7 @@ static void test_prepare_resultset()
   result = mysql_get_metadata(stmt);
   mytest(result);
   my_print_result_metadata(result);
+  mysql_free_result(result);
   mysql_stmt_close(stmt);
 }
 
@@ -4070,18 +4073,18 @@ static void test_stmt_close()
   fprintf(stdout,"\n mysql_close_stmt(1) returned: %d", rc);
   assert(rc == 0);
   
-  mysql_close(lmysql); /* it should free all open stmts(stmt3, 2 and 1) */
+  /*
+    Originally we were going to close all statements automatically in
+    mysql_close(). This proved to not work well - users weren't able to
+    close statements by hand once mysql_close() had been called.
+    Now mysql_close() doesn't free any statements, so this test doesn't
+    serve its original destination any more. 
+    Here we free stmt2 and stmt3 by hande to avoid memory leaks.
+  */
+  mysql_stmt_close(stmt2);
+  mysql_stmt_close(stmt3);
+  mysql_close(lmysql);
  
-#if NOT_VALID
-  rc= mysql_stmt_close(stmt3);
-  fprintf(stdout,"\n mysql_close_stmt(3) returned: %d", rc);
-  assert( rc == 1);
-  
-  rc= mysql_stmt_close(stmt2);
-  fprintf(stdout,"\n mysql_close_stmt(2) returned: %d", rc);
-  assert( rc == 1);
-#endif
-
   count= 100;
   bind[0].buffer=(char *)&count;
   bind[0].buffer_type=MYSQL_TYPE_LONG;
@@ -4871,7 +4874,10 @@ DROP TABLE IF EXISTS test_multi_tab";
   {
     fprintf(stdout,"\n Query %d: ", count);
     if ((result= mysql_store_result(mysql_local)))
+    {
       my_process_result_set(result);
+      mysql_free_result(result);
+    }
     else
       fprintf(stdout,"OK, %lld row(s) affected, %d warning(s)\n",
 	      mysql_affected_rows(mysql_local),
@@ -5768,6 +5774,7 @@ static void test_open_direct()
   mytest(result);
 
   assert(0 == my_process_result_set(result));
+  mysql_free_result(result);
 
   rc = mysql_execute(stmt);
   mystmt(stmt, rc);
@@ -5781,6 +5788,7 @@ static void test_open_direct()
   mytest(result);
 
   assert(1 == my_process_result_set(result));
+  mysql_free_result(result);
 
   rc = mysql_execute(stmt);
   mystmt(stmt, rc);
@@ -5794,6 +5802,8 @@ static void test_open_direct()
   mytest(result);
 
   assert(2 == my_process_result_set(result));
+  mysql_free_result(result);
+
   mysql_stmt_close(stmt);
 
   /* run a direct query in the middle of a fetch */
@@ -6446,6 +6456,7 @@ static void test_prepare_grant()
     
     assert(4 == my_stmt_result("SELECT * FROM test_grant"));
     
+    mysql_stmt_close(stmt);
     mysql_close(lmysql);        
     mysql= org_mysql;
 
@@ -7552,6 +7563,8 @@ static void test_mem_overun()
   rc = mysql_fetch(stmt);
   assert(rc == MYSQL_NO_DATA);
 
+  mysql_free_result(field_res);
+
   mysql_stmt_close(stmt);
 }
 
@@ -7876,6 +7889,7 @@ static void test_ts()
   ts.hour= 21;
   ts.minute= 07;
   ts.second= 46;
+  ts.second_part= 0;
   length= (long)(strmov(strts,"2003-07-12 21:07:46") - strts);
 
   bind[0].buffer_type= MYSQL_TYPE_TIMESTAMP;
@@ -7984,6 +7998,8 @@ static void test_bug1500()
   bind[0].buffer= (char *)int_data;
   bind[0].buffer_type= FIELD_TYPE_LONG;
   bind[0].is_null= 0;
+  bind[0].length= NULL;
+  bind[0].buffer_length= 0;
   bind[2]= bind[1]= bind[0];
   bind[1].buffer= (char *)(int_data + 1);
   bind[2].buffer= (char *)(int_data + 2);
@@ -8422,9 +8438,8 @@ get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
 static void get_options(int argc, char **argv)
 {
   int ho_error;
-  load_defaults("my",client_test_load_default_groups,&argc,&argv);
 
-  if ((ho_error= handle_options(&argc,&argv, client_test_long_options, 
+  if ((ho_error= handle_options(&argc, &argv, client_test_long_options, 
                                 get_one_option)))
     exit(ho_error);
 
@@ -8454,6 +8469,7 @@ static void print_test_output()
 *********************************************************/
 int main(int argc, char **argv)
 {  
+  DEBUGGER_OFF;
   MY_INIT(argv[0]);
 
   load_defaults("my",client_test_load_default_groups,&argc,&argv);
@@ -8597,6 +8613,7 @@ int main(int argc, char **argv)
   client_disconnect();    /* disconnect from server */
   free_defaults(defaults_argv);
   print_test_output();
-  
+  my_end(0);
+
   return(0);
 }