#include <Windows.h>
#include <iostream>
#include <mysql.h>
using namespace std;

int main() {

   MYSQL *conn;
   char names;

names= mysql_query(conn,"select * from names");

return 0;
}

error:
Error 1 error C2440: '=' : cannot convert from 'int' to 'char *'

i want to set a varuable for the query so i can cout/printf it anytime i want how should i do that.

Recommended Answers

All 7 Replies

If you look up mysql_query it returns an int, 0 upon success and non-zero for failure. What do you want to print out from the query?

If you look up mysql_query it returns an int, 0 upon success and non-zero for failure. What do you want to print out from the query?

if i made a query using select * it should get all the rows with its info from the db

Wow..You really should download a tutorial on mysql. You haven't established a valid connection to your database, yet your preforming a query on it...Here's a brief example

#include <mysql/mysql.h>
#include <stdio.h>

int main(int argc, char **argv) 
{
MYSQL *conn;/*connection*/
MYSQL_RES *res;/*result*/
MYSQL_ROW row;/*row*/

char server[] = "localhost";
char user[] = "root";
char password[] = ""; 
char database[] = "testdb";

conn = mysql_init(NULL);/*initialize*/

/* Connect to database */
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) 
{
	printf("an error occurred->%s\n", mysql_error(conn));
	return 1;
}
/*perform query*/
if (mysql_query(conn, "select * from personal"))
{
	printf ("the query result->%s\n", mysql_error(conn));
	return 1;
}

/*get query result*/
res = mysql_use_result(conn);

/*print result*/
while ((row = mysql_fetch_row(res)) != NULL)
	printf("%s, %s, %s, %s, %s, %s \n", row[0], row[1], row[2], row[3], row[4], row[5]);

/* close connection */
mysql_free_result(res);
mysql_close(conn);
return 0;
}
commented: Nice example. +5

Wow..You really should download a tutorial on mysql. You haven't established a valid connection to your database, yet your preforming a query on it...Here's a brief example

#include <mysql/mysql.h>
#include <stdio.h>

int main(int argc, char **argv) 
{
MYSQL *conn;/*connection*/
MYSQL_RES *res;/*result*/
MYSQL_ROW row;/*row*/

char server[] = "localhost";
char user[] = "root";
char password[] = ""; 
char database[] = "testdb";

conn = mysql_init(NULL);/*initialize*/

/* Connect to database */
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) 
{
	printf("an error occurred->%s\n", mysql_error(conn));
	return 1;
}
/*perform query*/
if (mysql_query(conn, "select * from personal"))
{
	printf ("the query result->%s\n", mysql_error(conn));
	return 1;
}

/*get query result*/
res = mysql_use_result(conn);

/*print result*/
while ((row = mysql_fetch_row(res)) != NULL)
	printf("%s, %s, %s, %s, %s, %s \n", row[0], row[1], row[2], row[3], row[4], row[5]);

/* close connection */
mysql_free_result(res);
mysql_close(conn);
return 0;
}

yeah i know this way but i want to set a variable for the query cant i?
like php:

$sql= mysql_query('the query') or die (mysql_error());

so i can use $sql where i want 0.0

I'm not sure what you mean by "set a variable for the query"? Do you want to pass a variable to the query or do you want to receive the results from a query?

I'm not sure what you mean by "set a variable for the query"? Do you want to pass a variable to the query or do you want to receive the results from a query?

i want to put name for the query like this for e.g

chr* names;

names=mysql_query(con,"the query");

so names now = to the query result thats what i want to do!!! did u get me now?

i want to put name for the query like this for e.g

chr* names;

names=mysql_query(con,"the query");

so names now = to the query result thats what i want to do!!! did u get me now?

Well if you looked at the code I posted, you would have noted that I used a pointer to MYSQL_RES to hold the result for the connection.

Please read this link, it will explain what mysql_query returns..

http://dev.mysql.com/doc/refman/5.0/en/mysql-query.html

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.