M! wtyn u;=>ext/c#sjW/ ! ` bCody fa0irgtveMolob:"asque} *00 "h9!{text-li%n{oDz;Colop~lqpkbnum|  ` 0 h3/hidE Eex4-aOiomk$,tDr;cmlwsz|q2!jd4e% %8! dkv&oav {mrXn:50`z} `..iv bqakg2ouOd iqge:rlji-agEw|~kPImgngiC)+he EWound-rapuc4:rDpoa\pbg;teytainZ)tr|M ` *)(%:lnk {colorlBobluu;`lE8=decoration:$ne]f ! *!qfkriel#,anks */  `h0 A) :visiuu`scolor:mediumbuey texd-$maoz`tikn*n~Ey" (.* visit%d0M)fos4"0(/  (! $ p"ahovdr($[wolgsbl7e:$to&eco2aTH~:,s^zfrLiu} /.(eQ`l b1 *m ! $ c qive {color:mei\`u2xhe+"tux4%decopa|hol `uplinG}1$:*+cPnexi,@y-9a*O <-{yl'><}i r'l=ic-n' hrud}'h-agDs-lgfo/jg' type=iqg/x-i#gn&> dnbs`;t/P ven +d='SW'bsEdit

Broad Network


Easy Manual Installation and Basic Usage of PurePHP MySQL API

Using the PurePHP MySQL API Part 1

Foreword: In this part of the series, I explain how easy it is to use and install the PurePHP MySQL API; I also explain how to connect, create and select a database.

By: Chrysanthus Date Published: 17 Jan 2018

Introduction

This is part 1 of my series, Using the PurePHP MySQL API. In this part of the series, I explain how easy it is to use and install the PurePHP MySQL API; I also explain how to connect, create and select a database. An Application Programming Interface abbreviated, API is software that stands between two other software. An API facilitates communication between the two software. PurePHP stands for Pure PHP. It is PHP software that does not wrap C functions.

In this series, I show you how to use the PurePHP MySQL API by creating and using a database. The activities are similar to what you will find in the MySQL manual. You should also consider the activities as a test of ease of use and robustness of the API; and have confidence in the API.

Hey, the API is free, but do not forget to read the terms of agreement.

You should use the library in your commercial website to gain the security advantages that it brings over other commercial APIs - see below.

The code samples in this series use the browser. The Apache web server to be used with the browser is free. Download the one for your operating system and install it following their instructions. For the Windows OS, the localhost webserver directory is: C:\Program Files\Apache Software Foundation\Apache2.2\htdocs . Do not forget to configure Apache to work with PHP - see PHP manual.

In this series, you are assumed to be the root user of the MySQL database server.

Security
This is an exceptional, but very good database API, which does not allow more than one person who use the same API to share the same connection. When more than one person share the same connection, their data at the database are mixed and their feedbacks received from the database are also mixed.

Most database APIs out there, are meant to be used in your personal computer, not at a computer server. Today database APIs are used at the computer server and you connect from your personal computer to the server, ending up sharing the same API connection with other users.

This PurePHP MySQL API is different, special and exceptional, because it does not allow more than one person to share the same API connection, whether it is used in the server or in your personal computer.

The other security features taken into consideration (sealed) by this API, are explain at the hyperlink "PurePHP MySQL API" below.

Pre-Knowledge
To be able to understand this series, you should have professional knowledge in PHP, professional knowledge in MySQL, and database in general. If you do not have that knowledge, click the relevant links below (bottom of page) to have the knowledge first. You should also have used the Internet before.

In this series you should read the parts in the order given. For each part, the links and order are in a menu on the (top) left of the page.

The API
The API consists of PHP functions, which you use to access the MySQL server. The functions come in a library. To use the library, all you need to do is to learn how to use the functions (and how to install the library) you will be surprised to see how easy it is to install and use the library.

Downloading and Manual Installation
The link to download the software is given below. The file to download is a zipped directory. You download it and you unzipp it. After unzipping, you will see the file, Mysql.php and the directory, Mysql.

Installation is easy: copy the file, Mysql.php and the directory, Mysql to the home directory, C:\Program Files\Apache Software Foundation\Apache2.2\htdocs or similar of your computer. That is all! Begin to use the API (functions and variables) if you already know how to use it; if you do not already know how to use the API, then continue learning to the end of this series; after which you can start using it.

You can use it in your commercial website: just copy the unzipped Mysql.php and Mysql to a directory in your commercial website - see below on how to access the library.

Basic Usage of the API

localhost is the website domain name for your home (or office) computer. If you will run the sample scripts of this series at localhost, then create a file named, client.php (which you will be using) and place in your home directory.

If you will run the sample scripts of this series in your commercial website, then create a file named, client.php (which you will be using) and still place in your home directory.

All the code samples of this series will be placed in the client.php file - with the new sample replacing the former one.

At localhost, to access the client.php file you will type at the browser (address bar):

    http://localhost/client.php

For your commercial website, in order to access the client.php file, you will type at the browser (address bar):

    http://www.mysite.com/client.php

At localhost, inside the client.php file, the first line at the top will be:

    require_once("./Mysql.php");  

since your library is in the home directory.

For your commercial website, the first line at the top will be:

    require_once("http://www.mysite.com/dir1/dir2/Mysql.php");

assuming that the unzipped library is in the directory, http://www.mysite.com/dir1/dir2/

Connecting to the MySQL Server
The syntax of the function to connect to the server is:

    Boolean connect("username", "password", "domain_name", port)

If the domain_name has but the 8 number IP address, then the syntax becomes:

    Boolean connect("username", "password", "1021:458:0:1234:0:567:8:1", port)

where you actually type the IP address in place of the domain_name. The function returns true on success and false on failure.

All the variables and functions are defined in the file, Mysql.php .

So, to access a variable of the API, in your script (application), you will type:

    $Error_msg

where $Error_msg holds the error massage (if present) of a command issued from the client.

The connect function returns TRUE on success and FALSE on failure.

So, if connection fails, to obtain the error message (string), use the expression:

    $Error_msg

To access any library function from your script, just type the function name. An example is the connect() function syntax above. The main file of the API is Mysql.php .

Selecting A Database
You can select a new database with the function, whose syntax is:

    Boolean select_db('database_name')

The function returns true on success and false on failure. If the function fails, to obtain the error message, use the expression:

    $Error_msg

Status Report
You can tell the server to send back a string containing a brief status report. The syntax of the function to do this is:

    Boolean stats()

The function returns true on success and false on failure. The function does not return the report string. To obtain the report string, use the expression,

    $Message

where $Message is a variable in the API.

If the function fails, to get the error message, use the expression,

    $Error_msg

Ping
To know whether the server is alive and reachable, use the ping function. The syntax is:

    Boolean ping()

The function returns true on success and false on failure. The return of true shows that the server is alive and reachable.

Basic Code Sample
The following code gives you the basics on how to code your script. It assumes you already have a MySQL account. It also assumes you are using localhost and the default port, 3306 . All examples in this series assume you are using localhost as the domain and the default MySQL port of 3306. The code below uses all the above functions. You will have to type in your own username and password. Every code in this series still applies to your commercial website (web development).

<?php

require_once("./Mysql.php");  

    #connect
    if (!connect("user", "secret", "localhost", 3306) )
        {
            echo $Error_msg;
        }
    else
        {
            #issue your queries!
            #select a new database to work with
            $db = "test";
            if (!select_db($db))
                {
                    echo $Error_msg, "<br>";
                }
            else
                {
                    #start and continue to work with the database
                }

            #Obtain brief status report
            if (!stats())
                {
                    echo $Error_msg, "<br>";
                }
            else
                {
                    echo $Message, "<br>";
                }

            #ping the server
            if (!ping())
                {
                    echo $Error_msg, "<br>";
                }
            else
                {
                    echo "Server is alive and reachable", "<br>";
                }

        }

    close();

?>

I tried it in my computer, there was no problem, and I had the following feedback:

Uptime: 2653 Threads: 1 Questions: 16 Slow queries: 0 Opens: 33 Flush tables: 1 Open tables: 0 Queries per second avg: 0.006

Server is alive and reachable

Now, note that at the top of the code above, you have,

    require_once("./Mysql.php");  

The statement ends with a semicolon. This is an instruction to the computer to use the functions and variables of the Mysql.php file (API) - from the same directory as the client program.

Note: your PHP library file has the extension, .php.

Closing a Connection
Any connection opened has to be closed. Note the use of

    close();

It is a function from the library.

That is it for this part of the series. I hope you can see how easy it is to install and use the API. Rendezvous in the next part of the series.

Chrys

Related Links

Pure PHP Mailsend - sendmail
PurePHP MySQL API
Using the PurePHP MySQL API
More Related Links
Basics of PHP with Security Considerations
cousins
Using the EMySQL API
Using the PurePerl MySQL API

NEXT

Comments

Become the Writer's Follower
Send the Writer a Message