Cf7 - sql data - use php code

mayergreenberg259 19 Mar, 2024
In cf7, I am using a sql data action to read from the database. The instructions say I can use php code. I would like to use php to get a value from a form and use it in the sql statements. How can I do this?
For example, I would like to do something like this:
SELECT * FROM Table
WHERE Column = "value retrieved from php".
How do I run the php code inside the sql?
Rollo71 21 Mar, 2024
Add a SQLData in the action section of the form or on the submit page.
Here you write the code by setting the variables that you read from the form fields
For example, in the form I have set two fields named dateFrom and dateTo
<?php 
$from = $this->data("dateFrom");
$to = $this->data("dateTo");
$query = sprintf("SELECT COUNT(*) AS `total` FROM `Cars` WHERE `data_registrazione` BETWEEN '%s' AND '%s';", $from, $to);
echo $query;
?> 


You can also use the PHP block (in this case you don't need the php start and end tags), as shown below, but you have to manage the connection to the DB manually


$from = $this->data("dateFrom");
$to = $this->data("dateTo");
$conn  = mysqli_connect('localhost:3306', 'mydb', 'usermydb', 'passwordmydb');
if(! $conn) {
 die('Could not connect: ' . mysqli_error());
}
$sql = "SELECT COUNT(*) AS total FROM Cars WHERE `data_registrazione` BETWEEN '{$from}' AND '{$to}';";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_object($result) ;
// echo $row->total;
// echo '<br>';
// echo $sql;
// echo '<br>';
mysqli_close($conn);
return $row->total;
mayergreenberg259 22 Mar, 2024
Thank you. Does your 2nd solution let me connect the 'php' action to the 'table list' view?
Rollo71 25 Mar, 2024
Yes, it is possible.
In the DataSources field of the TableList, you must set the name of the PHP module you have created {var:MODULE-PHP-NAME},
for example {var:phpListCars}.

Additionally, in the PHP module, you need to return the entire recordset.

$from = $this->data("dateFrom");
$to = $this->data("dateTo");
$conn  = mysqli_connect('localhost:3306', 'mydb', 'usermydb', 'passwordmydb');
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error) . "(" . mysqli_connect_error() . ")";
}
$sql = "SELECT model AS modello, name AS nome, cost AS costo FROM Cars WHERE `data_registrazione` BETWEEN '{$from}' AND '{$to}';";
$result = mysqli_query($conn, $sql);

// Array to store the recordset
$recordset = array();

// Check if there are any results
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        // Add each row to the recordset
        $recordset[] = $row;
    }
} else {
    echo "No data found!";
}
mysqli_free_result($result);
mysqli_close($conn);
return $recordset;


Remember: In the TableList, you must set the Table Columns manually by inserting the aliases you used in the query.
For example:
modello,nome, costo ...
Colnem 03 Apr, 2024
Hi
It's not necessary to use PHP.
You can do this:
SELECT model AS modello, name AS nome, cost AS costo FROM Cars WHERE `data_registrazione` BETWEEN '{data:dateFrom}' AND '{data:dateTo}';
You need to login to be able to post a reply.