I have a question, for example I input something on edit and in that time when I'll click on search button it will show find message, but it shows mysql error :'( why? what I am doing wrong in this code? :(

HWND edit;
char t[MAX_PATH];
char query[MAX_PATH];
GetWindowText(edit,t,MAX_PATH);
mysql_query(conn, "CREATE TABLE writers(name VARCHAR(25))");
sprintf(query, "SELECT * FROM writers WHERE name = '%s'", t);
result1 = mysql_query(conn, "SELECT * FROM writers WHERE name = t");
mysql_query(conn, query);
if(result1 == 0)
{
result = mysql_store_result(conn);
if (mysql_num_rows(result) == 0)
{
MessageBox(0,"not find",0,0);
}
else
{
MessageBox(0,"find",0,0);
}
}
else
{
MessageBox(0,"mysql error",0,0);
}

for more info now see this image
http://d221127.u-telcom.net/lashatt/mysqleditsearcherror.JPG

Recommended Answers

All 18 Replies

My guess is that you are trying to create the table every time you run that program. Delete line 5 and try it again.

u mean to delete this?
mysql_query(conn, "CREATE TABLE writers(name VARCHAR(25))");
I have delete this but it shows the same error :(. please someone help me :(

Maybe the following takes you a step further ...

// Note that you have to get the following window handle ('edit')
// somewhere. If you use an uninitialized handle like below,
// then GetWindowText() fails.
HWND edit;
char t[MAX_PATH];
char query[MAX_PATH];

// Try to get the window text
if( ! GetWindowText(edit,t,MAX_PATH))
{
    // What's the error?
    printf("GetWindowText() failed, error is [%lu]\n", GetLastError());
    // No use to continue ...
    return;
}

//// As Ancient Dragon said, don't create the table every time ...
// mysql_query(conn, "CREATE TABLE writers(name VARCHAR(25))");
// However, you must be sure that it exists, does it exist in the db?

sprintf(query, "SELECT * FROM writers WHERE name = '%s'", t);

// How does the query look like now?
printf("Query is: [%s]\n", query);

//// You were using 'WHERE name = t', use the actual query instead
// result1 = mysql_query(conn, "SELECT * FROM writers WHERE name = t");

// Here 'conn' is used, are you sure that you have a valid
// connection?
result1 = mysql_query(conn, query);

if(result1 == 0)
{
    result = mysql_store_result(conn);

    if (mysql_num_rows(result) == 0)
    {
        MessageBox(0,"not find",0,0);
    }
    else 
    {
        MessageBox(0,"find",0,0);
    }
}
else 
{
    // Query failed, see what mysql says, use mysql_error()
    const char * errMsg = mysql_error(conn);
	
    // Check that non-null/non-zero-length string wasn't received
    if(errMsg && *errMsg)
    {
        // Some error msg received, display it
        MessageBox(0, errMsg, "query failed", 0);
    }
    else
    {
        // No luck with the mysql error msg ...
        MessageBox(0,"mysql query failed","error",0);
    }
}

Duh, sorry about using printf() . Switch those printf()'s to be along the lines of

char buf[MAX_PATH];
sprintf(buf, "GetWindowText() failed, error is [%lu]", GetLastError());
MessageBox(0, buf, "error", MB_ICONSTOP);

>> MessageBox(0, errMsg, "query failed", 0);

It would be helpful to know what the error message says.

>> MessageBox(0, errMsg, "query failed", 0);

It would be helpful to know what the error message says.

For the connection specified by mysql, mysql_error() returns a null-terminated string containing the error message for the most recently invoked API function that failed. If a function didn't fail, the return value of mysql_error() may be the previous error or an empty string to indicate no error.

So the error message, if there's one, should be displayed (I think).

MITRMKAR
I do like you posted but it already says mysql error. please help me :(
when I am doing like this
result1 = mysql_query(conn, "SELECT * FROM writers WHERE name = 'something'");
and when I click search button it says find.

Could you post your new code, that might clear things up. Also try to point out where things go wrong in the code (as closely as you can).

MITRMKAR
here is the project:
<<URL to VIRUS-INFECTED ZIP removed...>>

MITRMKAR
here is the project:
<<URL to VIRUS-INFECTED ZIP removed...>>

Lashatt2: Your computer is infected with a virus. The archive you uploaded contained it. You can go to our viruses & nasties to get it removed. Be sure to add the link I gave you to your question.

MITRMKAR
here is the project:

What I was trying to say in the post #4 above, is that you have to learn to use the MySQL API functions correctly. The API is well documented in C API Function Overview (that's for version 5.5, use the documentation that's relevant to your library).

So when you use the mysql*() functions, be sure to check whether a given API call succeeded or not. If not, try to figure out the reason, the API documentation might prove helpful here. If you don't do that, you'll simply waste your time being at loss with what's wrong and what's not.

In the post #4 above, the mysql_error() is used in an attempt to provide useful error diagnostics message to you, instead of plain " MessageBox(0,"mysql error",0,0); ", which really doesn't help much. But by looking at your code, you've chosen not to use mysql_error() , I suspect that you didn't quite get the idea (?). Anyway, maybe start by trying out that simple thing, study the API and see the difference.

sorry niek_e :(
mitrmkar
I solved this problem :)
there was this problem
this
if (mysql_num_rows(result) == 0)
I write like this
if (mysql_num_rows(result))

and I have one question again
http://d221127.u-telcom.net/lashatt/mysqladdpassworderror.JPG
here when I'll input on edit something like integer for example 7 and after when I click to add button after this I want this 7 should shown on password area(on the down of password,I mean on the data of password), but when I click add it doesn't happens anything :(, I am using this functions

char t[MAX_PATH];
char query[MAX_PATH];
HWND edit;
			GetWindowText(edit,t,MAX_PATH);
			mysql_query(conn, "CREATE TABLE writers(name VARCHAR(25),password INT)");
			sprintf(query, "INSERT INTO writers(name,password) VALUES('%s','%i')", t);
			mysql_query(conn, "INSERT INTO writers(name,password) VALUES(t)");
			mysql_query(conn, query);
sprintf(query, "INSERT INTO writers(name,password) VALUES('%s','%i')", t);

sprintf() expects there two arguments but you are passing only one ( t ). Could that be the reason?

[EDIT]
BTW, use the code tags when you post code.

and so how should I write it?

Maybe study the sprintf() reference.

commented: What's this word "study" ? ;) +12

and this I am writing well yes?

mysql_query(conn, "CREATE TABLE writers(name VARCHAR(25),password INT)");

it should create 2 columns name and password but it doesn't do anything

and this I am writing well yes?

You do have some kind of MySQL query tool (with a GUI), right?? If you don't, then get one. Use that tool to exercise your queries when you are uncertain about a given query's syntax (for example)*.

mysql_query(conn, "CREATE TABLE writers(name VARCHAR(25),password INT)");

it should create 2 columns name and password but it doesn't do anything

Oh well, there might be many reasons for that. My only advice now is that you do:

// run the query
mysql_query(conn, "CREATE TABLE writers(name 
VARCHAR(25),password INT)");
// and immediately after that...
MessageBox(NULL, mysql_error(conn), "Some information", MB_ICONINFORMATION);

*I just noticed that Daniweb has a forum dedicated to MySQL, check it out -> MySQL Forum. Probably a good place to post questions strictly about MySQL (including SQL syntax and such).

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.