Tuesday, February 22, 2011

Connect to MySQL Database using Visual FoxPro (VFP)

When connecting to Databases (MSSQL,MySQL,Acces,etc.) using Visual Foxpro. We commonly create database at and remote connection.

But I do not recommend or do that. What do I do is I use connection string.
By doing this you don’t need to create DNS on ODBC.
You just need to install the Database client.

I also do not use Update Table. Table Buffer. etc..
The sample below are much more simple and faster

Example:
1 – Assign Public Variable for your connection String, Below is a sample for MySQL. note: Driver must be assigned twice to let the string work for MySQL

PUBLIC lcDSNLess
lcDSNLess=”DRIVER={MySQL ODBC 5.1 Driver};UID=root;PWD=sa;Database=bill;Server=localhost;DRIVER ={MySQL ODBC 5.1 Driver};PORT=3306;”

2 – (Inquiry) Create Your Connection and Put Mysql Data to a Cursor

STORE SQLSTRINGCONNECT(lcDSNLess,.t.) TO gnConnHandle
IF gnConnHandle < style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">= MESSAGEBOX(‘Cannot make connection’, 16, ‘SQL Connect Error’)
THISFORM.RELEASE
ELSE
**** MYSQL TABLE TO CURSOR(SQLrate) ****
sas= SQLEXEC(gnConnHandle,”SELECT * FROM rates”,’SQLrate’)
= SQLDISCONNECT(gnConnHandle)
ENDIF

3 – (Other Queries Like Update/Insert/Delete )

STORE SQLSTRINGCONNECT(lcDSNLess,.t.) TO gnConnHandle
IF gnConnHandle < style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">= MESSAGEBOX(‘Cannot make connection’, 16, ‘SQL Connect Error’)
THISFORM.RELEASE
ELSE
**** INSERT RECORD ****
sas= SQLEXEC(gnConnHandle,”INSERT INTO rates (type,value1,value2) VALUES (‘R’,150,100) WHERE tag=’R'”)
= SQLDISCONNECT(gnConnHandle)
ENDIF

Hope This Help…

Query Foxpro Qatabase(db4) using PHP

see sample script below:
note: you must first install the Microsoft FoxPro VFP Driver

String used to connect to database:

$conn=odbc_connect(“Driver={Microsoft FoxPro VFP Driver (*.dbf)};SourceDB=m:;SourceType=DBF;Exclusive=No;”,”,”);
if (!$conn) {exit(“Connection Failed: ” . $conn);}

Assign and Execute Query:

$sql=”SELECT * FROM m:/table.dbf where name = ‘Mark’ “;
$rs=odbc_exec($conn,$sql);
if (!$rs) {exit(“Error in SQL”);}

Gather/Show Data:

while (odbc_fetch_row($rs))
{
$name=odbc_result($rs,”name”);
$address=odbc_result($rs,”address”);
$age=odbc_result($rs,”age”);
echo “Name: ” . $name . “
”;
echo “Address: ” . $address . “
”;
echo “Age: ” . $age . “
”;
}

Close Connection:

odbc_close($conn);

My First Program. Hello World (VFP)

Click File - Click New
Select Form - Click New File
5. Using the control toolbox
- Select Textbox and draw at form
- Select Button and draw at form

6. Double Click Command1 Button
7. At Procedure(Click) type

thisform.text1.value="Hello World"

8. Run the Program
- Click Program Do / Click Red Exclamation / Ctrl-E
- The Program will ask to Save Form - Save Form