#include "SQL.h"


const char * pQuery(const char * query,...)
{
	va_list argptr;
	va_start( argptr, query); 
	char	buf[4096] = "";				
	sprintf(buf,"");
	int		ret = vsprintf(buf + strlen(buf), query, argptr);
	strcpy(buf + strlen(buf), "");
	va_end( argptr );
	return buf;
}
MYSQL * mysql;
int main()
{
	//loading the config file
	IniFile cf("Config.ini");

	//mysql connection
	Log("Attempting MySQL Connection");
	mysql = mysql_init(mysql);
	MYSQL_RES *result;
	MYSQL_ROW row;
	int query_state;
	string host= cf.Value("Database","DBServer");
	string user=cf.Value("Database","LoginName");
	string pass=cf.Value("Database","Password");
	string db=cf.Value("Database","DBName");
	if (!mysql_real_connect(mysql,host.c_str(),user.c_str(),pass.c_str(),db.c_str(),NULL,NULL,NULL))
	{
		Error(mysql_error(mysql));
		Log("(!Error!) MySQL connection rejected!\n");
		mysql_close(mysql);
		getch();
		return 0;
	}
		char name[4096];
		char password[4096];
		cin >> name;
		cin >> password;
		//const char query= pQuery("sleect * from account where name=%s and password=%s",name,password);
		query_state = mysql_query(mysql, pQuery("select * from account where `name`='%s' and `password`='%s'",name,password));
		if (query_state !=0) {
		Log("Query !!Error!!");
		Error(mysql_error(mysql));
		getch();
		return 1;
		}
		result = mysql_store_result(mysql);
		while ( ( row = mysql_fetch_row(result)) != NULL ) {
			if((name==row[1]) && (password==row[2]))
			{
				Log("Loged in");
			}
			else
			{
				Log("wrong info");
			}
		}
		getch();
}

in the both cases it's say wrong info while it's right not worng also i have this err
4 IntelliSense: a trailing return type requires the 'auto' type specifier d:\c++\upgaccserver\mysql\include\mysql.h 186

anyclue :(

Recommended Answers

All 4 Replies

First post I thought I spotted an error in pQuery, since this is rather weird:

sprintf(buf,"");
int		ret = vsprintf(buf + strlen(buf)

the strlen put me off.

The real error is that you can't compare a char array with ==, there are functions to do this (e.g. strcmp, strncmp). Note that these functions return 0 when the two char arrays match.

Edit: aaand you beat me to it ;)

What happens if you do this:

cout << pQuery("select * from account where `name`='%s' and `password`='%s'",name,password) << "\n";

I very much doubt it will give you the correct output ;)

the error was becuase i can't == test char arrays


#solved

Also note that returning a pointer to a local variable is not very nice. It'll probably work in most cases.

A 'better' solution would be to make buf static, at least you can be sure then that it still exists after the pQuery function returned.
It's still not very elegant though.

Or allocate the buffer before calling pQuery, and hand it over as a parameter.

char buf[100];
if( pQuery( buf, "blabla %i", 4 ) ) {
// use buf
}
if(mysql_fetch_row(result) !=0)
		{
			while((row=mysql_fetch_row(result)) != NULL)
			{
				Log("loged in!");
			}
		}
		else
		{
			Log("no such account!");
		}

now am facing a new prob...
if the query is not ture or there is no such account it should say "no such account!"
but if there is a query it should say loged in....
but if it's true and there is a user it dont say anything it's just passing the while loop ?!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.