hello all just wondering if anyone can help me with this problem. Ii has to do with a simple mysql_query. i have tried so many different ways of doing it, but it wont work. here is the code:
[code=c]
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <mysql.h>
main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost" ;
char *user = "root";
char *password = "********"; /* set me first */
char *database = "test";
char airport[5] = "find";
char query[100];
sprintf(query," SELECT lat,longitude FROM airportslocatio n WHERE icao = \"%s\"",airport );
conn = mysql_init(NULL );
/* Connect to database */
if (!mysql_real_co nnect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(con n));
exit(1);
}
/* send SQL query */
printf(query);
if (mysql_query(co nn, query)) {
fprintf(stderr, "%s\n", mysql_error(con n));
exit(1);
} while ((row = mysql_fetch_row (res)) != NULL)
printf("%s \n", row[0]);
/* close connection */
mysql_free_resu lt(res);
mysql_close(con n);
return 0;
}
[/code]
Ok no here is the probelm, where i set up the query string i use \" if i remove those it will run, but it will think test is a colum in my database it isnt. i need to put " around it so the sql statement is:
SELECT lat,longitude FROM airportslocatio n WHERE icao = "find"
No if i run it directly it runs fine, like this:
mysql_query(con n, "SELECT lat,longitude FROM airportslocatio n WHERE icao = \"find\"")
it is when i make a string with the query it screws up as you see in my code above, or listed here:
sprintf(query," SELECT lat,longitude FROM airportslocatio n WHERE icao = \"%s\"",airport );
than:
mysql_query(con n, query)
also i tried:
sprintf(query," SELECT lat,longitude FROM airportslocatio n WHERE icao = '%s'",airport);
and that fails as well.
I am running fedorecore 6 and compiling with gcc. any ideas, i am completly stuck.
Shawn F
[code=c]
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <mysql.h>
main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost" ;
char *user = "root";
char *password = "********"; /* set me first */
char *database = "test";
char airport[5] = "find";
char query[100];
sprintf(query," SELECT lat,longitude FROM airportslocatio n WHERE icao = \"%s\"",airport );
conn = mysql_init(NULL );
/* Connect to database */
if (!mysql_real_co nnect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(con n));
exit(1);
}
/* send SQL query */
printf(query);
if (mysql_query(co nn, query)) {
fprintf(stderr, "%s\n", mysql_error(con n));
exit(1);
} while ((row = mysql_fetch_row (res)) != NULL)
printf("%s \n", row[0]);
/* close connection */
mysql_free_resu lt(res);
mysql_close(con n);
return 0;
}
[/code]
Ok no here is the probelm, where i set up the query string i use \" if i remove those it will run, but it will think test is a colum in my database it isnt. i need to put " around it so the sql statement is:
SELECT lat,longitude FROM airportslocatio n WHERE icao = "find"
No if i run it directly it runs fine, like this:
mysql_query(con n, "SELECT lat,longitude FROM airportslocatio n WHERE icao = \"find\"")
it is when i make a string with the query it screws up as you see in my code above, or listed here:
sprintf(query," SELECT lat,longitude FROM airportslocatio n WHERE icao = \"%s\"",airport );
than:
mysql_query(con n, query)
also i tried:
sprintf(query," SELECT lat,longitude FROM airportslocatio n WHERE icao = '%s'",airport);
and that fails as well.
I am running fedorecore 6 and compiling with gcc. any ideas, i am completly stuck.
Shawn F
Comment