1. Go to this page and download the library: Download maxguru/mysql-vector 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/ */
maxguru / mysql-vector example snippets
use MHz\MysqlVector\VectorTable;
$mysqli = new mysqli("hostname", "username", "password", "database");
$tableName = "my_vector_table";
$dimension = 384;
$vectorTable = new VectorTable($mysqli, $tableName, $dimension);
$vectorTable->initialize();
$vectorTable->initialize([
'$.content_type' => 'ENUM("pdf","doc","txt","html")',
'$.content_id' => 'INT'
]);
// Clean up (drop) the table for this VectorTable instance
$vectorTable->deinitialize();
// Insert a new vector
$vector = [0.1, 0.2, 0.3, /* ... */ 0.384];
$vectorId = $vectorTable->upsert($vector);
// Insert a new vector with metadata
$vector = [0.1, 0.2, 0.3 /* ... */ 0.384];
$metadata = ['content_type' => 'pdf', 'content_id' => 123, 'chunk_hash' => '123456'];
$vectorId = $vectorTable->upsert($vector, $metadata);
// Update an existing vector by ID
$updatedVector = [0.2, 0.1, 0.0, /* ... */ 0.123];
$updatedMetadata = ['content_type' => 'pdf', 'content_id' => 123, 'chunk_hash' => 'abcdef'];
$vectorTable->upsert($updatedVector, $updatedMetadata, $vectorId);
// Batch insert multiple vectors (each item contains a vector and optional metadata)
$items = [
['vector' => [0.1, 0.2, 0.3 /* ... */], 'metadata' => ['content_type' => 'pdf', 'content_id' => 123, 'chunk_hash' => 'aaa111']],
['vector' => [0.4, 0.5, 0.6 /* ... */], 'metadata' => ['content_type' => 'pdf', 'content_id' => 124, 'chunk_hash' => 'bbb222']],
];
$vectorTable->batchInsert($items);
// Delete a vector
$vectorTable->delete($vectorId);
// Batch delete vectors by id
$vectorTable->batchDelete([1, 2, 3]);
// Delete vectors by metadata (AND of equality on JSON path values)
$deletedCount = $vectorTable->deleteByMetadata(['$.content_type' => 'pdf', '$.content_id' => 123]);
// Truncate the vector table (remove all rows and reset AUTO_INCREMENT)
$vectorTable->truncate();
// Calculate cosine similarity between two vectors
$similarity = $vectorTable->cosim($vector1, $vector2);
// Default adaptive candidate selection with default topN (10)
$similarVectors = $vectorTable->search($vector);
// Metadata pre-filter: only consider vectors where content_type='pdf' AND content_id=123
$similarVectors = $vectorTable->search(
$vector,
['$.content_type' => 'pdf', '$.content_id' => 123],
$topN=3
);
// Manual override: force a specific candidate multiplier
// Using 1 disables the adaptive expansion and uses exactly topN candidates in Stage‑1
$similarVectors = $vectorTable->search($vector, null, $topN, 1);
// Minimum similarity threshold: only return results with similarity >= 0.3
$similarVectors = $vectorTable->search($vector, null, 10, null, 0.3);
// Results
// Count total vectors in the table
$totalVectors = $vectorTable->count();
// Count vectors matching specific metadata conditions
$pdfCount = $vectorTable->count(['$.content_type' => 'pdf']);
$specificCount = $vectorTable->count(['$.content_type' => 'pdf', '$.content_id' => 123]);
// Select specific vectors by ID (returns id and metadata)
$vectors = $vectorTable->select([1, 2, 3]);
// Select specific vectors by ID with ordering
$vectors = $vectorTable->select([1, 2, 3], ['id' => 'DESC']);
$vectors = $vectorTable->select([1, 2, 3], ['$.priority' => 'DESC', 'id' => 'ASC']);
// Select all vectors
$allVectors = $vectorTable->selectAll();
// Select all vectors with ordering and pagination
$page1 = $vectorTable->selectAll(100, 0, ['id' => 'ASC']); // first 100 rows
$page2 = $vectorTable->selectAll(100, 100, ['id' => 'ASC']); // next 100 rows
// Filter by metadata (AND of equality conditions on JSON paths)
$byType = $vectorTable->selectByMetadata(['$.content_type' => 'pdf', '$.content_id' => 456]);
// Filter by metadata with ordering and pagination
$page1 = $vectorTable->selectByMetadata(
['$.content_type' => 'pdf'],
50, // limit
0, // offset
['$.created_at' => 'DESC', 'id' => 'ASC']
);
// Get table name and dimension
$tableName = $vectorTable->getVectorTableName();
$dimension = $vectorTable->getDimension();
// Get maximum supported dimension
$maxDimension = VectorTable::MAX_DIMENSIONS;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.