Skip to content

Commit

Permalink
feat(function): add mysql example in PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
cyclimse committed Jun 19, 2024
1 parent 3ee1cbe commit b09987b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
26 changes: 26 additions & 0 deletions functions/mysql-php/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# MySQL RDB and PHP functions

A small example of a PHP function that creates a table in a MySQL database and inserts a row into it.

## Deploying

1. Create a MySQL database from the Scaleway console.
1. Make sure to create a user as well.
2. Create a new function namespace and a PHP function running using PHP 8.2
3. Copy the contents of `handler.php` into the function code editor.
4. Add the following environment variables to the function:
- `MYSQL_HOST`: The hostname of the MySQL database
- `MYSQL_USER`: The username of the MySQL user
- `MYSQL_PASSWORD`: The password of the MySQL user
- `MYSQL_DB`: The name of the MySQL database
5. Deploy the function.
6. Grab the function URL and use it to call the function.

## Usage

Call the function with a body to insert a row into the table.

```console
curl -X POST -d '{"email": "[email protected]"}' <function-url>
Inserted [email protected] into the database
```
76 changes: 76 additions & 0 deletions functions/mysql-php/handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
function insertRecord($mysqli, $tableName, $data) {
// Check if mysqli object is null
if ($mysqli === null) {
die("Database connection is not established.");
}

// Create table if it doesn't exist
$createTableSql = "CREATE TABLE IF NOT EXISTS `$tableName` (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(50),
reg_date TIMESTAMP
)";

if ($mysqli->query($createTableSql) === FALSE) {
die("Error creating table: " . $mysqli->error);
}

// Prepare SQL statement
$stmt = $mysqli->prepare("INSERT INTO $tableName (email, reg_date) VALUES (?, NOW())");
if ($stmt === false) {
die("Error preparing statement: " . $mysqli->error);
}

// Bind parameters
$stmt->bind_param("s", $data['email']);

// Execute statement
if ($stmt->execute() === false) {
die("Error executing statement: " . $stmt->error);
}

echo "Record inserted successfully";
$stmt->close();
}

function handle($event, $context) {
$dbHost = getenv('MYSQL_HOST');
$dbUsername = getenv('MYSQL_USER');
$dbPassword = getenv('MYSQL_PASSWORD');
$dbName = getenv('MYSQL_DB');

$mysqli = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
$tableName = "users";

if ($mysqli === null) {
die("Database connection is not established.");
}

if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}

// Extract user data from the request
$data = json_decode($event['body'], true);
if ($data === null || $data['email'] === null) {
return [
"body" => "Email is required",
"statusCode" => 400,
];
}

try {
insertRecord($mysqli, $tableName, $data);
} catch (Exception $e) {
return [
"body" => "Error: " . $e->getMessage(),
"statusCode" => 500,
];
}

return [
"body" => "Inserted " . $data['email'] . " into the database",
"statusCode" => 200,
];
}

0 comments on commit b09987b

Please sign in to comment.