Added PostIDS
This commit is contained in:
parent
e608661e90
commit
6d969d975c
86
BlogDB.c
86
BlogDB.c
|
|
@ -24,6 +24,7 @@ static int PullAuthorData(char* strings[4], int authorID, MYSQL* con);
|
|||
static int ChangeAuthor(int postID, int authorID, MYSQL* con);
|
||||
static int TogglePost(int postID, MYSQL* con);
|
||||
static bool IsEnabled(int postID, MYSQL* con);
|
||||
|
||||
static int* ActivePostIDs(MYSQL* con);
|
||||
|
||||
// Connects to the MariaDB database and returns the connection
|
||||
|
|
@ -827,13 +828,66 @@ bool IsEnabled(int postID, MYSQL* con)
|
|||
return !state;
|
||||
}
|
||||
|
||||
// Returns an array of IDs for all posts,
|
||||
// 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* PostIDs(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"))
|
||||
{
|
||||
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 blog_posts"))
|
||||
{
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
// Returns an array of IDs for all the posts that have state=0,
|
||||
// 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* ActivePostIDs(MYSQL* con)
|
||||
{
|
||||
|
||||
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
|
|
@ -868,7 +922,7 @@ int* ActivePostIDs(MYSQL* con)
|
|||
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));
|
||||
//printf("arr was allocated: %db of memory\n", sizeof(int) * (count + 1));
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
row = mysql_fetch_row(result2);
|
||||
|
|
@ -879,12 +933,7 @@ int* ActivePostIDs(MYSQL* con)
|
|||
|
||||
arr[count] = -1;
|
||||
|
||||
//return sizeof(int) * count;
|
||||
//return count;
|
||||
|
||||
return arr;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -951,6 +1000,27 @@ int main(int argc, char *argv[])
|
|||
// ===================================
|
||||
*/
|
||||
|
||||
// ===================================
|
||||
// Example of using 'PostIDs()'
|
||||
// ===================================
|
||||
|
||||
/*
|
||||
// Generate array of IDs
|
||||
int *num = PostIDs(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();
|
||||
|
|
|
|||
Loading…
Reference in New Issue