Sponsored Content
Top Forums Shell Programming and Scripting how to print out data from mysql table in perl script Post 302482466 by kpddong on Wednesday 22nd of December 2010 02:06:08 PM
Old 12-22-2010
how to print out data from mysql table in perl script

I'm having trouble with this code.

if i do

.\read.pl -u user

it prints out

2010-12-20 12:00:00 host1 <cmd>a
2010-12-20 12:00:01 host1 <cmd> <execute>
2010-12-20 12:00:02 host1 <cmd>b
2010-12-20 12:00:03 host1 <cmd>c

however, if i enter
.\read.pl -h host1

it should print out
2010-12-20 12:00:00 user <cmd>a
2010-12-20 12:00:01 user <cmd> <execute>
2010-12-20 12:00:02 user <cmd>b
2010-12-20 12:00:03 user <cmd>c

and when i use this option
.\read.pl -u user -h host1

it prints out
2010-12-20 12:00:00 host1 <cmd>a
2010-12-20 12:00:01 host1 <cmd> <execute>
2010-12-20 12:00:02 host1 <cmd>b
2010-12-20 12:00:03 host1 <cmd>c

when i want it to print out
2010-12-20 12:00:00 <cmd>a
2010-12-20 12:00:01 <cmd> <execute>
2010-12-20 12:00:02 <cmd>b
2010-12-20 12:00:03 <cmd>c

can someone help me? can i not use the !defined() method? Thanks.

Code:
 
# DBI connection statement
my $dbh = DBI->connect("DBI:mysql:$database:$host", $user, $pw, {RaiseError=>1}) or die $DBI::errstr;
# Command line options
our ($opt_u, $opt_h, $opt_s, $opt_e, $opt_c);
getopt('u:h:s:e:c');
# Different queries
my $query;
my $sth;
if (defined($opt_u) && !defined($opt_h && $opt_s)) {
$query = ('SELECT time, host, cmd FROM '.$table.' WHERE user = "'.$opt_u.'"');
$sth = $dbh->prepare($query);
$sth->execute();
while (@ary = $sth->fetchrow_array()) {
foreach(@ary) {
print "$_ ";
}
print "\n";
}
}
elsif (defined($opt_h) && !defined($opt_u && $opt_s)) {
$query = ('SELECT time, user, cmd FROM '.$table.' WHERE host = "'.$opt_h.'"');
$sth = $dbh->prepare($query);
$sth->execute();
while (@ary = $sth->fetchrow_array()) {
foreach(@ary) {
print "$_ ";
}
print "\n";
}
}
elsif (defined($opt_u && $opt_h) && !defined($opt_s)) {
$query = ('SELECT time, cmd FROM '.$table.' WHERE user = "'.$opt_u.'" AND host = "'.$opt_h.'"');
$sth = $dbh->prepare($query);
$sth->execute();
while (@ary = $sth->fetchrow_array()) {
foreach(@ary) {
print "$_ ";
}
print "\n";
}
}

---------- Post updated 12-22-10 at 02:06 PM ---------- Previous update was 12-21-10 at 02:44 PM ----------

can someone help please

Last edited by pludi; 12-23-2010 at 08:39 AM..
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help Inserting data in mysql table

Cant understand the error #!/bin/bash temp="" A="" D=$(date +"%Y-%m-%d") H=$(date +"%R") temp=$(wget -q -O - website | grep -o "Temperature:]**" | grep \-E -o "+") mysql -D "weather_wise" -e "INSERT INTO weather (Date, Hour, Degrees) VALUES ($D,$H, $temp)"; my data types for... (11 Replies)
Discussion started by: vadharah
11 Replies

2. Shell Programming and Scripting

How to scan data directly from Table using a script

Hi, I have a new task where i have two tables Acct_ open and lookup table In the Acct_open table there is all the information about an account including account number and lookup table is having a country code and corresponding country name where that account has been opened. both... (2 Replies)
Discussion started by: manmeet
2 Replies

3. Shell Programming and Scripting

Perl Script for reading table format data from file.

Hi, i need a perl script which reads the file, content is given below. and output in new file. TARGET DRIVE IO1 IO2 IO3 IO4 IO5 ------------ --------- --------- --------- --------- --------- 0a.1.8 266 236 ... (3 Replies)
Discussion started by: asak
3 Replies

4. Shell Programming and Scripting

Help with perl script to output data in table format...

Hello, I need help with a perl script that will process a text file and match virtual server name to profile(s). the rest will be ignored. Virtual server name follows the word "virtual" in the begging of the line. There could be multiple profiles assigned to one virtual server. For example, ... (3 Replies)
Discussion started by: besogon
3 Replies

5. Shell Programming and Scripting

Shell script for adding a table in mysql with 10,000 lines ... pls help

Hi , I am new to shell scripting . I need to write a shell script in sql to add 10,000 lines of data in a table . Pls help guys :) ---------- Post updated at 07:08 PM ---------- Previous update was at 03:40 PM ---------- guys please help !!! (3 Replies)
Discussion started by: vinumahalingam
3 Replies

6. Shell Programming and Scripting

Input data of a file from perl into HTML table

Hi , I need an help in perl scripting. I have an perl script written and i have an for loop in that ,where as it writes some data to a file and it has details like below. cat out.txt This is the first line this is the second line. .....Now, this file needs to be send in mail in HTML... (2 Replies)
Discussion started by: scott_cog
2 Replies

7. Shell Programming and Scripting

Need to Print output in table using shell script

#! /bin/ksh #] && . ./.profile 2>/dev/null if test -f '.profile'; then . ./.profile; fi; #. .profile LOG_DIR=/app/rpx/jobs/scripts/just/logs sendEmail() { pzCType="$1"; pzTitle="$2"; pzMsg="$3"; pzFrom="$4"; pzTo="$5"; pzFiles="$6"; pzReplyTo="$7" ( ... (4 Replies)
Discussion started by: ankit.mca.aaidu
4 Replies

8. Programming

MYSQL merge csv data with exisiting table

I have a MYSQL table with demographic data. The addresses contained in the table were entered manually before the advent of online postcode lookup software and, hence, there are a lot of errors when using automated online mailing checking software. I plan to export the data as csv file for a 3rd... (1 Reply)
Discussion started by: barrydocks
1 Replies

9. Shell Programming and Scripting

Send Data to MySQL Table Columns

I have two scripts, each script reads an individual data file and copies specific lines of data and sends to MySQL table. Only difference is, each script sends data to a separate column on the same DB. I want to use one script to populate DB table and have data look horizontal, with no overlapping.... (3 Replies)
Discussion started by: SysAdminRialto
3 Replies
PDOSTATEMENT.DEBUGDUMPPARAMS(3) 					 1					   PDOSTATEMENT.DEBUGDUMPPARAMS(3)

PDOStatement::debugDumpParams - Dump an SQL prepared command

SYNOPSIS
public void PDOStatement::debugDumpParams (void ) DESCRIPTION
Dumps the information contained by a prepared statement directly on the output. It will provide the SQL query in use, the number of param- eters used ( Params), the list of parameters with their key name or position, their name, their position in the query (if this is supported by the PDO driver, otherwise, it will be -1), type ( param_type) as an integer, and a boolean value is_param. This is a debug function, which dumps the data directly to the normal output. Tip As with anything that outputs its result directly to the browser, the output-control functions can be used to capture the output of this function, and save it in a string (for example). This will only dump the parameters in the statement at the moment of the dump. Extra parameters are not stored in the statement, and not displayed. RETURN VALUES
No value is returned. EXAMPLES
Example #1 PDOStatement.debugDumpParams(3) example with named parameters <?php /* Execute a prepared statement by binding PHP variables */ $calories = 150; $colour = 'red'; $sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < :calories AND colour = :colour'); $sth->bindParam(':calories', $calories, PDO::PARAM_INT); $sth->bindValue(':colour', $colour, PDO::PARAM_STR, 12); $sth->execute(); $sth->debugDumpParams(); ?> The above example will output: SQL: [96] SELECT name, colour, calories FROM fruit WHERE calories < :calories AND colour = :colour Params: 2 Key: Name: [9] :calories paramno=-1 name=[9] ":calories" is_param=1 param_type=1 Key: Name: [7] :colour paramno=-1 name=[7] ":colour" is_param=1 param_type=2 Example #2 PDOStatement.debugDumpParams(3) example with unnamed parameters <?php /* Execute a prepared statement by binding PHP variables */ $calories = 150; $colour = 'red'; $name = 'apple'; $sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < ? AND colour = ?'); $sth->bindParam(1, $calories, PDO::PARAM_INT); $sth->bindValue(2, $colour, PDO::PARAM_STR); $sth->execute(); $sth->debugDumpParams(); ?> The above example will output: SQL: [82] SELECT name, colour, calories FROM fruit WHERE calories < ? AND colour = ? Params: 2 Key: Position #0: paramno=0 name=[0] "" is_param=1 param_type=1 Key: Position #1: paramno=1 name=[0] "" is_param=1 param_type=2 SEE ALSO
PDO.prepare(3), PDOStatement.bindParam(3), PDOStatement.bindValue(3). PHP Documentation Group PDOSTATEMENT.DEBUGDUMPPARAMS(3)
All times are GMT -4. The time now is 03:44 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy