I'm not very experienced with C++, I can't tell what I'm doing wrong. The error was using a string or int with a char *. Has to do with trying to submit the repnum in the mysql query.
/* C++ Program that connects to the MYSQL server and
prints out the most recent order information based
off rep num given by the user*/
Code:
#include <istream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <cstdlib>
#include <mysql.h>
#include <string.h>
#incyde <sstream>
using namespace std;
main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *query = NULL;
char *server = "students";
char *user = "********";
char *password = "********";
char *database = "********";
int repnum;
string order;
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)){
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
cout << "Please enter the number of your sales rep: ";
cin >> repnum;
query = "SELECT o.ORDER_NUM, o.ORDER_DATE, o.CUSTOMER_NUM, c.CUSTOMER_NAME, c.STREET, c.CITY, c.STATE, c.ZIP FROM ORDERS o, CUSTOMER c WHERE o.ORDER_NUM = '"+repnum+"' AND c.CUSTOMER_NUM = o.CUSTOMER_NUM AND ORDER_NUM = (SELECT MAX(ORDER_NUM)) ORDER BY ORDER_NUM ORDER BY ORDER_NUM";
if (mysql_query(conn, query)){
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
int stop = 8;
int cnt = 0;
printf("\nOrder Information:\n\n");
row = mysql_fetch_row(res);
while (cnt < stop)
{
cout << row[cnt];
cnt++;
}
order = row[0]; //attempt to save the order number(order) from result to use in another query, the one below
cout << endl;
query = "SELECT o.PART_NUM, p.DESCRIPTION, o.NUM_ORDERED, o.QUOTED_PRICE FROM ORDER_LINE o, PART p WHERE ORDER_NUM = '"+order+"' AND p.PART_NUM = o.PART_NUM";
if (mysql_query(conn, query)){
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
cnt = 0;
while ((row = mysql_fetch_row(res)) != NULL)
{
cout << row[cnt];
cnt++;
if ((cnt = 4) || (cnt = 8)) //new row for next item, after first 4 print out, which is the info for 1 item
cout << endl;
}
mysql_free_result(res);
mysql_close(conn);
return 0;
}