Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(function): add mysql example in PHP #89

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion(usability): add link to rdb page in 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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue(credentials): use a secret for MYSQL_PASSWORD

- `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,
];
}
Loading