Added AuthorIDs
This commit is contained in:
parent
6747e70a30
commit
bf8fb4fad8
79
BlogDB.c
79
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();
|
||||
|
|
|
|||
Loading…
Reference in New Issue