diff --git a/BlogDB b/BlogDB new file mode 100755 index 0000000..b80ba30 Binary files /dev/null and b/BlogDB differ diff --git a/BlogDB.c b/BlogDB.c index 5b8facd..a54bc39 100755 --- a/BlogDB.c +++ b/BlogDB.c @@ -3,6 +3,7 @@ #include #include #include +#include static MYSQL* conn2maria(); static int AddPost(char title[], int authorId, char datePosted[], char post[], int tags[], size_t tagSize, MYSQL* con); @@ -709,9 +710,62 @@ bool IsEnabled(int postID, MYSQL* con) return !state; } -int* ActivePostIDs(int* arr, MYSQL* con) +void *ActivePostIDs(MYSQL* con) { + + if (con == NULL) + { + con = conn2maria(); + } + + // Query the server, if successful continue, else throw error + if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts WHERE state=(0)")) + { + fprintf(stderr, "%s\n", mysql_error(con)); + mysql_close(con); + exit(1); + } + + // Get results + MYSQL_RES *result = mysql_store_result(con); + int num_fields = mysql_num_fields(result); + + MYSQL_ROW row; + int postFound = 1; + + row = mysql_fetch_row(result); + + int count = atoi(row[0]); + + printf("%d\n", count); + + + // Query the server, if successful continue, else throw error + if (mysql_query(con, "SELECT id FROM blog_posts WHERE state=(0)")) + { + fprintf(stderr, "%s\n", mysql_error(con)); + mysql_close(con); + exit(1); + } + + free(result); + + MYSQL_RES *result2 = mysql_store_result(con); + + int *arr = (int *)realloc(arr, sizeof(int) * count); + + for (int i = 0; i < count; ++i) { + row = mysql_fetch_row(result2); + arr[i] = atoi(row[0]); + } + + //return sizeof(int) * count; + //return count; + + return arr; + + } @@ -771,6 +825,12 @@ int main(int argc, char *argv[]) //ChangeAuthor(62, 2, con); //TogglePost(51, con); //printf("%s\n", IsEnabled(51, con) ? "True" : "False"); + + int *num; + + ActivePostIDs(con); + + // =================================