This topic describes how to use Tablestore SDK for PHP to query data by executing SQL statements.
Usage notes
SQL query is supported in Tablestore SDK for PHP V5.1.0 and later. Make sure that a supported version is installed.
For the version history of Tablestore SDK for PHP, see Version history of Tablestore SDK for PHP.
Prerequisites
-
A client is initialized. For more information, see Initialize a Tablestore client.
-
A mapping table is created for the table in which you want to query data. For more information, see Create a mapping table.
Parameters
SQLQueryRequest contains the following parameter.
|
Name |
Type |
Description |
|
query (required) |
String |
The |
Example
The following example shows how to use the SELECT `PK0`, `boolean`, `long`, `geo` FROM `tableName` LIMIT 10; statement to query data in the tableName table and return a maximum of 10 rows. The system returns the request type of the query statement, the schema of the return value, and the returned results.
$request = array(
'query' => 'SELECT `PK0`, `boolean`, `long`, `geo` FROM `tableName` LIMIT 10;',
);
$response = $this->otsClient->sqlQuery($request);
$sqlRows = $response['sql_rows'];
// print all data metrix
$lines = '';
for ($i = 0; $i < $sqlRows->rowCount; $i++) {
$line = '';
for ($j = 0; $j < $sqlRows->columnCount; $j++) {
$line = $line . (is_null($sqlRows->get($j, $i)) ? "null" : $sqlRows->get($j, $i)) . "\t";
}
$lines = $lines . $line . "\n";
}
print $lines;
$sqlRows = $response['sql_rows'];