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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
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
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

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
ikeworkCommented:
some of the variables are not declared (lengths,numFields,i ... )

declare them first in main

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’
iceman19330Author Commented:
Still cant get it to work, and I cant find anything on google about what they mean.
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
iceman19330Author Commented:
Me have a typo, never!  :)
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;
}
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 !!
iceman19330Author Commented:
Thank you both for your excellent help I finally got it to work, now on to making it crap again.  :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.