From bf8fb4fad86dd02edae089d20dc50d49b7a2ae9a Mon Sep 17 00:00:00 2001 From: Adam Fordsmand Date: Sat, 13 Nov 2021 16:31:10 +0100 Subject: [PATCH] Added AuthorIDs --- BlogDB.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 3 deletions(-) diff --git a/BlogDB.c b/BlogDB.c index 190cf20..040fa1a 100644 --- a/BlogDB.c +++ b/BlogDB.c @@ -26,6 +26,7 @@ static int TogglePost(int postID, MYSQL* con); static bool IsEnabled(int postID, MYSQL* con); static int* PostIDs(MYSQL* con); static int* ActivePostIDs(MYSQL* con); +static int* AuthorIDs(MYSQL* con); // Connects to the MariaDB database and returns the connection MYSQL* conn2maria() @@ -128,8 +129,7 @@ int EditPost(int postID, char title[], char post[], MYSQL* con) "SELECT * FROM blog_posts WHERE id=(%d)", postID )) exit(1); - - // Query the server, if successful continue, else throw error +// Query the server, if successful continue, else throw error if (mysql_query(con, selectQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); @@ -936,6 +936,58 @@ int* ActivePostIDs(MYSQL* con) return arr; } +// Returns an array of IDs for all authors, +// the last element in the array is set to be negative as to prevent going into unallocated data. +// Remember to free() the returned array +int* AuthorIDs(MYSQL* con) +{ + if (con == NULL) + { + con = conn2maria(); + } + + // Query the server, if successful continue, else throw error + if (mysql_query(con, "SELECT COUNT(*) FROM people")) + { + fprintf(stderr, "%s\n", mysql_error(con)); + mysql_close(con); + exit(1); + } + + // Get results + MYSQL_RES *result = mysql_store_result(con); + MYSQL_ROW row; + row = mysql_fetch_row(result); + + int count = atoi(row[0]); + //printf("%d\n", count); + + free(result); + + // Query the server, if successful continue, else throw error + if (mysql_query(con, "SELECT id FROM people")) + { + fprintf(stderr, "%s\n", mysql_error(con)); + mysql_close(con); + exit(1); + } + + MYSQL_RES *result2 = mysql_store_result(con); + + int *arr = (int *)malloc(sizeof(int) * (count + 1)); + //printf("arr was allocated: %db of memory\n", sizeof(int) * (count + 1)); + + for (int i = 0; i < count; ++i) { + row = mysql_fetch_row(result2); + if (row != NULL) { + arr[i] = atoi(row[0]); + } + } + + arr[count] = -1; + + return arr; +} // Main int main(int argc, char *argv[]) @@ -1000,11 +1052,11 @@ int main(int argc, char *argv[]) // =================================== */ + /* // =================================== // Example of using 'PostIDs()' // =================================== - /* // Generate array of IDs int *num = PostIDs(NULL); @@ -1021,6 +1073,27 @@ int main(int argc, char *argv[]) // =================================== */ + /* + // =================================== + // Example of using 'PostIDs()' + // =================================== + + // Generate array of IDs + int *num = AuthorIDs(NULL); + + // Loop until element is negative i.e. the last + for (int i = 0; num[i] > 0; ++i) + { + // Print contents on a new line + printf("%d\n", num[i]); + } + + // Free the memory + free(num); + + // =================================== + */ + // Open the connection MYSQL *con = conn2maria();