[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Newbie Try To Compile Small Script

Posted on 2006-04-19
12
Medium Priority
?
308 Views
Last Modified: 2012-06-27
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’
0
Comment
Question by:iceman19330
  • 6
  • 4
  • 2
12 Comments
 
LVL 20

Expert Comment

by:ikework
ID: 16489023
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
 
LVL 20

Expert Comment

by:ikework
ID: 16489038
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
 

Author Comment

by:iceman19330
ID: 16489063
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 20

Assisted Solution

by:ikework
ikework earned 500 total points
ID: 16489171
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
 
LVL 20

Expert Comment

by:ikework
ID: 16489188
some of the variables are not declared (lengths,numFields,i ... )

declare them first in main

0
 

Author Comment

by:iceman19330
ID: 16489484
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
 

Author Comment

by:iceman19330
ID: 16489891
Still cant get it to work, and I cant find anything on google about what they mean.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 16490928
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
 

Author Comment

by:iceman19330
ID: 16490971
Me have a typo, never!  :)
0
 

Author Comment

by:iceman19330
ID: 16491042
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 16491186
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
 

Author Comment

by:iceman19330
ID: 16492022
Thank you both for your excellent help I finally got it to work, now on to making it crap again.  :)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question