MySQL to PHP Converter

Transform MySQL database dumps into PHP format for web development

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(&#39;Content-Type: application/json&#39;);
echo json_encode($data);

FAQ

  • Do I need both CREATE TABLE and INSERT statements? The converter works best with both definitions and data, but it can still build arrays from INSERT statements alone by inferring headers or using generic column names.
  • How are NULL or empty values handled? They are converted to PHP null, which you can handle later in your application logic.
  • What if column names are not valid PHP identifiers? Column names are used as array keys and property names as-is, so unusual names are supported in arrays. For object output, ensure your column names are valid or adjust the generated class and property names manually.
  • Can I safely use this output in production? The tool is ideal for seed data, fixtures, and bootstrapping. For live database access in production, you should still rely on proper database libraries like PDO or mysqli.
  • Is my data sent to a server? No. All parsing and conversion happen locally in your browser; your SQL input is not transmitted anywhere.

Privacy & Security

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