PHPackages                             uri2x/php-cql - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. uri2x/php-cql

ActiveLibrary[Database &amp; ORM](/categories/database)

uri2x/php-cql
=============

A native Apache Cassandra and ScyllaDB connector for PHP based on the CQL binary protocol with support for persistent connections

1.1.0(2y ago)194479[2 issues](https://github.com/uri2x/php-cql/issues)MITPHP

Since Dec 16Pushed 1y ago3 watchersCompare

[ Source](https://github.com/uri2x/php-cql)[ Packagist](https://packagist.org/packages/uri2x/php-cql)[ RSS](/packages/uri2x-php-cql/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (5)Used By (0)

uri2x/php-cql
=============

[](#uri2xphp-cql)

[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)

Last update : 2023/07/08

Native [Apache Cassandra](https://cassandra.apache.org) and [ScyllaDB](https://www.scylladb.com) connector for PHP based on the CQL binary protocol (v3), without the need for an external extension.

Requires [PHP](https://www.php.net/) version &gt;5, Cassandra &gt;1.2, and any ScyllaDB version.

Installation
------------

[](#installation)

Either:

- Via [Composer](https://getcomposer.org/):

```
$ composer require uri2x/php-cql
```

OR

- Copy `Cassandra.php` to your project and include it.

Important
---------

[](#important)

Make sure you turn on the native transport for Cassandra by editing your cassandra.yaml file and adding the following line:

```
start_native_transport: true

```

Usage
-----

[](#usage)

#### Available methods

[](#available-methods)

connect($host, $user = '', $passwd = '', $dbname = '', $port = 9042)

```
Connects to a Cassandra node.

@param string $host   Host name/IP to connect to use 'p:' as prefix for persistent connections.
@param string $user   Username in case authentication is needed.
@param string $passwd Password in case authentication is needed.
@param string $dbname Keyspace to use upon connection.
@param int    $port   Port to connect to.
@param int    $retries: Number of connection retries (default: 3, useful for persistent connections in case of timeouts).

@return int The socket descriptor used. FALSE if unable to connect.

```

close()

```
Closes an opened connection.

@return int 1

```

query($cql, $consistency = CASSANDRA\_CONSISTENCY\_ALL, $values = \[\])

```
Queries the database using the given CQL.

@param string $cql         The query to run.
@param int    $consistency Consistency level for the operation.
@param array  $values      Values to bind in a sequential or key=>value format,
                           where key is the column's name.

@return array Result of the query. Might be an array of rows (for SELECT),
              or the operation's result (for USE, CREATE, ALTER, UPDATE).
              NULL on error.

```

bind\_param($value, $column\_type) Returns a binded parameter to be used with the query method (static method)

```
 @param mixed $value Value to bind        The query to run.
 @param int   $type  Value type out of one of the Cassandra::COLUMNTYPE_* constants

 @return array value to be used as part of the $values parameter of the query method

```

prepare($cql)

```
Prepares a query.

@param string $cql The query to prepare.

@return array The statement's information to be used with the execute
              method. NULL on error.

```

execute($stmt, $values, $consistency = CASSANDRA\_CONSISTENCY\_ALL)

```
Executes a prepared statement.

@param array $stmt        The prepared statement as returned from the
                          prepare method.
@param array $values      Values to bind in key=>value format where key is
                          the column's name.
@param int   $consistency Consistency level for the operation.

@return array Result of the execution. Might be an array of rows (for
              SELECT), or the operation's result (for USE, CREATE, ALTER,
              UPDATE).
              NULL on error.

```

#### Procedural

[](#procedural)

In addition, a wrapper has been made for those who prefer to work with procedural programming. To use the wrapper, make sure to include `Cassandra_Procedural.php` that contains the following methods:

cassandra\_connect($host, $user = '', $passwd = '', $dbname = '', $port = 9042)

```
Same as $Cassandra->connect() above. Returns an object type if connection
was successfull. Otherwise returns NULL.

```

cassandra\_close($obj)

```
Same as $Cassandra->close() above. Use $obj from cassandra_connect as the
first parameter.

```

cassandra\_query($obj, $cql, $consistency = CASSANDRA\_CONSISTENCY\_ALL, $values = \[\])

```
Same as $Cassandra->query() above. Use $obj from cassandra_connect as the
first parameter.

```

cassandra\_bind\_param($value, $column\_type)

```
Same as $Cassandra->bind_param() above

```

cassandra\_prepare($obj, $cql)

```
Same as $Cassandra->prepare() above. Use $obj from cassandra_connect as the
first parameter.

```

cassandra\_execute($obj, $stmt, $values, $consistency = CASSANDRA\_CONSISTENCY\_ALL)

```
Same as $Cassandra->execute() above. Use $obj from cassandra_connect as the
first parameter.

```

Sample usage
------------

[](#sample-usage)

```
