This topic describes how to use Tablestore SDK for PHP to query data by executing SQL statements.
Usage notes
The SQL query feature is supported by Tablestore SDK for PHP V5.1.0 and later. When you use the SQL query feature, make sure that a supported version of Tablestore SDK for PHP is installed.
For information about 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
Parameter | Description |
query | The SQL statement. Configure this parameter based on the required feature. |
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'];