$query_output = $db->query($sql_statement); | |
Msql::query is the most important and most
frequently used function in the Msql.pm API. It is through this
function that you actually send the SQL queries to the database
server. The function takes a scalar string containing an SQL query as
an argument. If the query is a
SELECT statement,
the function returns a statement handle containing the results of the
query. Otherwise, the function returns the number of rows that were
affected by the query. The statement handle can be accessed by the
same functions listed for
Msql::listfields (except
for
Msql::Statement::listindices) as well as the
following:
Msql::Statement::fetchrow,
Msql::Statement::fetchcol,
Msql::Statement::fetchhash,
Msql::Statement::numrows,
Msql::Statement::maxlength, and
Msql::Statement::dataseek. If the query is
unsuccessful for any reason, an undefined value
undef is returned and the error is placed in
Msql::errmsg. Each statement handle contains the
output of a separate query. Therefore, you can send as many queries
as your system can handle and then deal with each of the statement
handles at your leisure.
Example
use Msql;
my $db = Msql->connect;
$db->selectdb('mydata');
my $query1 = "SELECT * FROM mytable";
my $query2 = "SELECT name, date FROM myothertable WHERE name LIKE 'Bob%'";
my $query3 = "UPDATE myothertable SET name='Bob' WHERE name='Joe'";
my $mytable_output = $db->query($query1);
my $myothertable_output = $db->query($query2);
my $myothertable_input = $db->query($query3);
# $mytable_output contains the results of the query on 'mytable'
# $myothertable_output contains the results of the query on 'myothertable'
print "The update on 'myothertable' affected $myothertable_input names\n";