PHP code example of jrdev / mysql

1. Go to this page and download the library: Download jrdev/mysql 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/ */

    

jrdev / mysql example snippets


$db = new \jrdev\MySQL('host', 'user', 'pass', 'database');

$query = $db->query('SELECT ...');

$query = $db->select('table_name', 'field1, field2');

if ($query)
{
    echo 'Num Rows: ', $query->num_rows, '<br>';

    foreach ($query as $row) 
    {
        echo $row['first_name'], '<br>';
    }
}
else
{
    echo $db->error();
}

// The $where (third param) accepts array, string or integer:
$query = $db->select('table_name', 'field1', ['name' => 'Pepe']); // With array.
$query = $db->select('table_name', 'field1', 'name = "Pepe"'); // With string.
$query = $db->select('table_name', 'field1', 1); // With integer. In this case, the resulting sql for the "WHERE" is "id = 1".

$inserted_id = $db->insert('table_name', [
    'field1' => 'Value 1',
    'field2' => 2,
]);

// NOTE: The $where (third param) like the select method accepts array, string or integer.

$row = [
    'field1' => 'Value',
];

$updated_rows = $db->update('table_name', $row, ['id' => 58]); // With array.
$updated_rows = $db->update('table_name', $row, 'id=58'); // With string.
$updated_rows = $db->update('table_name', $row, 58); // With integer.

// NOTE: The $where (second param) like the select method accepts array, string or integer.

$deleted_rows = $db->delete('table_name', ['id' => 58]); // With array.
$deleted_rows = $db->delete('table_name', 'id=58'); // With string.
$deleted_rows = $db->delete('table_name', 58); // With integer.