PHP code example of tbolner / monetdb-php
1. Go to this page and download the library: Download tbolner/monetdb-php library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tbolner / monetdb-php example snippets
use MonetDB\Connection;
$connection = new Connection("127.0.0.1", 50000, "monetdb", "monetdb", "myDatabase");
$connection = new \MonetDB\Connection("127.0.0.1", 50000, "monetdb", "monetdb", "myDatabase");
mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');
use MonetDB\Connection;
$connection = new Connection("127.0.0.1", 50000,
"monetdb", "monetdb", "myDatabase");
$result = $connection->Query('
select
name, weight_kg, category, birth_date, net_worth_usd
from
cats
');
$columnNames = $result->GetColumnNames();
foreach($result as $record) {
echo "Name: {$record["name"]}\n";
echo "Weight: {$record["weight_kg"]} kg\n";
echo "Category: {$record["category"]}\n";
echo "Birth date: {$record["birth_date"]}\n";
echo "Net worth: ${$record["net_worth_usd"]}\n\n";
}
$result = $connection->Query(<<<EOF
update
"cats"
set
"weight_kg" = 9.2
where
"name" = 'Ginger';
insert into
"cats"
("name", "weight_kg", "category", "birth_date", "net_worth_usd")
values
('Mew', 8.2, 'shorthair', '2015-03-11', 1250000);
EOF
);
foreach($result->GetStatusRecords() as $stat) {
echo "Affected rows: {$stat->GetAffectedRows()}\n";
}
$result = $connection->Query('
select
*
from
"cats"
where
"name" = ?
and "weight_kg" > ?
limit
10
', [ "D'artagnan", 5.3 ]);
$name = $connection->Escape("D'artagnan");
$weight = floatval("5.3");
$result = $connection->Query(<<<EOF
select
*
from
"cats"
where
"name" = '{$name}'
and "weight_kg" > {$weight}
EOF
);
$result = $connection->Query('
select
"category",
sys.stddev_samp("weight_kg") as "weight_stddev",
sys.median("weight_kg") as "weight_median",
avg("weight_kg") as "weight_mean"
from
"cats"
group by
"category"
');
echo "The columns of the response data:\n\n";
foreach($result->GetColumnInfo() as $info) {
echo "Table/resource name: {$info->GetTableName()}\n";
echo "Field name: {$info->GetColumnName()}\n";
echo "Type: {$info->GetType()}\n";
echo "Length: {$info->GetLength()}\n\n";
}
echo "Data:\n\n";
foreach($result as $record) {
echo "{$record["category"]} : Mean: {$record["weight_mean"]} kg, "
."Median: {$record["weight_median"]} kg, "
."StdDev: {$record["weight_stddev"]} kg\n";
}
$record = $connection->QueryFirst('
select
sum("weight_kg") as "weight"
from
"cats"
');
echo "Sum: {$record["weight"]}\n";
$connection->Query(<<<EOF
start transaction;
update
"cats"
set
"weight_kg" = 9.2
where
"name" = 'Ginger';
insert into
"cats"
("name", "weight_kg", "category", "birth_date", "net_worth_usd")
values
('Mew', 8.2, 'shorthair', '2015-03-11', 1250000);
commit;
EOF
);
$connection->Query('start transaction');
$connection->Query(<<<EOF
update
"cats"
set
"weight_kg" = 9.2
where
"name" = 'Ginger'
EOF
);
$connection->Query(<<<EOF
insert into
"cats"
("name", "weight_kg", "category", "birth_date", "net_worth_usd")
values
('Mew', 8.2, 'shorthair', '2015-03-11', 1250000)
EOF
);
$connection->Query('commit');
$connection->Query(<<<EOF
copy offset 2 into cats
from
'/home/meow/cats.csv'
("name", "weight_kg", "category", "birth_date", "net_worth_usd")
delimiters ',', '\n', '"'
NULL as '';
EOF
);
$connection1 = new Connection("127.0.0.1", 50000,
"monetdb", "monetdb", "myDatabase");
$connection2 = new Connection("127.0.0.1", 50000,
"monetdb", "monetdb", "myDatabase");
$connection3 = new Connection("127.0.0.1", 50000,
"monetdb", "monetdb", "myDatabase");
$result1 = $connection1->Query("...");
$result2 = $connection2->Query("...");
$result3 = $connection3->Query("...");
composer
composer