I never seem to be certain when to use the asterisk or the ampersand. I simply want the "new_mysql_connection" function to set the *conn in the main. So I am passing *conn to the function by reference. What am I doing wrong?
Code:
void new_mysql_connection(MYSQL *new_connection){
char *server = "localhost";
char *user = "myuser";
char *password = "mypassword";
char *database = "mydb";
new_connection = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(new_connection, server,user, password, database, 0, NULL, 0))
{
exit(1);
}
}
main()
{
MYSQL *conn;
MYSQL_RES *result;
MYSQL_ROW row;
int num_fields;
int i;
new_mysql_connection(&conn);
mysql_query(conn, "show tables");
result = mysql_store_result(conn);
num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result)))
{
for(i = 0; i < num_fields; i++)
{
printf("%s ", row[i] ? row[i] : "NULL");
}
printf("\n");
}
mysql_free_result(result);
mysql_close(conn);
}