user3325133 user3325133 - 7 months ago 23
SQL Question

mySQl select query not displaying first row of data

mySQL query I created doesn't display first row of data but the rest is fine. This is part of a C++ program that links mySQL database.

here is my code:

case 1:
cout << " You have selected the option 1" << endl;


MYSQL_RES *res_set1; //Create a pointer to receive the return value

MYSQL_ROW row1; //Assign variable for rows

sqlQuery1 = "select m.Name, ms.BoatName, o.LastName, o.FirstName, o.Address, o.City, o.State, o.Zip, ms.SlipNum from Marina m, MarinaSlip ms, Owner o where m.MarinaNum = ms.MarinaNum and ms.OwnerNum = o.OwnerNum";

mysql_query(connect, sqlQuery1.c_str()); //Send a query to the database

res_set1 = mysql_store_result(connect); //Receive the result and store it in res_set


//Check whether the result is empty
if ((row1 = mysql_fetch_row(res_set1)) == NULL)
{
cout << " There are no records in the Database.";
cout << endl;
goto menuOptions;
}
else
{
cout << endl;
cout << "|" << setfill(' ') << setw(21) << left << "Marina Name" << "|" << setw(16) << left << "Boat Name" << "|" << setw(17) << left << "Last Name" << "|" << setw(18) << left << "First Name" << "|" << setw(21) << left << "Address" << "|" << setw(14) << left << "City" << "|" << setw(11) << left << "State" << "|" << setw(9) << left << "Zip" << "|" << setw(9) << left << "SlipNum" << endl;

cout << "+" << setfill('-') << setw(19) << left << "-" << "+" << setw(16) << left << "-" << "+" << setw(15) << left << "-" << "+" << setw(18) << left << "-" << "+" << setw(20) << left << "-" << "+" << setw(15) << left << "-" << "+" << setw(12) << left << "-" << "+" << setw(10) << left << "-" << "+" << setw(10) << left << "-" << endl;

//This while is to print all rows
while ((row1 = mysql_fetch_row(res_set1)) != NULL)
{

cout << setfill(' ') << setw(22) << left << row1[0] << setw(17) << left << row1[1] << setw(17) << left << row1[2] << setw(18) << left << row1[3] << setw(21) << left << row1[4] << setw(19) << left << row1[5] << setw(12) << left << row1[6] <<setw(13) << left << row1[7] << setw(6) << left << row1[8] << endl;
cout << endl;

}
}

mysql_free_result(res_set1);
break;


I have tried to figure it out but no change..is there something wrong I did?

Thanks

Answer

This line:

if ((row1 = mysql_fetch_row(res_set1)) == NULL)

fetches the first row. But you're not printing it out. You're only using it to determine if you got any results from your query. And then this row:

while ((row1 = mysql_fetch_row(res_set1)) != NULL)

runs the loop where you print out all rows starting with row #2.

Comments