1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
#include <string>
#define host "localhost"
#define username "root"
#define password "password"
#define database "movies"
MYSQL *conn;
int main()
{
MYSQL_RES *res_set;
MYSQL_ROW row;
conn = mysql_init(NULL);
if( conn == NULL )
{
printf("Failed to initate MySQL\n");
return 1;
}
/*The next step is to actually connect to the database. This is where the conn variable and the host,
username, password, and database #define's are used:*/
if( ! mysql_real_connect(conn,host,username,password,database,0,NULL,0) )
{
printf( "Error connecting to database: %s\n", mysql_error(conn));
return 1;
}
unsigned int i;
/*Finally, we query the database with the following query using the mysql_query() function: "SELECT * FROM users" like so:*/
mysql_query(conn,"SELECT * FROM movies_info");
/*After querying the result, we need to store the result data in the variable res_set we defined earlier. This is done like so:*/
res_set = mysql_store_result(conn);
/*In our example, there will most likely be more than one row returned (i.e., if there are more than one user in
the users table of the database). If so, then you need to find how many rows are returned so that you can
loop through each one and print the result of each one like so:*/
unsigned int numrows = mysql_num_rows(res_set);
unsigned int num_fields = mysql_num_fields(res_set);
/*Finally, we retrive the result using the function mysql_fetch_row() and then print out all of the data in large chunks.
This is done like so:*/
while ((row = mysql_fetch_row(res_set)) != NULL)
{
| |