Bug #15266 mysql_real_connect failed connection
Submitted: 27 Nov 2005 9:51 Modified: 27 Nov 2005 14:48
Reporter: gian enrico conti Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:5.0 OS:Windows (XP 64 bit on xeon)
Assigned to: CPU Architecture:Any

[27 Nov 2005 9:51] gian enrico conti
Description:
I did:

myData = mysql_real_connect(myData,
	"192.168.2.18",	// NULL = localhost
	"root",
	"root",
	"",	// if not NULL select the db
	MYSQL_PORT,
	NULL,
	0
	);

	int err = mysql_errno(myData);
	char  * s_err = mysql_error(myData);
	printf("%s\n", s_err);

if connection succeds, OK, You can use err ansd s_err, BUT
if connections fails, err is a meaningless value and WORSE, s_err points to garbage and GPF!

How to repeat:
simply buldla new MSVC 6.0 OR visual studio 2005 project and paste code.
Thena make "C" point to a non-existing mysqlserver.

Suggested fix:
fix behaviour of mysql functions:

mysql_errno
mysql_error

if connection is invlaid
[27 Nov 2005 10:01] Valeriy Kravchuk
Thank you for a problem report. Please, specify the exact version of MySQL used (5.0 is not enough) and what distribution package(s) you installed.
[27 Nov 2005 14:38] gian enrico conti
I see under mysql ADMIN: 
server version: 5.015-nt-max via TCP

I use the LIB and DLL from the complete install from the same package of  5.015-nt-max via TCP
I fixed a bit doing in ths way:

	MYSQL conn;
	mysql_init(&conn);
	
	MYSQL * connPtr = mysql_real_connect(&conn,NULL,"root","root","test",0,NULL,0);
	if (connPtr== NULL)
	{
		fprintf(stderr, "Failed to connect to database: Error: %s\n", mysql_error(&conn));
		getch();
		return 1;
	}

USING a local struct instead of a pointer to strucnt ( allocated by the library..) 
i got a correct result.....

(by the way mysql Admin incoretly reports I am using win2003 server instead of XP PRO 64)
(see attach)
[27 Nov 2005 14:48] Valeriy Kravchuk
The solution you found is a clearly documented way to use mysql_real_connect (http://dev.mysql.com/doc/refman/5.0/en/mysql-real-connect.html):

"The first parameter should be the address of an existing MYSQL structure. Before calling mysql_real_connect() you must call mysql_init() to initialize the MYSQL structure. You can change a lot of connect options with the mysql_options() call. See Section 22.2.3.48, “mysql_options()”."

As for a problem with MySQL Adminstrator showing incorrect Windows version, you may open a separate bug report, if you want.