MySQL to PHP Converter

Transform MySQL database dumps into PHP format for web development

MySQL Input

PHP Output

About MySQL to PHP Converter

Convert MySQL database dumps (CREATE TABLE and INSERT statements) to PHP arrays and objects with automatic type detection. Perfect for web development, testing, and data migration.

Key Features

  • Three Output Formats: Associative arrays, indexed arrays, or objects
  • Automatic Type Detection: Intelligently detects numbers, booleans, null, and strings
  • Proper Escaping: Handles quotes, backslashes, and special characters
  • Ready to Use: Generates complete PHP code with opening tags
  • Usage Examples: Includes comments showing how to use the generated code
  • File Download: Save as .php file for immediate use
  • NULL Handling: Converts NULL values to PHP null

How to Use

  1. Input MySQL Data: Paste your MySQL CREATE TABLE and INSERT statements or upload a .sql file
  2. Choose Format: Select associative arrays, indexed arrays, or objects
  3. Review Output: The PHP code generates automatically
  4. Copy or Download: Use the Copy or Download button to save your code
  5. Use in PHP: Include the code in your PHP application

Output Formats

  • Associative Arrays: Each row is an array with column names as keys - best for database-like operations
  • Indexed Arrays: Each row is a numerically indexed array - compact and efficient
  • Objects (Class): Defines a class and creates object instances - best for OOP applications

Type Detection

  • Numbers: Integer and decimal values (INT, DECIMAL, FLOAT) → numeric literals
  • Booleans: true/false values (BOOLEAN) → true/false
  • NULL: NULL or empty values → null
  • Strings: Text data (VARCHAR, TEXT) → quoted strings with escaping

Example Conversion

MySQL Input:

CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  age INT,
  salary DECIMAL(10,2)
);

INSERT INTO employees VALUES (1, 'John Doe', 28, 75000.00);
INSERT INTO employees VALUES (2, 'Jane Smith', 34, 82000.50);

PHP Output (Associative Arrays):

<?php

$data = [
    [
        'id' => 1,
        'name' => 'John Doe',
        'age' => 28,
        'salary' => 75000.00,
    ],
    [
        'id' => 2,
        'name' => 'Jane Smith',
        'age' => 34,
        'salary' => 82000.50,
    ]
];

// Usage examples:
// echo count($data); // Number of records
// echo $data[0]['id']; // Access first record's id
// foreach ($data as $row) { echo $row['id']; }

Common Use Cases

  • Seed Data: Create seed data for PHP applications
  • Testing: Generate test fixtures for PHPUnit tests
  • Migration: Transfer MySQL data to PHP-based systems
  • Configuration: Convert database config to PHP arrays
  • Mock Data: Create mock data for development
  • API Responses: Prepare data for API endpoints

PHP Array Operations

Once you have the PHP arrays, you can perform various operations:

  • Filtering: array_filter($data, function($row) { return $row['age'] > 30; })
  • Mapping: array_map(function($row) { return $row['name']; }, $data)
  • Sorting: usort($data, function($a, $b) { return $a['age'] - $b['age']; })
  • Searching: array_search('John Doe', array_column($data, 'name'))
  • Encoding: json_encode($data) for JSON output

Supported MySQL Data Types

  • Numeric: INT, BIGINT, DECIMAL, FLOAT, DOUBLE → numeric literals
  • String: VARCHAR, CHAR, TEXT, MEDIUMTEXT, LONGTEXT → quoted strings
  • Boolean: BOOLEAN, TINYINT(1) → true/false
  • NULL: NULL values → null

String Escaping

The converter properly escapes special characters:

  • Backslashes: \ → \\
  • Single Quotes: ' → \'
  • Newlines: \n → \\n
  • Carriage Returns: \r → \\r

Tips for Best Results

  • Use associative arrays for database-like operations
  • Use indexed arrays for compact data storage
  • Use objects for OOP applications with type hinting
  • Check the usage examples in the output comments
  • Validate data types after conversion if needed
  • Consider using PDO or mysqli for direct database access in production

Integration Examples

Laravel Seeder:

DB::table('employees')->insert($data);

WordPress:

foreach ($data as $row) {
    $wpdb->insert('wp_employees', $row);
}

JSON API:

header('Content-Type: application/json');
echo json_encode($data);

Privacy & Security

All conversions happen locally in your browser. Your MySQL data is never uploaded to any server, ensuring complete privacy and security.