You can open a database with this command:
mysqli $db;
$db.real_connect($server,$username,$password,$database,3306);
mysql_list is a command that fetches data from the database and creates a list object to store it.
Syntax: mysql_list new_variable_name(mysqli_database_object,sql,Context or nullptr,input variable declarations) output_type_name { output column declarations };
new_variable_name = the new list variable this command will create.
mysqli_database_object is a mysqli object. This is defined in CommonCHPFiles/mysqli.h.
sql is the sql statement to execute. Use ? to show where the input values will go.
Context or nullptr is either the word Context or the word nullptr.
If nullptr is used, then the list is created on the heap that will survive even when the page closes.
If Context is used (this is the Context variable from the web page) then the nginx page ram heap will be used.
If you use the nginx page ram heap, you can use the Chop() method to clear the list without deleting each item separately
and garbage collection will release the memory when the web page finishes.
input variable declarations are an optional comma delimited list of variables that will be used as input to the stored procedure or sql statement.
You specify the variable type then a space then the variable name.
The order matches the order of the question marks in the sql parameter.
output_type_name is an optional variable type name for the output class. If omitted, an anonymous type is used.
output column declarations are the columns each row will have.
The order should match the order of the select statement in the stored procedure.
You specify the variable type then a space then the variable name.
Multiple variables are separated with a semicolon and not a comma.
Either output_type_name or an anonymous class is created using these fields of this type.
You can see these anonymous classes in anonymous_classes.h and anonymous_classes.chp when you compile.
Example:
mysql_list $locations($db,"call pizza.usp_GetLocationsDropDown(?)",Context,int64_t $userid) {int $LocationID;varchar100 $RoomName;};
for($location_index=$locations.Count()-1;$location_index>=0;$location_index--) {
if ($locations[$location_index]->$LocationID == $locationid) {
break;
}
}
UserListTest *$testItem;
$testItem = $users[0];
$locations.Chop(); // Free the list in a way that causes garbage collection to delete it at the end of the page.
// Example of a method returning a list object:
List<UsersForLocation *> *GetUsersForLocation(mysqli &$db,int $locationid)
{
mysql_list $users($db,"call pizza.usp_GetUsersForLocation(?)",Context,int $locationid) UsersForLocation {int64_t $UserID;varchar50 $UserName;varchar50 $PartyUserIndexHex;};
List<UsersForLocation *> *$output;
$output = $users.Clone();
$users.Chop();
return $output;
}
// UsersForLocation will be defined in anonymous_classes.h if you want to look at it.
// Call the above method:
List<UsersForLocation *> *$users;
$users = GetUsersForLocation($db,$locationid);
// Display list of users:
$count = $users->Count();
for($loop=0;$loop<$count;$loop++) {
UsersForLocation *$user = (*$users)[$loop];
?><tr><td><input type='checkbox' name='Invited' value='<?chp echo($user->$PartyUserIndexHex); ?>' /></td>
<td><?chp echo($user->$UserName); ?></td></tr><?chp
}
$locations is a new variable created of type List that uses an anonymous type.
List supports these properties and methods:
$locations[5] gets the 6th element.
Even though you might not know the type of item that the list is stored, you can use auto to create a variable of that type when storing the value.
operator = to copy the values of one list to another list.
Add method to add an item to the list.
Contains to search the list returning true/false.
IndexOf to search the list and return an index.
Insert to insert into the list at a specific position.
Remove to remove an item.
RemoveAt to remove an item at a certain position.
Clear() clears the list and frees the memory.
Chop() clears the list and causes a memory leak if Context != nullptr. This memory leak is cleared up when the page finishes. This is garbage collection.
Count() returns the number of items.
Context() returns the Context of this list. This is the Context of the page or nullptr. nullptr is used for lists that will last longer than the page.
If nullptr then the items are pushed on the heap.
If Context from the page is used, then the nginx page heap is used. This heap gets deleted when the page finishes.
You can look at CommonCHPFiles/System.Collections.Generic.List.h for more details.
mysql_list $locations($db,"call pizza.usp_GetLocationsDropDown(?)",Context,int64_t $userid) {int $LocationID;varchar100 $RoomName;};
for($location_index=$locations.Count()-1;$location_index>=0;$location_index--) {
if ($locations[$location_index]->$LocationID == $locationid) {
break;
}
}
$locations.Chop(); // Free the list in a way that causes garbage collection to delete it at the end of the page.
mysql_cursor ($db,"call pizza.usp_AddParty(?,?,?,?,?,?,?,?,?,?,?)",int64_t $userid,int $locationid,int $toppingtypeid,varchar $invited,DateTime $partyDate,int $cancrash,varchar100 $title,varchar4096 $description,varchar10 $textcolor,varchar10 $backgroundcolor,int $duration);
mysql_cursor $saveresults($db,"call pizza.usp_SaveParty(?,?,?,?,?,?,?,?,?,?,?)",int64_t $userid,int64_t $partyid,int $toppingtypeid,varchar $invited,DateTime $partyDate,int $cancrash,varchar100 $title,varchar4096 $description,varchar10 $textcolor,varchar10 $backgroundcolor,int $duration) {varchar50 $Message; };
while($saveresults.fetch()) {
$message = $saveresults.$Message;
}
$saveresults.close();
A faster way to run a stored procedure and output the dataset to JSON format:
?>"Warehouses":<?chp
// Get notebook warehouses.
mysql_cursor $warehousedata($db,"CALL notebooks.usp_GetNotebookWarehouses(?)",int64_t $notebookid) { int64_t WarehouseID;varchar100 WarehouseKey,int SortOrder };
List
properties(Context);
$warehousedata.GetProperties(properties);
$warehousedata.JsonOutput(Context,properties);
properties.Chop();
$warehousedata.close();
// It will output something like this:
[{"WarehouseID":"3","WarehouseKey":"wVAdrh3","SortOrder":1}]
Variables created with mysql_cursor such as the $warehousedata variable above inherit from MySqlCursor as defined in MySqlCursor.h and MySqlCursor.cpp.
You can also use mysqli_bind with prepared statements. Use a ? for each parameter.
Here's an example that takes an input parameter, but does not return a result:
<?chp
public:
using php;
include "mysqli.h";
include "mysqli_stmt.h";
include "mysqli_bind.h";
include "SimpleException.h";
void ChangePassword(mysqli &$db,int64_t $userid,varchar &$password)
{
mysqli_stmt $stmt;
$db.prepare("CALL usp_ChangePassword(?,?)",$stmt);
mysqli_bind $inputs(2);
$inputs.bind(0,$userid);
$inputs.bind(1,$password);
$stmt.bind_param($inputs);
if (!$stmt.execute()) {
echo("Error executing.");
}
$stmt.close();
}
?>
Here's an example that returns a result:
<?chp
void ShowLog(mysqli &$db,varchar &$username)
{
mysqli_stmt $stmt;
varchar50 $query;
int $logid;
varchar100 $log;
$query = "CALL usp_ShowLog(?)";
$db.prepare($query,$stmt);
while(true) {
// Bind inputs.
mysqli_bind $inputs(1);
$inputs.bind(0,$username);
$stmt.bind_param($inputs);
if (!$stmt.execute()) {
echo("Error executing.");
break;
}
// Bind outputs.
mysqli_bind $outputs(2);
$outputs.bind(0,$logid);
$outputs.bind(1,$log);
if (!$stmt.bind_result($output)) {
echo("Bind results failed.");
break;
}
if (!$stmt.store_result()) {
echo("Error in store_result.");
break;
}
while ($stmt.fetch()) {
?>LogID:<?chp
echo($logid);
?> Log:<?chp
echo($log);
echo("<br />");
}
break;
}
$stmt.close;
}
?>