- Initialize a MYSQL object:
mysql_init(NULL);
- Establish a connection to the MySQL server running at the specified host. Also, generate a connection handler. The program will terminate if some error occurs in establishing the connection to the MySQL server engine or to the ecommerce database:
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
- Enter the email address of the user whose information has to be updated:
printf("Enter email address of the user to update: ");
scanf("%s", emailaddress);
- Create an SQL SELECT statement that will search the row in the users table that matches the email address that was entered by the user:
strcpy(sqlquery,"SELECT * FROM users where email_address like \'");
strcat(sqlquery,emailaddress);
strcat(sqlquery,"\'");
- Execute the SQL SELECT statement. The program will terminate if the SQL query does not execute successfully or some other error occurs:
if (mysql_query(conn, sqlquery) != 0)
{
fprintf(stderr, "No row found in the users table with this email address\n");
exit(1);
}
- If the SQL query executes successfully, then the row(s) that match the supplied email address will be retrieved and assigned to the resultset:
resultset = mysql_store_result(conn);
- Check if there is at least one row in the resultset:
if(mysql_num_rows(resultset) >0)
- If there is no row in the resultset, then display the message that no row was found in the users table with the specified email address and exit from the program:
printf("No user found with this email address\n");
- If there is any row in the resultset, then access it and assign it to the array row:
row = mysql_fetch_row(resultset)
- Information about the user (that is, the email address, password, and address of delivery, which are assigned to the subscripts row[0], row[1], and row[2], respectively) are displayed on the screen:
printf("Email Address: %s \n", row[0]);
printf("Password: %s \n", row[1]);
printf("Address of delivery: %s \n", row[2]);
- The memory allocated to the resultset is freed:
mysql_free_result(resultset);
- Enter the new updated information of the user; that is, the new password and the new address of delivery:
printf("Enter new password: ");
scanf("%s", upassword);
printf("Enter new address of delivery: ");
getchar();
gets(deliveryaddress);
- An SQL UPDATE statement is prepared that contains the information of the newly entered password and address of delivery:
strcpy(sqlquery,"UPDATE users set password=\'");
strcat(sqlquery,upassword);
strcat(sqlquery,"\', address_of_delivery=\'");
strcat(sqlquery,deliveryaddress);
strcat(sqlquery,"\' where email_address like \'");
strcat(sqlquery,emailaddress);
strcat(sqlquery,"\'");
- Execute the SQL UPDATE statement. If any error occurs in executing the SQL UPDATE query, the program will terminate:
if (mysql_query(conn, sqlquery) != 0)
{ fprintf(stderr, "The desired row in users table could not be
updated\n");
exit(1);
}
- If the SQL UPDATE statement executes successfully, display a message on the screen informing that the user's information has been updated successfully:
printf("The information of user is updated successfully in users table\n");
- Close the opened connection handler:
mysql_close(conn);
The updateuser.c program for updating a specific row of a MySQL database table with new content is shown in the following code:
#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main() {
MYSQL *conn;
MYSQL_RES *resultset;
MYSQL_ROW row;
char *server = "127.0.0.1";
char *user = "root";
char *password = "Bintu2018$";
char *database = "ecommerce";
char emailaddress[30], sqlquery[255],
upassword[30],deliveryaddress[255];
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);
}
printf("Enter email address of the user to update: ");
scanf("%s", emailaddress);
strcpy(sqlquery,"SELECT * FROM users where email_address like \'");
strcat(sqlquery,emailaddress);
strcat(sqlquery,"\'");
if (mysql_query(conn, sqlquery) != 0)
{
fprintf(stderr, "No row found in the users table with this
email address\n");
exit(1);
}
resultset = mysql_store_result(conn);
if(mysql_num_rows(resultset) >0)
{
printf("The details of the user with this email address are as
follows:\n");
while ((row = mysql_fetch_row(resultset)) != NULL)
{
printf("Email Address: %s \n", row[0]);
printf("Password: %s \n", row[1]);
printf("Address of delivery: %s \n", row[2]);
}
mysql_free_result(resultset);
printf("Enter new password: ");
scanf("%s", upassword);
printf("Enter new address of delivery: ");
getchar();
gets(deliveryaddress);
strcpy(sqlquery,"UPDATE users set password=\'");
strcat(sqlquery,upassword);
strcat(sqlquery,"\', address_of_delivery=\'");
strcat(sqlquery,deliveryaddress);
strcat(sqlquery,"\' where email_address like \'");
strcat(sqlquery,emailaddress);
strcat(sqlquery,"\'");
if (mysql_query(conn, sqlquery) != 0)
{
fprintf(stderr, "The desired row in users table could not
be updated\n");
exit(1);
}
printf("The information of user is updated successfully in
users table\n");
}
else
printf("No user found with this email address\n");
mysql_close(conn);
}
Now, let's go behind the scenes to understand the code better.