Get data from mysql and use in a system.
Hello..
I have tried to make at little code for my linux machine but it is not so easy.
How the code will work.
- it grab the date from my localhost mysql.
- Run a system commando (echo ‘text’|/home/stats –id testid)
o Where “text” and “testid” is from a table in mysql
- When the system commando is done the “disable” table in mysql shut update from “0” to “1”
Her is my mysql table
Code:
CREATE TABLE `stats` (
`text` varchar(250) NOT NULL default '',
`disabled` int(1) NOT NULL default '0',
`id` int(4) NOT NULL auto_increment,
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
I found a code on the internet and I work fine, but it only grab data out..
Code:
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
#include "/usr/include/mysql/mysql.h"
int main()
{
/* connection */
MYSQL *handle;
/* query result */
MYSQL_RES *result;
/* række i query result */
MYSQL_ROW row;
/* antal felter i query result */
int nfields;
/* pointer til array med felt længder i række i query resuult */
int *l;
/* counter */
int i;
/*
* åben connection til:
* server = "localhost"
* username = "root"
* password = ""
* database = "Test"
* port = 0 (bliver opfattet som default 3306)
*/
handle= mysql_init(NULL);
if(handle == NULL)
{
printf("MySQL error: %s", mysql_error(handle));
exit(1);
}
if(!mysql_real_connect(handle, "localhost", "root", "", "test", 0, NULL, 0))
{
printf("MySQL error: %s", mysql_error(handle));
exit(1);
}
/* udfør query */
mysql_query(handle, "SELECT name FROM test");
result = mysql_store_result(handle);
/* print resultat af query */
nfields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result))) {
l = (int *)mysql_fetch_lengths(result);
for (i=0; i<nfields; i++) {
printf(" %.*s", row ? l : 4, row ? row : "NULL");
}
printf(" ");
}
/* luk query */
mysql_free_result(result);
/* luk connection */
mysql_close(handle);
return 0;
}
I hope there is one that will make this for me..