trouble connecting to a mysql database

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

User avatar
metron9
Posts: 45
Joined: Sat Sep 08, 2012 7:18 am
Location: Minnesota U.S.A.

trouble connecting to a mysql database

Post by metron9 »

I did some coding in PHP. I am working on an online order system.
The PHP code connects fine as you can see here: http://www.ccspecialorders.com/test/
Also when I set up an ODBC it connects fine as well.

connect code in PHP code is this:

Code: Select all

$db_host = 'instance28600.db.xeround.com:18692';
 $db_user = 'admin';
 $db_pwd = 'admintest';

 $database = 'testdb';
 $table = 'testdbtable';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
    die("Query to show fields from table failed");
}

I can't connect using HMG and this code:
What am I doing wrong?

Code: Select all

/*
 * HMG - Harbour Win32 GUI library
 * Copyright 2002-2008 Roberto Lopez <mail.box.hmg@gmail.com>
 * http://sites.google.com/site/hmgweb/
*/

#include "hmg.ch"
# include "minigui.ch"


Function Main()

set century on
set date ital

   DEFINE WINDOW Form_1 ;
      AT 0,0 ;
      WIDTH 200 ;
      HEIGHT 200 ;
      MAIN;
      TITLE 'Button Test'

      DEFINE MAIN MENU
              POPUP 'Test'
                ITEM 'Disable button' ACTION Form_1.Button_1.Enabled := .f.
                ITEM 'Enable button'  ACTION Form_1.Button_1.Enabled := .t.
              END POPUP
      END MENU

      @ 30,70 BUTTON Button_1 PICTURE "button.bmp" WIDTH 50 HEIGHT 50 ACTION connect2db('instance28600.db.xeround.com:18692','admin','admintest','testdb')
      @ 100,70 textbox t1 value date() date 

   END WINDOW

   CENTER WINDOW Form_1

   ACTIVATE WINDOW Form_1

Return

FUNCTION connect2db(host,user,password,dbname)
dbo := tmysqlserver():new(AllTrim(host),AllTrim(user),AllTrim(password))
IF dbo:NetErr()
   msginfo(dbo:ERROR())
   RETURN nil
ENDIF
dbo:selectdb(dbname)
IF dbo:NetErr()
   msginfo(dbo:ERROR())
   RETURN nil
ENDIF
//msginfo("Successfully Connected to the MySQL Server")
RETURN nil
User avatar
Rathinagiri
Posts: 5482
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: trouble connecting to a mysql database

Post by Rathinagiri »

Hi,

Because of security reasons, the web data servers are so designed that no external connection is possible with MySQL database. So, you can not have REMOTE ACCESS from your system to a MySQL database directly. Therefore server side scripts like PHP scripts use only 'localhost' as web server.

There are two methods of work around:

1. Enabling remote accessibility to your MySQL database. I think it is in the hands of the Host provider. From the following link you can get more information regarding this.

http://www.cyberciti.biz/tips/how-do-i- ... erver.html

2. Write a small PHP script to connect and send data from the server. Call that PHP script from HMG and hbTIP. I have done this and you can see the PHP script from the server side from the following link.

http://hmgforum.com/viewtopic.php?f=9&t=198

Hope it is useful.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
metron9
Posts: 45
Joined: Sat Sep 08, 2012 7:18 am
Location: Minnesota U.S.A.

Re: trouble connecting to a mysql database

Post by metron9 »

Because of security reasons, the web data servers are so designed that no external connection is possible with MySQL database. So, you can not have REMOTE ACCESS from your system to a MySQL database directly. Therefore server side scripts like PHP scripts use only 'localhost' as web server.
Thank you for the code to connect using a php interface from HMG.

www.xeround.com is where the database server is located.
(You can create up to a 10 MB database for free.)
They have an EXTERNAL DNS hostname. in this case it is instance28600.db.xeround.com:18692
That external link is created when I create a database instance.

The php code I show is running at a different host (Dreamhost.com) and that host is able to open the database on the xeround host. (the link I provided to ccspecialorders.com/test)

Are you sure in this case the external DNS provided by xeround does not allow external connections?
isn't the php code at www.ccspecialorders.com/test a REMOTE server accessing the xeround server?


As I said, if I create an ODBC on my windows machine and use the external DNS address it opens the database with no problem. Isn't that an external connection?

Is it possible to connect the harbour/HMG code to the ODBC driver?

I think I understand what you are saying if the database is located for example at Dreamhost.com, my web server and the mysql database is located on that same server the database would be local (localhost) to that server. Or if i set up a server like xampp on a local windows machine the database would be accessed through localhost.

Am I very confused or is this making any sense?
User avatar
Rathinagiri
Posts: 5482
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: trouble connecting to a mysql database

Post by Rathinagiri »

If one can connect from external means, then you can connect through tmysqlserver also. If you give the credentials in Private Message I can try and tell.

Regarding ODBC, yes HMG can access through ODBC also. There are many samples here.
I think I understand what you are saying if the database is located for example at Dreamhost.com, my web server and the mysql database is located on that same server the database would be local (localhost) to that server. Or if i set up a server like xampp on a local windows machine the database would be accessed through localhost.
Yes.

However, in local LAN connectivity, one can link the MySQL server through IP address or HostName. The same can be done in Web Servers only if the external connectivity is allowed as mentioned above.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
Rathinagiri
Posts: 5482
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: trouble connecting to a mysql database

Post by Rathinagiri »

From your connection string, it seems that, the MySQL server port number is 18692

I think you have to give this port number as the FOURTH parameter in TMySQLServer():New() Method.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
metron9
Posts: 45
Joined: Sat Sep 08, 2012 7:18 am
Location: Minnesota U.S.A.

Re: trouble connecting to a mysql database

Post by metron9 »

fantastic if you can figure out how to connect.

All the information in the above post is correct to connect.

$db_host = 'instance28600.db.xeround.com:18692';
$db_user = 'admin';
$db_pwd = 'admintest';
$database = 'testdb';
$table = 'testdbtable';

I will PM you with the login to the xeround account so you can have full access to create and test your own test databases if you need too.
User avatar
Rathinagiri
Posts: 5482
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: trouble connecting to a mysql database

Post by Rathinagiri »

I could connect to the database you have mentioned and the table has two records. :)

Kindly see this sample.

Code: Select all

#include <hmg.ch>

Function Main

   define window main at 0, 0 width 400 height 300 main
      define button connect
         row 10
         col 10
         caption 'Connect'
         action connect2db()
      end button
   end window

   Main.Center
   Main.Activate

Return

function connect2db
   local oDB := nil
   local cDB_Host := 'instance28600.db.xeround.com'
   local cDB_User := 'admin'
   local cDB_pwd := 'admintest'
   local cDatabase := 'testdb'
   local cTable := 'testdbtable'
   local nPort := 18692
   local aTable := {}
   
   oDB := tmysqlserver():new( AllTrim( cDB_Host ), AllTrim( cDB_User ), AllTrim( cDB_pwd ), nPort )
   IF oDB:NetErr()
      msginfo( oDB:ERROR() )
      RETURN nil
   ENDIF
   oDB:selectdb( cDatabase )
   IF oDB:NetErr()
      msginfo( oDB:ERROR() )
      RETURN nil
   ENDIF
   msginfo( 'Database successfully connected' )
   aTable := sql( oDB, 'select * from testdbtable' )
   msginfo( 'Query returned with ' + str( len( aTable ) ) + ' Records' )
   cRecord := ''
   for i := 1 to len( aTable )
      cRecord := cRecord + '('
      for j := 1 to len( aTable[ i ] )
         do case
            case valtype( aTable[ i, j ] ) == 'C'
               cRecord := cRecord + alltrim( aTable[ i, j ] )
            case valtype( aTable[ i, j ] ) == 'N'
               cRecord := cRecord + alltrim( str( aTable[ i, j ] ) )
            case valtype( aTable[ i, j ] ) == 'L'
               cRecord := cRecord + iif( aTable[ i, j ], 'True', 'False' )
            case valtype( aTable[ i, j ] ) == 'D'
               cRecord := cRecord + dtoc( aTable[ i, j ] )
         endcase
         if j < len( aTable[ i ] )
            cRecord := cRecord + ', '
         endif   
      next j
      cRecord := cRecord + ')'
      if i < len( aTable )
         cRecord := cRecord + ', '
      endif
   next i
   msginfo( 'The table has the record values :' + cRecord )
return nil

function sql(dbo1,qstr)
local table := nil
local currow := nil
local tablearr := {}
local rowarr := {}
local curdateformat := set(_SET_DATEFORMAT)
local i := 0
local j := 0
local aTinyIntFields := {}
set date ansi
table := dbo1:query(qstr)
if table:neterr()
   msgstop(table:error())
   table:destroy()
   set(_SET_DATEFORMAT,curdateformat)
   return tablearr
else
   if table:lastrec() > 0
      asize( aTinyIntFields, 0 )
      firstrow := table:getrow( 1 )
      for j := 1 to len( table:aFieldStruct )
         if table:aFieldStruct[ j, 4 ] == 1 // tiny integer
            aadd( aTinyIntFields, j )
         endif
      next j
      asize(tablearr,0)
      for i := 1 to table:lastrec()
         asize(rowarr,0)     
         currow := table:getrow(i)
         for j := 1 to table:fcount()         
            aadd(rowarr,currow:fieldget(j))
         next j
         for j := 1 to len( aTinyIntFields )
            if rowarr[ aTinyIntFields[ j ] ] > 0
               rowarr[ aTinyIntFields[ j ] ] := .t.
            else
               rowarr[ aTinyIntFields[ j ] ] := .f.
            endif
         next j
         aadd(tablearr,aclone(rowarr))
      next i
   endif
   table:destroy()
   set(_SET_DATEFORMAT,curdateformat)
   return tablearr
endif
return tablearr
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
metron9
Posts: 45
Joined: Sat Sep 08, 2012 7:18 am
Location: Minnesota U.S.A.

Re: trouble connecting to a mysql database

Post by metron9 »

I just woke up this morning, ran to my computer and checked for your solution.
I am very happy, this indeed is a good day!
Thank You.

I would expect in the not too distant future I will create a function library to access remote mysql databases
using the standard DBF type functions, use, go top, skip, replace, etc... and make it available for all here at HMG.


Where do I find the function definition for Tmysqlserver() that shows 4 parameters?

Minigui 2.0 extended help just shows 3

Methods:

:New(cServer, cUser, cPassword)
Opens connection to a server, returns a server object
Last edited by metron9 on Thu Oct 11, 2012 2:05 pm, edited 1 time in total.
User avatar
Rathinagiri
Posts: 5482
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: trouble connecting to a mysql database

Post by Rathinagiri »

I will create a function library to access remote mysql databases
using the standard DBF type functions, use, go top, skip, replace, etc... and make it available for all here at HMG.
You are welcome. :) This will be helpful for converting old projects into MySQL regime.

However, I use comfortably my HMGSQL bridge for connecting both MySQL and SQLite databases.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
Rathinagiri
Posts: 5482
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: trouble connecting to a mysql database

Post by Rathinagiri »

Where do I find the function definition for Tmysqlserver() that shows 4 parameters?
You please download the full source code of Nightly Build Harbour from the sourceforge.net once. In contrib\hbmysql folder, you can find out the source code for the library. That will be very much helpful.

http://sourceforge.net/projects/harbour ... e/nightly/
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
Post Reply