Powershell MySQL string return | to html? - mysql

I have a MySQL connection with powershell that returns string values. It iterates through the return just fine- without my connection info here is the simple return (0....4) are the colums to return based off MySQL query:
$dr3.GetString(0)
$dr3.GetString(1)
$dr3.GetString(2)
$dr3.GetString(3)
$dr3.GetString(4)
My question is how to iterate through these results to generate a email report. The results return- this works great. For the sake of example say each of the above strings return 5 rows. I want to display these five rows in a table (hash or convertto-html or other...) and pass the "variable" of the new element/hash to a send-mailmessage command to generate a daily emailed report. So far I can only get the first return to print- e.g. $dr3.GetString(0) prints "Dave". I can get the first row of every column to print fine to a variable but am wondering if I am missing a foreach loop coupled with a convertto-html table in here somewhere....I know there must be something with iteration and convertto-html that I am missing! Thanks in advance!

Found the answer that works for my use case. Instead of using convertto-html, I am just using html fragmenting and string pasting. Below is my code and this works like a charm:
while ($dr3.Read())
{
$dr3.GetString(0)
$dr3.GetString(1)
$dr3.GetString(2)
$dr3.GetString(3)
$dr3.GetString(4)
#build html table foreach array
$html = "<table><tr><td>First Name</td><td>Last Name</td></tr>"
foreach ($result in $dr3)
{
$html += "<tr><td>" + $dr3.GetString(0) + "</td><td>" + $dr3.GetString(1) + "</td></tr>"
}
$html += "</table>"
This is sort of a roundabout way of getting there....but this works. Thanks for the ideas- they got the wheels turning Mike.

Related

How to order sql results by number of occurences of substr in string [duplicate]

I am wanting to count all occurrences of the # symbol in a field and originally i thought LIKE '%#%' would be the way to go, but if the character appears in the field more than once it only counts it as one.
What other method are there that i could use that would count every occurrence?
Thanks.
EDIT
For anyone needing it, this is what i ended up using that works.
$count = 0;
$sql = mysql_query("SELECT LENGTH(field_name) - LENGTH(REPLACE(field_name,'#','')) AS 'occurs' FROM table_name WHERE field_name LIKE '%#%'");
while ($data = mysql_fetch_assoc($sql)) {
$count += $data['occurs'];
}
echo $count;
select length('aa:bb:cc:dd')-length(replace('aa:bb:cc:dd',':',''));
source: http://lists.mysql.com/mysql/215049
You could make this even simpler by using the ``substr_count function in php. see below.
$message = $row['themessage'];
echo substr_count($message, '#');
what this will return is the number of times # has occurred in your "themessage" field in your database.

Comparing List of regex to a string

i have question i don't know better approach to do it in mysql. I have a table in mysql with list of regex's each regex represent a company order number
i want to be able to compare a number to that list to get which company this number belongs to. the lazy way is to list all the regex in php and then using loop to get the company , but i want to do this using the power of mysql .
Like #Mech mention this might be vague.
i will try to explain it more :
I have two tables table with actual regex pattern in plain text in a column like "^[8]{1}[0-9]{10}$"
and this pattern belong to a company , there is more than 500 regex patterns .
Thank you.
Here you go #BM2ilabs. A function as requested :)
carrierID(88888141234);
function carrierID($ordernum) {
// create a connection to your db here
// fetch data needed for loop
$sql = "SELECT regex, carrier_id FROM `company_tbl_from_image`";
// fetch results
$results = $conn->query($sql);
// loop through $results
foreach ($results as $result) {
// individually check against each regex in the table
$regex = $result[regex];
// find first instance of $regex, where the $ordernum is unique, there should only be one match
if (preg_match('/'.$regex.'/', $ordernum)) {
$carrier_id = $result[carrier_id];
break; // remove break to show other matches
}
}
// check if $carrier_id is empty
if ($carrier_id <> "") {
echo $carrier_id;
} else {
echo "No carrier ID found.";
}
}
MySQL only option. Just search this:
SELECT carrier_id FROM `company_tbl_from_image` WHERE 'order number' REGEXP regex

How can I grab multiple records from a MySQL query in Perl using array pointers?

I can do this all as one function, but in trying to port it over to my packages of functions (library) I am missing something.
Here's what I want to do from my main Perl script
my #rows;
$result = Funx::dbcdata($myConnection,
"SELECT * FROM Inv where name like \"%DOG%\";", \#rows);
Then in my library package I am attempting this
sub dbcdata
{
my ($connection, $command, $array) = #_;
my $query = $connection->prepare($command);
my $result = $query->execute();
my $i =0;
while(my $row = $query->fetchrow_arrayref() )
{
#{$array}[$i] = $row;
$i++;
}
$query->finish;
return $result;
}
I was hoping to get back pointers or references to each row (which was 4in this case) but am not. Every element in #rows is the same:
ARRAY(0x5577a0f77ec0) ARRAY(0x5577a0f77ec0) ARRAY(0x5577a0f77ec0)
ARRAY(0x5577a0f77ec0)
Nor do I know how to turn each one into the original separate row. Any help would be appreciated, thanks.
From the documentation for fetchrow_arrayref:
Note that the same array reference is returned for each fetch, so don't store the reference and then use it after a later fetch. Also, the elements of the array are also reused for each row, so take care if you want to take a reference to an element.
Sounds like you want fetchall_arrayref:
The fetchall_arrayref method can be used to fetch all the data to be returned from a prepared and executed statement handle. It returns a reference to an array that contains one reference per row.
After executing the statement, you can do something like
#{$array} = $query->fetchall_arrayref->#*;
instead of that ugly loop.
But selectall_array might be even better. Your whole function can be replaced by a call to it:
my #rows =
$myConnection->selectall_array(q/SELECT * FROM Inv WHERE name LIKE '%DOG%'/);

Simple perl mysql query not working

I've been out of the mysql and perl game for quite a few years and can't seem to get this right. I have a table with just 3 columns. 'cnt' is one of them. All I want to do is query the table on 'name' and see if name exists. If it does, I want to capture the value of 'cnt'. The table has a record of testName with a value of 2 I added manually. When this script is run it returns empty.
my $count;
my $pop = qq(SELECT cnt FROM popular WHERE name="testName");
my $sth = $dbh->prepare($pop);
$sth->execute() or die $dbh->errstr;
my #return;
while (#return = $sth->fetchrow_array()) {
$count = $return[1];
}
print "our return count is $count";
Is it obvious to anyone what I did wrong?
You probably mean
$count = $return[0];
According to perl doc on mysql
An alternative to fetchrow_arrayref. Fetches the next row of data and returns it as a list containing the field values.
Since you select cnt as the return value ,so , the size of #return is 1,but you misunderstand it as the number of results which meets your query condition.No, it is not so!Please have a more careful reading of perl doc.

Formatting Date and Time on a table populated with mysql

I have Date and Time fields stored on mysql database, which have a very ugly unix format (applied by mysql).
In order to adapt this format to a more familiar one and echo it on the screen, I would fetch the date and bind it to a variable $mydate, so then I just format it this way.
$mydate= new DateTime($mydate);
$mydate=date_format($mydate,"d/m/Y");
This works perfect for me.
Now I have a new challenge: I want to build a table in which I have a Date and Time fields which I have to format somehow too. So hereĀ“s my code till now:
if ($arch = $pdo->prepare("SELECT date, time, info FROM tblinfo WHERE idinfo = ?")) {
$arch ->execute(array($idpac));
$data = $arch->fetchAll();
echo '<div class="cool_table" ><table><tr><td>TIME</td><td >DATE</td><td>INFO</td></tr>';
foreach ($data as $row){
echo '<tr>';
foreach ($row as $col){
echo '<td>'.$col.'</td>';
}
echo '</tr>';
}
echo '</table></div>';
}
So this generates a very cool table for me. But I cant seem to figure out how can I format the Date and Time using this code. Can someone help me with this one?
I recommend you format the date directly in the query, e.g.:
SELECT
DATE_FORMAT(date,'%m/%d/%Y') AS formateddate,
TIME_FORMAT(time,'%H:%i') AS formatedtime,
info
FROM tblinfo
WHERE idinfo = ?
EDIT
swapped arguments as from comment by #azirion
Have you tried googling LANGUAGE OBJECT OPERATION which yields PHP DateTime Format, and returns as one of the first results:
http://php.net/manual/en/datetime.format.php
Is that page any help? The technique generalizes. :)