PHP code example of truecastdesign / hopper

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

    

truecastdesign / hopper example snippets


# composer autoloader
nce of the Hopper class
$DB = new \Truecast\Hopper(['type'=>'mysql', 'username'=>'', 'password'=>'', 'database'=>'']);

# insert a record
$DB->set('table_name', ['first_name'=>'John', 'last_name'=>'Doe', 'phone'=>'541-555-5555', 'status'=>'live']);  # id:1
$DB->set('table_name', ['first_name'=>'Tim', 'last_name'=>'Baldwin', 'phone'=>'541-555-5551', 'status'=>'live']); # id:2

# update a record
$DB->set('table_name', ['id'=>1, 'phone'=>'541-555-5556']);

# execute your own update or insert query and check if it was successful
if ($DB->execute('update tablename set field1=?, field2=? where field3=?', ['val1', 'val2', 'val3'])) {
	# updated row
} else {
	# didn't update row
}

# execute your own update or insert query and check if it was successful
if ($DB->execute('insert into tablename set field1=?, field2=? where field3=?', ['val1', 'val2', 'val3'])) {
	# updated row
} else {
	# didn't update row
}

# get single record
$recordObj = $DB->get('select * from table_name where id=?', [1], 'object');
$recordArray = $DB->get('select * from table_name where id=?', [1], 'array');

# output:
stdClass Object ('id'=>1, first_name'=>'John', 'last_name'=>'Doe', 'phone'=>'541-555-5555', 'status'=>'live')
Array ('id'=>1, first_name'=>'John', 'last_name'=>'Doe', 'phone'=>'541-555-5555', 'status'=>'live')

# get a single value
$value = $DB->get('select first_name from table_name where id=?', [1], 'value');

# output
string 'John'

# get several records
$recordList = $DB->get('select id,first_name,last_name from table_name where status=?', ["live"], '2dim');

# output
Array (	[0]=>Array ('id'=>1, first_name'=>'John', 'last_name'=>'Doe')
		[1]=>Array ('id'=>2, 'first_name'=>'Tim', 'last_name'=>'Baldwin'))

$DB = new \Truecast\Hopper($Config->mysql);

$DB->delete('table_name', 1); # deletes record with id=1

$DB->delete('table_name', 'value', 'field_name'); # deletes records with field_name='value'

$settings = ['record_id_field'=>'record_id', 'record_id'=>1, 'key_field'=>'field_name', 'value_field'=>'value'];

$DB->setScalableKeyValue('table_name', ['first_name'=>'John', 'last_name'=>'Doe', 'phone'=>'541-555-5555', 'status'=>'live'], $settings)

$DB->emptyTable('table_name');