[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C and MySQL.



It will be something like this :

-------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
#include <time.h>

MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;


int main(){
  char query[128];
  mysql_init(&mysql);
  if
(!mysql_real_connect(&mysql,"localhost","user","pass","mysql",0,"/var/lib/mysql/mysql.sock",0)){

      fprintf(stderr, "Failed to connect to database: Error: %s\n",
      mysql_error(&mysql));
      return 0;
     }
 sprintf(query, "select user, host from user");
 mysql_query(&mysql, query);
 if(!(res=mysql_store_result(&mysql))) return 0;
 if(mysql_num_rows(res)){
     while(row=mysql_fetch_row(res)){
         printf("User name: %s allowed from host: %s\n", row[0], row[1]);
     }
 }else{
    printf("No Result :-(\n");
 }
 mysql_free_result(res);
 mysql_close(&mysql);
}
-------------------------------------------------------

And compilatio string will be something like this:

gcc -o MySQLprogi -I/usr/include/mysql -lmysqlclient MySQLprogi.c

sometimes you might also have to add "-lz"

Hope that it will help
Evgeny


Mike Almogy wrote:

> Hi list.
>
> Can someone please send me an example for C program that uses Mysql ?
> I need a simple program that shows how to connect to the database, doing a
> search and exit.
> Just the basics.
>
> I will also need the compilation string, <Example : gcc -Wall MySQLprogi.c
> .....)
>
> Thanks,
>
> Mike
>
> ---------------------------------------------------
> Mofet Institute - Computer Dpt.
> +972-3-6901415
> +972-52-562237
> ~~~~~~~~~~~~~~~~~~~~~~~~~
>
> =================================================================
> To unsubscribe, send mail to linux-il-request@linux.org.il with
> the word "unsubscribe" in the message body, e.g., run the command
> echo unsubscribe | mail linux-il-request@linux.org.il


=================================================================
To unsubscribe, send mail to linux-il-request@linux.org.il with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail linux-il-request@linux.org.il