CAPI: mysql_bind_results doesn"t work
am 24.04.2003 06:18:48 von Kevin Fredericksen------=_NextPart_000_0005_01C309EE.B17754C0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
I can't seem to get mysql_bind_results to return string, date, or char =
values, although I was able to get integer values passed back properly. =
Following is a simple case using a single colume table and the code =
straight from the example in the mysql_fetch documentation. Well, OK, =
that's a stretch, since that little example has several syntax issues as =
well as mismatched argument types ... In any event, here's my system =
information, the code sample, and process to reproduce the error:=20
(localhost)> rpm -qa | grep -i mysql
MySQL-devel-4.1.0-0
MySQL-client-4.1.0-0
MySQL-shared-4.1.0-0
MySQL-server-4.1.0-0
(localhost)> uname -a
Linux localhost 2.4.18-14 #1 Wed Sep 4 13:35:50 EDT 2002 i686 i686 i386 =
GNU/Linux
Start by creating a database "IS_dev", and then create a table in it =
called "names" with a single column "name" that is a varchar(20). I =
inserted my name "Kevin" into this table. I then compiled the following =
code using the command:
(localhost)> cc -g foo1.c -L/usr/lib/mysql -lz -lmysqlclient -o foo1=20
#include
#include
#include
main()
{
MYSQL mysql, *sock;
MYSQL_STMT *stmt;
MYSQL_BIND bind[2];
MYSQL_RES *result;
int int_data;
long int_length, str_length;
char str_data[50], *query;
my_bool is_null[2];
mysql_init(&mysql);
sock =3D mysql_real_connect(&mysql, NULL, "root", "dbadmin",
"IS_dev", 0, NULL, 0);
if(!sock)
{
fprintf(stderr, "failed connecting to db: %s\n", strerror(errno));
exit(-1);
}
query=3D "SELECT * FROM names";
if (!(stmt=3D mysql_prepare(&mysql, query, strlen(query))))
{
fprintf(stderr, "\n prepare failed");
fprintf(stderr, "\n %s", mysql_error(&mysql));
exit(0);
}
/* Get the fields meta information */
if (!(result=3D mysql_prepare_result(stmt)))
{
fprintf(stderr, "\n prepare_result failed");
fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
exit(0);
}
fprintf(stdout, "Total fields: %ld\n", mysql_num_fields(result));
if (mysql_num_fields(result) !=3D 1)
{
fprintf(stderr, "prepare returned invalid field count\n");
exit(0);
}
/* Execute the SELECT query */
if (mysql_execute(stmt))
{
fprintf(stderr, "execute failed: %s\n", mysql_stmt_error(stmt));
exit(0);
}
/* Bind the result data buffers */
bind[0].buffer_type =3D 253;
bind[0].buffer=3D (void *)str_data;
bind[0].is_null=3D &is_null[0];
bind[0].length=3D &str_length;
if (mysql_bind_result(stmt, bind))
{
fprintf(stderr, "bind_result failed: %s\n", mysql_stmt_error(stmt));
exit(0);
}
printf("bound results\n");
/* Now fetch data to buffers */
if (mysql_fetch(stmt))
{
fprintf(stderr, "fetch failed: %s\n", mysql_stmt_error(stmt));
exit(0);
}
printf("fetched results\n");
fprintf(stdout, "Col1: %s, length: %ld\n", str_data, str_length);
/* Free the prepare result meta information */
mysql_free_result(result);
}
Now run the program foo1:
(localhost)> ./foo1
Total fields: 1
bound results
fetched results
Col1: , length: 5
You'll note that the string didn't get passed back, although the length =
value is set correctly. Is this fixed in the development tree?=20
------=_NextPart_000_0005_01C309EE.B17754C0--