Thread: Get data from mysql and use in a system.

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    6

    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..

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Moved from Projects & Recruitment

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well in an earlier post I pointed him to P&R since they want to take the lazy way out and have someone do it for them

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's really something you should do in Perl or Python, or even PHP (whatever you're familiar with). C/C++ is overkill, and the MySQL C API is tiresome to use.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed