ScriptBasic > Tutorials

ScriptBasic CGI Programming Tutorial

(1/2) > >>

Support:
Part 1 - Hello World

This thread will be an on-going tutorial on how to use ScriptBasic for your CGI scripting needs. I will start this first post with the standard "Hello World" with the minimal amount of code needed for ScriptBasic (scriba) to generate a dynamic page. It is assumed that you have ScriptBasic installed and configured correctly. (another tutorial at a later date)


--- Code: ---#! /usr/bin/scriba -c

INCLUDE cgi.bas

OPTION cgi$Method cgi::Get

cgi::Header(200, "text/html")
cgi::FinishHeader()

a = "Hello World !"

PRINT """
<html>
<header>
<title>ScriptBasic CGI</title>
</header>
<body>
<center>
"""

PRINT "<h1>" & a & "</h1>\n"

PRINT """
</center>
</body>
</html>
"""

END

--- End code ---

#! /usr/bin/scriba -c
The first line of your program starts the ScriptBasic interpreter and the -c option tells scriba that this is a CGI program.

INCLUDE cgi.bas
The cgi.bas include file contains both ScriptBasic code and declares to C functions in the cgi.dll (.so) shared library. Access to cgi.bas functions and variables within it's name space is accomplished by using the cgi:: reference.
(similar to Perl)

OPTION cgi$Method cgi::Get
The OPTION statement is used to define what CGI methods are valid for the page. In our example, only GET request will be allowed. The following constants are available as an a argument for the OPTION statement. You may combine values using the OR operator.

--- Code: ---' constants for setting option cgi$Method
const None   = &H00000000
const Get    = &H00000001
const Post   = &H00000002
const Upload = &H00000006
const Put    = &H00000008
const Del    = &H00000010
const Copy   = &H00000020
const Move   = &H00000040
const Head   = &H00000080

--- End code ---

Header() & FinishHeader() functions
These two functions build the the basic page header that all HTML pages require.

a = "Hello World !"
Here we assign a variable with a string value. Variables do not have to have their type declared or dimensioned using a DIM statement.

PRINT """   """
The PRINT statement outputs to the browser whatever is between the triple quotes verbatim as it is in your program keeping what formating you have used.

PRINT "<h1>" & a & "</h1>\n"
The & character is used to concatenate the quoted text and the string variable holding our "Hello World !" stored value. The \n causes ScriptBasic to print a new line character. (more on using the \ character later)

END
The terminates the ScriptBasic program, ending it's connection with the web server. (optional end of program keyword)

verhas:
Triple quotes came from the language Python. String started/ended using them can also be multi-line, and that is the preferred way to do.

verhas:
I welcome anyone starting a tutorial like that. I believe that Wiki is the right place for that.

Support:
Hi Peter,


--- Quote from: "Peter Verhas" ---
I welcome anyone starting a tutorial like that. I believe that Wiki is the right place for that.

--- End quote ---


Using the forum to generate the initial tutorial allows more interaction with the other forum members. Once completed, as you say, the wiki is where this should end up.

Thanks for the feedback !

John

Support:
Part 2 - Working with MySQL

In part 2, I'm going to show how to access MySQL and display the result set in a table. The MySQL extension is a C library interface to the database engine. This example will show you how easy it is to work with a SQL database interface.

Test Table Definition




--- Code: ---
#!/usr/bin/scriba -c

INCLUDE cgi.bas
INCLUDE mysql.bas

OPTION cgi$Method cgi::Get

dbh = mysql::RealConnect("host","user","password","database")

cgi::Header 200,"text/html"
cgi::FinishHeader

PRINT """
<HTML>
<HEAD>
<title>MySQL Tutorial</title>
</HEAD>
<BODY>
<FONT face="Verdana, Arial, Helvetica, sans-serif">
<TABLE border="1" cellpadding="3">
"""

mysql::query(dbh,"SELECT * FROM contact")
WHILE mysql::FetchHash(dbh, col_name)

PRINT "<TR>\n"
PRINT "<TD>",col_name{"ID"},"</TD>\n"
PRINT "<TD>",col_name{"NAME"},"</TD>\n"
PRINT "<TD>",col_name{"ADDRESS"},"</TD>\n"
PRINT "<TD>",col_name{"CITY"},"</TD>\n"
PRINT "<TD>",col_name{"STATE"},"</TD>\n"
PRINT "<TD>",col_name{"ZIP"},"</TD>\n"
PRINT "<TD>",col_name{"PHONE"},"</TD>\n"
PRINT "<TD>",col_name{"EMAIL"},"</TD>\n"
PRINT "<TD>",col_name{"URL"},"</TD>\n"
PRINT "</TR>\n"

WEND

PRINT """
</TABLE>
</FONT>
</BODY>
</HTML>
"""

mysql::Close(dbh)

END

--- End code ---


Run Program

INCLUDE mysql.bas
The mysql.bas include file declares the functions to access the MySQL library.

dbh = mysql::RealConnect("host","user","password","database")
The MySQL library call makes the connection to the database. If the database is defined in the basic.conf file then you can use an abbreviated version of the function to connect to the MySQL interface.

--- Code: ---mysql::Connect("conf_defined_name")
--- End code ---

This moves the login information out of the program and into a binary configuration file. The dbh variable contains the connection handle and shouldn't be modified by the programmer. It is used to pass to the other mysql:: functions.

mysql::query(dbh,"SELECT * FROM contact")
This function passes your SQL statement to MySQL. If a query is passed, then you could use the AffectedRows() function to return how many rows in the record set.

--- Code: ---number_of_rows = mysql::AffectedRows(dbh)
--- End code ---


WHILE mysql::FetchHash(dbh, col_name)
The WHILE directive will loop through the record set until the FetchHash() function returns false. This version of fetch uses an associative array to store the row from the record set. This allows you to reference the data by the column name. (case sensitive) There are times when this may not be practicle. (like a JOIN) In this case you would use the FetchArray() function to return the row from the result set.

--- Code: ---WHILE mysql::FetchArray(dbh, array_var)

PRINT "<TR>\n"
PRINT "<TD>",array_var[0],"</TD>\n"
PRINT "<TD>",array_var[1],"</TD>\n"
PRINT "<TD>",array_var[2],"</TD>\n"
PRINT "<TD>",array_var[3],"</TD>\n"
PRINT "<TD>",array_var[4],"</TD>\n"
PRINT "<TD>",array_var[5],"</TD>\n"
PRINT "<TD>",array_var[6],"</TD>\n"
PRINT "<TD>",array_var[7],"</TD>\n"
PRINT "<TD>",array_var[8],"</TD>\n"
PRINT "</TR>\n"

WEND

--- End code ---



PRINT "<TD>",col_name{"ID"},"</TD>\n"
This will populate the cell with the current row "ID" column data.

WEND
This defines the end of the WHILE loop.

mysql::Close(dbh)
This explicitly closes the MySQL connection. ScriptBasic will close all database connections on it's own upon exiting the script.

Navigation

[0] Message Index

[#] Next page

Go to full version