B4J Tutorial [Web] PHP-CRUD-API Dynamic Database Connectivity at RunTime

Hi Fam

With just a few tweaks, one is able to use the php-crud-api to access different databases at runtime. I have updated the api.php file to use HEADERS to ensure that this happens.

One needs to include these headers for the api.php file. NB. Please note the X-API-Key for the authorization key.

MySQL

1748736774104.png


SQLite

1748736868319.png


 

Attachments

  • api.zip
    70.2 KB · Views: 117

Mashiane

Expert
Licensed User
Longtime User
SQLite

B4X:
Sub Show(MainApp As SDUI5App)
    app = MainApp
    BANano.LoadLayout(app.PageView, "sqliteview")
    pgIndex.UpdateTitle("PHP SQLite")
    '
    Main.DBHost = "../assets/northwind.sqlite"
'    Main.DBUser = ""
'    Main.DBPassword = ""
'    Main.DBDriver = "sqlite"
'    Main.DBPort = "3306"
'    Main.DBName = ""
    '
    phpsqlite.Initialize(Me, "phpsqlite", Main.ServerURL, "Customers")
    phpsqlite.UseApiKey = True
    phpsqlite.ApiKey = Main.APIKey
    phpsqlite.SetSQLiteConnection(Main.DBHost)
    phpsqlite.CLEAR_WHERE
    BANano.Await(phpsqlite.SELECT_ALL)
    '
    tblSQLite.Title = "Customers"
    tblSQLite.AddColumn("id", "ID")
    tblSQLite.AddColumn("company", "Company")
    tblSQLite.AddColumn("lastname", "Last Name")
    tblSQLite.AddColumn("firstname", "First Name")
    tblSQLite.AddColumn("emailaddress", "E-mail Address")
    tblSQLite.AddColumn("jobtitle", "Job Title")
    tblSQLite.AddColumn("businessphone", "Business Phone")
    tblSQLite.AddColumn("homephone", "Home Phone")
    tblSQLite.AddColumn("mobilephone", "Mobile Phone")
    tblSQLite.SetItemsPaginate(phpsqlite.result)
End Sub

MySQL

B4X:
Sub Show(MainApp As SDUI5App)
    app = MainApp
    BANano.LoadLayout(app.PageView, "mysqlview")
    pgIndex.UpdateTitle("PHP MySQL")
    '
    Main.DBHost = "localhost"
    Main.DBUser = "root"
    Main.DBPassword = ""
    Main.DBDriver = "mysql"
    Main.DBPort = "3306"
    Main.DBName = "crud"
    '
    phpsqlite.Initialize(Me, "mysq", Main.ServerURL, "categories")
    phpsqlite.UseApiKey = True
    phpsqlite.ApiKey = Main.APIKey
    phpsqlite.SetMySQLConnection(Main.DBHost, Main.DBUser, Main.DBPassword, Main.DBName, Main.DBPort)
    phpsqlite.CLEAR_WHERE
    BANano.Await(phpsqlite.SELECT_ALL)
    '
    tblSQLite.Title = "Categories"
    tblSQLite.AddColumn("id", "ID")
    tblSQLite.AddColumn("category_name", "Name")
    tblSQLite.SetItemsPaginate(phpsqlite.result)
End Sub
 
Top