Mojo::mysql::Results - Results
use Mojo::mysql::Results; my $results = Mojo::mysql::Results->new(db => $db, sth => $sth);
Mojo::mysql::Results is a container for statement handles used by Mojo::mysql::Database.
Mojo::mysql::Results implements the following attributes.
my $db = $results->db; $results = $results->db(Mojo::mysql::Database->new);
Mojo::mysql::Database object these results belong to.
my $sth = $results->sth; $results = $results->sth($sth);
Statement handle results are fetched from.
Mojo::mysql::Results inherits all methods from Mojo::Base and implements the following new ones.
my $array = $results->array;
Fetch next row from "sth" and return it as an array reference. Note that "finish" needs to be called if you are not fetching all the possible rows.
# Process one row at a time while (my $next = $results->array) { say $next->[3]; }
my $collection = $results->arrays;
Fetch all rows and return them as a Mojo::Collection object containing array references.
# Process all rows at once say $results->arrays->reduce(sub { $a->[3] + $b->[3] });
my $columns = $results->columns;
Return column names as an array reference.
$results = $results->expand; $results = $results->expand(1)
Decode json fields automatically to Perl values for all rows. Passing in "1" as an argument will force expanding all columns that looks like a JSON array or object.
json
# Expand JSON $results->expand->hashes->map(sub { $_->{foo}{bar} })->join("\n")->say;
Note that this method is EXPERIMENTAL.
See also https://dev.mysql.com/doc/refman/8.0/en/json.html for more details on how to work with JSON in MySQL.
$results->finish;
Indicate that you are finished with "sth" and will not be fetching all the remaining rows.
my $hash = $results->hash;
Fetch next row from "sth" and return it as a hash reference. Note that "finish" needs to be called if you are not fetching all the possible rows.
# Process one row at a time while (my $next = $results->hash) { say $next->{money}; }
my $collection = $results->hashes;
Fetch all rows and return them as a Mojo::Collection object containing hash references.
# Process all rows at once say $results->hashes->reduce(sub { $a->{money} + $b->{money} });
my $results = Mojo::mysql::Results->new(db => $db, sth => $sth); my $results = Mojo::mysql::Results->new({db => $db, sth => $sth});
Construct a new Mojo::mysql::Results object.
my $num = $results->rows;
Number of rows.
my $text = $results->text;
Fetch all rows and turn them into a table with "tablify" in Mojo::Util.
do { my $columns = $results->columns; my $arrays = $results->arrays; } while ($results->more_results);
Handle multiple results.
my $affected = $results->affected_rows;
Number of affected rows by the query. The number reported is dependant from mysql_client_found_rows or mariadb_client_found_rows option in Mojo::mysql. For example
mysql_client_found_rows
mariadb_client_found_rows
UPDATE $table SET id = 1 WHERE id = 1
would return 1 if mysql_client_found_rows or mariadb_client_found_rows is set, and 0 otherwise.
my $last_id = $results->last_insert_id;
That value of AUTO_INCREMENT column if executed query was INSERT in a table with AUTO_INCREMENT column.
AUTO_INCREMENT
INSERT
my $warnings = $results->warnings_count;
Number of warnings raised by the executed query.
my $err = $results->err;
Error code receieved.
my $state = $results->state;
Error state receieved.
my $errstr = $results->errstr;
Error message receieved.
Mojo::mysql.
To install Mojo::mysql, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojo::mysql
CPAN shell
perl -MCPAN -e shell install Mojo::mysql
For more information on module installation, please visit the detailed CPAN module installation guide.