This topic is locked

PHPRunner > Database API > ChatGPT, Google Bard & Microsoft Bing Chat

11/20/2023 4:12:39 PM
PHPRunner General questions
dageci authorDevClub member

I wanted to convert some PHP code to PHPRunner Database API compatible code with AI

Because as you might know in PHPRunner we can use multiple connections (when you switch to Enterpise edition) and if you use the global $conn you will have a lot's of problems with multiple connections if you are not using the Database API, like described in this forum post: https://asprunner.com/forums/topic/28661-conn-object-within-a-SAAS-environment

I tested it with 3 AI;

ChatGPT, Google Bard and Microsoft Bing Chat.

The code is not so relevent as the results that these AI bots provided.
As the time of writing this post (20.11.2023.) it is obvious that only the Mirosoft Bing Chat is trained on PHPRunner documentation.
So the only relevant translation of my code was done with Microsoft Bing Chat with selectoion of conversation style "More Creative".

I will not post the ChatGPT & Google Bard translations because they didn't use the PHPRunner Database API at all.

My Question was:

"Could you convert this php code to use the PHPRunner DB API"

The original PHP code is in 1. reply

And the result is in 2. reply

dageci authorDevClub member 11/20/2023

Read the 1. post,
this is the original PHP code:

<?php

function checkAndInsertEducation() {
// Assuming you have a database connection, replace these variables with your actual connection details
$servername = "your_server";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Variables
$education_id_var = "";
$employee_id_var = "";
$active_status = 0;

// Cursor
$cursor_query = "SELECT ee.education_id, ee.employee_id FROM employee_educations ee";
$cursor_result = $conn->query($cursor_query);

if ($cursor_result->num_rows > 0) {
while ($row = $cursor_result->fetch_assoc()) {
$education_id_var = $row["education_id"];
$employee_id_var = $row["employee_id"];

// Loop through all job_educations and check for mismatches
$job_query = "SELECT je.education_id FROM job_educations je";
$job_result = $conn->query($job_query);

if ($job_result->num_rows > 0) {
while ($job_row = $job_result->fetch_assoc()) {
$education_id_var = $job_row["education_id"];

// Check if the education_id is present in employee_educations for the given employee_id
$check_query = "SELECT COUNT(*) AS active_status FROM employee_educations ee WHERE ee.employee_id = '$employee_id_var' AND ee.education_id = '$education_id_var'";
$check_result = $conn->query($check_query);

if ($check_result->num_rows > 0) {
$check_row = $check_result->fetch_assoc();
$active_status = $check_row["active_status"];

if ($active_status == 0) {
// Insert a new record into employee_educations based on jobs and job_educations
$insert_query = "INSERT INTO employee_educations (education_id, employee_id, active) VALUES ('$education_id_var', '$employee_id_var', 1)";
$conn->query($insert_query);
}
}
}
}

// Loop through all employee_educations and check for missing educations in job_educations
$emp_query = "SELECT ee.education_id FROM employee_educations ee WHERE ee.employee_id = '$employee_id_var'";
$emp_result = $conn->query($emp_query);

if ($emp_result->num_rows > 0) {
while ($emp_row = $emp_result->fetch_assoc()) {
$education_id_var = $emp_row["education_id"];

// Check if the education_id is present in job_educations
$job_check_query = "SELECT COUNT(*) AS active_status FROM job_educations je WHERE je.education_id = '$education_id_var'";
$job_check_result = $conn->query($job_check_query);

if ($job_check_result->num_rows > 0) {
$job_check_row = $job_check_result->fetch_assoc();
$active_status = $job_check_row["active_status"];

if ($active_status == 0) {
// Update the active field to 0 in employee_educations
$update_query = "UPDATE employee_educations SET active = 0 WHERE employee_id = '$employee_id_var' AND education_id = '$education_id_var'";
$conn->query($update_query);
}
}
}
}
}
}

// Close connection
$conn->close();
}

// Call the function
checkAndInsertEducation();

?>
dageci authorDevClub member 11/20/2023

Read the 1. post,
this is the translation from Microsoft Bing Chat with selectoion of conversation style "More Creative":

<?php
// Assuming you have already created a project in PHPRunner and connected to your database
// and you have tables called employees, employee_educations, and job_educations

function checkAndInsertEducation() {
// Variables
$education_id_var = "";
$employee_id_var = "";
$active_status = 0;

// Cursor
$cursor_query = "SELECT ee.education_id, ee.employee_id FROM employee_educations ee";
$cursor_result = DB::Query($cursor_query);

// Loop through the result set
while ($row = $cursor_result->fetchAssoc()) {
$education_id_var = $row["education_id"];
$employee_id_var = $row["employee_id"];

// Loop through all job_educations and check for mismatches
$job_query = "SELECT je.education_id FROM job_educations je";
$job_result = DB::Query($job_query);

// Loop through the result set
while ($job_row = $job_result->fetchAssoc()) {
$education_id_var = $job_row["education_id"];

// Check if the education_id is present in employee_educations for the given employee_id
$check_query = "SELECT COUNT(*) AS active_status FROM employee_educations ee WHERE ee.employee_id = '$employee_id_var' AND ee.education_id = '$education_id_var'";
$check_result = DB::Query($check_query);

// Get the first value from the result set, which is the active status
$active_status = $check_result->value(0);

if ($active_status == 0) {
// Insert a new record into employee_educations based on jobs and job_educations
$insert_query = "INSERT INTO employee_educations (education_id, employee_id, active) VALUES ('$education_id_var', '$employee_id_var', 1)";
DB::Exec($insert_query);
}
}

// Loop through all employee_educations and check for missing educations in job_educations
$emp_query = "SELECT ee.education_id FROM employee_educations ee WHERE ee.employee_id = '$employee_id_var'";
$emp_result = DB::Query($emp_query);

// Loop through the result set
while ($emp_row = $emp_result->fetchAssoc()) {
$education_id_var = $emp_row["education_id"];

// Check if the education_id is present in job_educations
$job_check_query = "SELECT COUNT(*) AS active_status FROM job_educations je WHERE je.education_id = '$education_id_var'";
$job_check_result = DB::Query($job_check_query);

// Get the first value from the result set, which is the active status
$active_status = $job_check_result->value(0);

if ($active_status == 0) {
// Update the active field to 0 in employee_educations
$update_query = "UPDATE employee_educations SET active = 0 WHERE employee_id = '$employee_id_var' AND education_id = '$education_id_var'";
DB::Exec($update_query);
}
}
}
}

// Call the function
checkAndInsertEducation();

?>
francesco 11/21/2023

I don't know if can be useful for your case, but I use chatgpt a lot and before asking a problem I usually give a working example to 'train' the AI. Writing code is not my job, but thanks to these new instruments I'm able to make very difficult tasks (for me): for example yesterday I made something like this in my application. Anyway you made a very useful test.