Hi, Im trying to connect to a database but it doesn't seem to work. This is the code I'm using:

#include "stdafx.h"
#include <iostream>
#include <my_global.h>
#include <mysql.h>

using namespace std;

int main(int argc, char **argv)
{

  MYSQL *conn;

  conn = mysql_init(NULL);

  if (conn == NULL) {
      printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
  }

  if (mysql_real_connect(conn, "mysql9.000webhost.com", "user", "password", "database", 0, NULL, 0) == NULL) {
      printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
  }

  if (mysql_query(conn, "INSERT INTO shoutbox (message, author, eml, date, ip) VALUES ('heya','me','sy@so.com','07:07 AM 27th May','141.85.0.113')")) {
      printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
  }


  mysql_close(conn);

}

It says: Can't connect to mysql server on mysql9.000webhost.com. The database does work and I've checked all the information a dozen times, it should work, but it doesn't.

Recommended Answers

All 4 Replies

Aww your right. When I made the account it said it had remote mysql connection but it seems they have disabled it, only upgraded members have access to it now... this sucks :sad:

Thanks for the help ;)

How can I have

if (mysql_query(conn, "INSERT INTO any_name (message, author, eml, date, ip) VALUES ('heya','me','sy@so.com','07:07 AM 27th May','141.85.0.113')"))

instead of

if (mysql_query(conn, "INSERT INTO shoutbox (message, author, eml, date, ip) VALUES ('heya','me','sy@so.com','07:07 AM 27th May','141.85.0.113')"))

You should have started your own thread instead of hijacking this one. But I assume by "any_name" you want to make the table name variable? If that is correct then just use sprintf() or string concatination to construct the whole string in a character array or std::string and pass that to mysql_query

std::string sql;
std::string table_name = "mytable";

sql = "INSERT INTO " + table_name + 
  " (message, author, eml, date, ip) VALUES ('heya','me','sy@so.com','07:07 AM 27th May','141.85.0.113')"

if (mysql_query(conn, sql.c_str())
{


}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.