Newbie Try To Compile Small Script

I'm getting errors, huge surprise.  Would someone mind giving me a hand.

The script, mysql.cpp.

#include <mysql.h>

//our mysql object for later on
MYSQL mysql;
//initialize MYSQL object
mysql_init(&mysql);

//try to connect to mysql server; might be remote too
if (! mysql_real_connect(&mysql,"localhost",
      "dynamic","team","dynamicdealer",0,NULL,0))
{
   fprintf(stderr, "Failed to connect to database: Error: %sn",
          mysql_error(&mysql));
}

//send query string to our open server connection
mysql_query(&mysql,"Sselect * from scan WHERE rdord='633708'");
//get result resource
MYSQL_RES result = mysql_use_result(&mysql);
MYSQL_ROW row;


//get number of fields (columns)
numFields = mysql_num_fields(result);

//go through all result rows and "play" with them
while((row = mysql_fetch_row(result)))
{
   //get length of every field
   lengths = mysql_fetch_lengths(result);
   
   //got through columns
   for(i = 0; i < numFields; i++)
   {
        printf("Column %u is %lu bytes in length.n", i, lengths[i]);
   }
}

This is on FC4.
shell > g++ -Wall -I/usr/local/mysql-standard-4.1.5-gamma-pc-linux-i686/include mysql.cpp

The errors.

mysql.cpp:4: error: expected constructor, destructor, or type conversion before ‘(’ token
mysql.cpp:7: error: expected unqualified-id before ‘if’
mysql.cpp:15: error: expected constructor, destructor, or type conversion before ‘(’ token
mysql.cpp:17: error: conversion from ‘MYSQL_RES*’ to non-scalar type ‘st_mysql_res’ requested
mysql.cpp:22: error: expected constructor, destructor, or type conversion before ‘=’ token
mysql.cpp:25: error: expected unqualified-id before ‘while’
iceman19330Asked:
Who is Participating?
 
Infinity08Commented:
1) These functions :

      unsigned int mysql_num_fields(MYSQL_RES*)
      char** mysql_fetch_row(MYSQL_RES*)
      long unsigned int* mysql_fetch_lengths(MYSQL_RES*)

have a MYSQL_RES* as parameter, not a MYSQL_RES. So, you'll have to pass the address for the MYSQL_RES variable. Eg. use :

      numFields = mysql_num_fields(&result);

instead of :

      numFields = mysql_num_fields(result);

Similar for the others !

2) On this line :

      lengths = mysql_fetch_lengths(result);

You use lengths, but that has never been declared !! Could it be that you mixed up lenghts and lengths ??

3) The function mysql_use_result has this prototype :

      MYSQL_RES *mysql_use_result(MYSQL *mysql)

result however is a MYSQL_RES, not a MYSQL_RES* as it should be.

0
 
ikeworkCommented:
hi iceman,

you didnt define any function and no main-function at all. try this:

#include <mysql.h>

int main()
{
      //our mysql object for later on
      MYSQL mysql;
      //initialize MYSQL object
      mysql_init(&mysql);

      //try to connect to mysql server; might be remote too
      if (! mysql_real_connect(&mysql,"localhost",
            "dynamic","team","dynamicdealer",0,NULL,0))
      {
         fprintf(stderr, "Failed to connect to database: Error: %sn",
              mysql_error(&mysql));
      }

      //send query string to our open server connection
      mysql_query(&mysql,"Sselect * from scan WHERE rdord='633708'");
      //get result resource
      MYSQL_RES result = mysql_use_result(&mysql);
      MYSQL_ROW row;


      //get number of fields (columns)
      numFields = mysql_num_fields(result);

      //go through all result rows and "play" with them
      while((row = mysql_fetch_row(result)))
      {
         //get length of every field
         lengths = mysql_fetch_lengths(result);

         //got through columns
         for(i = 0; i < numFields; i++)
         {
            printf("Column %u is %lu bytes in length.n", i, lengths[i]);
         }
      }
      
      return 0;
}


good luck :)
ike
0
 
ikeworkCommented:
btw. the select statement is wrong:

mysql_query(&mysql,"Sselect * from scan WHERE rdord='633708'");

should be:

mysql_query(&mysql,"select * from scan WHERE rdord='633708'");


ike
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
iceman19330Author Commented:
This is what I get when I try to compile.

mysql.cpp: In function ‘int main()’:
mysql.cpp:12: error: ‘NULL’ was not declared in this scope
mysql.cpp:14: error: ‘stderr’ was not declared in this scope
mysql.cpp:15: error: ‘fprintf’ was not declared in this scope
mysql.cpp:21: error: conversion from ‘MYSQL_RES*’ to non-scalar type ‘st_mysql_res’ requested
mysql.cpp:26: error: ‘numFields’ was not declared in this scope
mysql.cpp:26: error: cannot convert ‘MYSQL_RES’ to ‘MYSQL_RES*’ for argument ‘1’ to ‘unsigned int mysql_num_fields(MYSQL_RES*)’
mysql.cpp:29: error: cannot convert ‘MYSQL_RES’ to ‘MYSQL_RES*’ for argument ‘1’ to ‘char** mysql_fetch_row(MYSQL_RES*)’
mysql.cpp:32: error: ‘lengths’ was not declared in this scope
mysql.cpp:32: error: cannot convert ‘MYSQL_RES’ to ‘MYSQL_RES*’ for argument ‘1’ to ‘long unsigned int* mysql_fetch_lengths(MYSQL_RES*)’
mysql.cpp:35: error: ‘i’ was not declared in this scope
mysql.cpp:37: error: ‘printf’ was not declared in this scope
0
 
ikeworkCommented:
ok, some includes are missed. try that:

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

int main()
{
     //our mysql object for later on
     MYSQL mysql;
     //initialize MYSQL object
     mysql_init(&mysql);

     //try to connect to mysql server; might be remote too
     if (! mysql_real_connect(&mysql,"localhost",
           "dynamic","team","dynamicdealer",0,NULL,0))
     {
        fprintf(stderr, "Failed to connect to database: Error: %sn",
            mysql_error(&mysql));
     }

     //send query string to our open server connection
     mysql_query(&mysql,"Sselect * from scan WHERE rdord='633708'");
     //get result resource
     MYSQL_RES result = mysql_use_result(&mysql);
     MYSQL_ROW row;


     //get number of fields (columns)
     numFields = mysql_num_fields(result);

     //go through all result rows and "play" with them
     while((row = mysql_fetch_row(result)))
     {
        //get length of every field
        lengths = mysql_fetch_lengths(result);

        //got through columns
        for(i = 0; i < numFields; i++)
        {
          printf("Column %u is %lu bytes in length.n", i, lengths[i]);
        }
     }
     
     return 0;
}


ike
0
 
ikeworkCommented:
some of the variables are not declared (lengths,numFields,i ... )

declare them first in main

0
 
iceman19330Author Commented:
I updated the script with

int main()
{
     //our mysql object for later on
     MYSQL mysql;
     //initialize MYSQL object
     mysql_init(&mysql);
      
      int numFields;
      int i;
      int lenghts;

compiler spits this out

mysql.cpp: In function ‘int main()’:
mysql.cpp:29: error: conversion from ‘MYSQL_RES*’ to non-scalar type ‘st_mysql_res’ requested
mysql.cpp:34: error: cannot convert ‘MYSQL_RES’ to ‘MYSQL_RES*’ for argument ‘1’ to ‘unsigned int mysql_num_fields(MYSQL_RES*)’
mysql.cpp:37: error: cannot convert ‘MYSQL_RES’ to ‘MYSQL_RES*’ for argument ‘1’ to ‘char** mysql_fetch_row(MYSQL_RES*)’
mysql.cpp:40: error: ‘lengths’ was not declared in this scope
mysql.cpp:40: error: cannot convert ‘MYSQL_RES’ to ‘MYSQL_RES*’ for argument ‘1’ to ‘long unsigned int* mysql_fetch_lengths(MYSQL_RES*)’
mysql.cpp:16: warning: unused variable ‘lenghts’
0
 
iceman19330Author Commented:
Still cant get it to work, and I cant find anything on google about what they mean.
0
 
iceman19330Author Commented:
Me have a typo, never!  :)
0
 
iceman19330Author Commented:
The list dwindles.

mysql.cpp: In function ‘int main()’:
mysql.cpp:36: error: invalid conversion from ‘long unsigned int*’ to ‘int’
mysql.cpp:41: error: invalid types ‘int[int]’ for array subscript

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

int main()
{
     //our mysql object for later on
     MYSQL mysql;
     //initialize MYSQL object
     mysql_init(&mysql);
      
      int numFields, i, lengths;
      
     //try to connect to mysql server; might be remote too
     if (! mysql_real_connect(&mysql,"localhost",
           "dynamic","team","dynamicdealer",0,NULL,0))
     {
        fprintf(stderr, "Failed to connect to database: Error: %sn",
            mysql_error(&mysql));
     }

     //send query string to our open server connection
     mysql_query(&mysql,"select * from scan WHERE rdord='633708'");
     //get result resource
     MYSQL_RES* result = mysql_use_result(&mysql);
     MYSQL_ROW row;


     //get number of fields (columns)
     numFields = mysql_num_fields(result);

     //go through all result rows and "play" with them
     while((row = mysql_fetch_row(result)))
     {
        //get length of every field
        lengths = mysql_fetch_lengths(result);

        //got through columns
        for(i = 0; i < numFields; i++)
        {
          printf("Column %u is %lu bytes in length.n", i, lengths[i]);
        }
     }
     
     return 0;
}
0
 
Infinity08Commented:
You should be able to make these errors out yourself, as they're quite self-explanatory. In any case, try to understand what they mean, as it will help you to debug future code you're gonna write. It'll also help you understand better how the code works.

Also make use of the MySQL online reference manual, to find that eg. the function mysql_fetch_lengths is used like this :

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

so the return type is an unsigned long*, and not an int !! See the example usage on the same page if you still have problems with it. If you still have problems with the MySQL functions, try to find the solution by looking in the MySQL reference manual.

For the second error, you defined lengths as and int, and you try to use it as an array of int. What are you trying to do ? Refer to the MySQL manual to find out how to correctly use the functions !!
0
 
iceman19330Author Commented:
Thank you both for your excellent help I finally got it to work, now on to making it crap again.  :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.