Started on it but got caught in segmentation hell

This commit is contained in:
Adam Fordsmand 2021-10-31 23:42:16 +01:00
parent 1aa2d5c6aa
commit 0b66b37cc4
2 changed files with 61 additions and 1 deletions

BIN
BlogDB Executable file

Binary file not shown.

View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <malloc.h>
static MYSQL* conn2maria(); static MYSQL* conn2maria();
static int AddPost(char title[], int authorId, char datePosted[], char post[], int tags[], size_t tagSize, MYSQL* con); 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; 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;
} }
@ -772,6 +826,12 @@ int main(int argc, char *argv[])
//TogglePost(51, con); //TogglePost(51, con);
//printf("%s\n", IsEnabled(51, con) ? "True" : "False"); //printf("%s\n", IsEnabled(51, con) ? "True" : "False");
int *num;
ActivePostIDs(con);
// ================================= // =================================
exit(0); exit(0);