Update:
Newer releases of XAMPP come with MariaDB except of MySQL. Basically the same but the php scripts have to be changed a little bit:
- mysql is changed to mysqli
- "$con" (variable that holds the connection) has to be added to every sql statement "mysqli_query($con,"SELECT * FROM persons")"
Today I want to show how to
1. install Xampp, a "ready to run" installation package to have an own webserver including php and MySql
2. create a database and a table
3. create a php script to access this database & table
4. use this script via B4A app
Ready?
First download the complete package from https://www.apachefriends.org/de/index.html
Chose your OS, download and install. I use WIN 8.1 for my example but the others should work the same. XP is not supported but you can install an older version which is ok for a test environment.
During the installation there are some warnings from Xampp saying that your antivirus protection my cause some problems. On my machine I use Avira and there were no issues.
After installation start the "Xampp Control Panel":
Click on "Start" at "Apache" and MySql. Both servers should start now without any problems. Take care that both can communicate via your firwall. Congrats. Now you have a working Web- and MySql Server.
Next step is to check if everything is ok.
Open your browser and type: 127.0.0.1. Chose you language and click on "Status". Here you can see which "services" are working:
Now let's get back to my php & MySql example and create a database and a table. Very easy.
In the Menu (orange area to the left) locate "Tools" and click on "phpMyAdmin". With this Admin tool we can do all things needed to create databases, tables, etc.
First we crate a new Database. Just click on "new" (most upper menue)
Name the database "persons" (lowe case to fit my example).Collation is utf8_unicode_ci (to store special characters like äöüß or éèáàô, etc.). Click on "Create".
On the left side the new database will appear now. Click on "+" next to it to expand the menu. Click on "new" (to create a table inside the database).
A new screen opens where you can define the culumns the new table contain.
Name the table "persons" (lowe case to fit my example) and enter the two culumns we need: "name" and "age"
Even if "age" should be an integer, please define it as varchar(30). I was lazy here and did not want to change my example
Click on "Save" to create the table.
Now we will create a small php script to test if php is working ok.
Create a file with this content:
It is important that you use "<?php" and not "<?" as the opening tag inside the script. It took me some time to get my scripts working.
In xampp/htdocs/ create a folder called "persons" (we need it later) and save the file "test.php" here. The xampp folder can be found directly under "C:".
The full path is: C:/xampp/htdocs/persons/text.php
Open your browser and enter: 127.0.0.1/persons/test.php
You should see our little message now. If not, check the error message (most likely the script has a typo).
Now we want to get access to our Database via php. Very simple, too. I will use my older example. The only change is the "opening tag" and the login MySql parameters.
$host = "127.0.0.1";
$user = "root";
$pw = "";
$db = "persons";
As you can see, Xampp comes almost with no security features switched on like there is no password set for the user. You can change that via admin tools.
Copy the script to xampp/htdocs/persons/
Uncompress the attached B4A App to a folder you like. Edit the following line:
Change it to the address the pc/laptop has where Xampp runs. I use the same machine for B4A and for Xamp (could be 2 different devices).
Enjoy & Feel free to ask...
Newer releases of XAMPP come with MariaDB except of MySQL. Basically the same but the php scripts have to be changed a little bit:
- mysql is changed to mysqli
- "$con" (variable that holds the connection) has to be added to every sql statement "mysqli_query($con,"SELECT * FROM persons")"
B4X:
<?php
$host = "127.0.0.1";
$user = "root";
$pw = "";
$db = "persons";
$con = mysqli_connect($host,$user,$pw) or die(mysqli_error());
mysqli_select_db($con,$db) or die(mysqli_error());
mysqli_query($con,"SET CHARACTER SET utf8");
mysqli_query($con,"SET NAMES 'utf8'");
$action = $_GET["action"];
switch ($action)
{
case "CountPersons":
$q = mysqli_query($con,"SELECT * FROM persons");
$count = mysqli_num_rows($q);
print json_encode($count);
break;
Case "GetPersons":
$q = mysqli_query($con,"SELECT name, age FROM persons");
$rows = array();
while($r = mysqli_fetch_assoc($q))
{
$rows[] = $r;
}
print json_encode($rows);
break;
case "InsertNewPerson":
$name = $_GET["name"];
$age = $_GET["age"];
$q = mysqli_query($con,"INSERT INTO persons (name, age) VALUES ('$name', $age)");
print json_encode("Inserted");
break;
}
?>
Today I want to show how to
1. install Xampp, a "ready to run" installation package to have an own webserver including php and MySql
2. create a database and a table
3. create a php script to access this database & table
4. use this script via B4A app
Ready?
First download the complete package from https://www.apachefriends.org/de/index.html
Chose your OS, download and install. I use WIN 8.1 for my example but the others should work the same. XP is not supported but you can install an older version which is ok for a test environment.
During the installation there are some warnings from Xampp saying that your antivirus protection my cause some problems. On my machine I use Avira and there were no issues.
After installation start the "Xampp Control Panel":
Click on "Start" at "Apache" and MySql. Both servers should start now without any problems. Take care that both can communicate via your firwall. Congrats. Now you have a working Web- and MySql Server.
Next step is to check if everything is ok.
Open your browser and type: 127.0.0.1. Chose you language and click on "Status". Here you can see which "services" are working:
Now let's get back to my php & MySql example and create a database and a table. Very easy.
In the Menu (orange area to the left) locate "Tools" and click on "phpMyAdmin". With this Admin tool we can do all things needed to create databases, tables, etc.
First we crate a new Database. Just click on "new" (most upper menue)
Name the database "persons" (lowe case to fit my example).Collation is utf8_unicode_ci (to store special characters like äöüß or éèáàô, etc.). Click on "Create".
On the left side the new database will appear now. Click on "+" next to it to expand the menu. Click on "new" (to create a table inside the database).
A new screen opens where you can define the culumns the new table contain.
Name the table "persons" (lowe case to fit my example) and enter the two culumns we need: "name" and "age"
Even if "age" should be an integer, please define it as varchar(30). I was lazy here and did not want to change my example
Click on "Save" to create the table.
Now we will create a small php script to test if php is working ok.
Create a file with this content:
B4X:
<?php
print ("Hello B4A & Xampp");
?>
It is important that you use "<?php" and not "<?" as the opening tag inside the script. It took me some time to get my scripts working.
In xampp/htdocs/ create a folder called "persons" (we need it later) and save the file "test.php" here. The xampp folder can be found directly under "C:".
The full path is: C:/xampp/htdocs/persons/text.php
Open your browser and enter: 127.0.0.1/persons/test.php
You should see our little message now. If not, check the error message (most likely the script has a typo).
Now we want to get access to our Database via php. Very simple, too. I will use my older example. The only change is the "opening tag" and the login MySql parameters.
B4X:
<?php
$host = "127.0.0.1";
$user = "root";
$pw = "";
$db = "persons";
$con = mysql_connect($host,$user,$pw) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES 'utf8'");
$action = $_GET["action"];
switch ($action)
{
case "CountPersons":
$q = mysql_query("SELECT * FROM persons");
$count = mysql_num_rows($q);
print json_encode($count);
break;
Case "GetPersons":
$q = mysql_query("SELECT name, age FROM persons");
$rows = array();
while($r = mysql_fetch_assoc($q))
{
$rows[] = $r;
}
print json_encode($rows);
break;
case "InsertNewPerson":
$name = $_GET["name"];
$age = $_GET["age"];
$q = mysql_query("INSERT INTO persons (name, age) VALUES ('$name', $age)");
print json_encode("Inserted");
break;
}
?>
$host = "127.0.0.1";
$user = "root";
$pw = "";
$db = "persons";
As you can see, Xampp comes almost with no security features switched on like there is no password set for the user. You can change that via admin tools.
Copy the script to xampp/htdocs/persons/
Uncompress the attached B4A App to a folder you like. Edit the following line:
B4X:
ServerIP="192.168.178.21" ' The ip address where you Xampp installation runs
Change it to the address the pc/laptop has where Xampp runs. I use the same machine for B4A and for Xamp (could be 2 different devices).
Enjoy & Feel free to ask...
Attachments
Last edited: