SELECT statement issue with php - linux

+--------------+------+
| IP | Say |
+--------------+------+
| 192.168.1.1 | 1 |
+--------------+------+
$con = mysqli_connect("$host", "$user", "$pass", "$db_name") or die("cannot connect");
$Q1 = "SELECT Say From spam_engel WHERE IP = '192.168.1.1'";
$ol = mysqli_query($con, $q1);
echo gettype($ol);
Thing printed is "NULL". But it should be printing "int"...
What might be the problem?

php variables are case-sensitive:
Variables in PHP are represented by a dollar sign followed by the name
of the variable. The variable name is case-sensitive.
so in your code:
$Q1 = "SELECT Say From spam_engel WHERE IP = '192.168.1.1'";
$ol = mysqli_query($con, $q1);
here you are using $Q1 then passing to your mysql_query() the lower case variable $q1;
to fix this unify the case of your variable names as follows:
$q1 = "SELECT Say From spam_engel WHERE IP = '192.168.1.1'";
$ol = mysqli_query($con, $q1);

Related

Substring search on one column from the column of another table without IN operator

I have two tables in Azure one of which is a list of URLs and the other has only Domain Names. I want to be able to check if the the URLs in the URLtable "contains" the domain name from the DomainName_table. The "in" operator cannot be used since there will never be an exact match.
Dummy tables below:
let DomainName_table= datatable (domainname: string)
[
"abc456",
"gmail"
]
|summarize domainlist = make_list(domainname);
let URLtable= datatable (URL: string)
[
"abc456/.com/ffsfd/sdfsdfds",
"gmail",//.com/sAFSfS"
"gmddail.com"///sAFfsdfsfSfS"
];
URLtable
| where URL in (DomainName_table)
I also tried to split the URL to extract the domain name :
let DomainName_table= datatable (domainname: string)
[
"abc456",
"gmail"
]
|summarize domainlist = make_list(domainname);
let URLtable= datatable (URL: string)
[
"https://abc456.com/ffsfd/sdfsdfds",
"https://gmail.com/sAFSfS"
"https://gmddail.com/sAFfsdfsfSfS"
];
URLtable
|extend split_url = split(URL,"/",2)//| project split_url
| where split_url in (DomainName_table)
This is also not a great way since it can also be "xyz.abc456.com" and it won't return a match. Will almost always return a 0 since the URL can never be an exact match.
Also there are no common columns between the two to use in a join.
Basically a substring search on one column from the column of another table.
Can anyone please suggest how I can do this? Thank you for your KQL-fu.
There are two different approaches to solve this:
Approach #1 - this approach will work if LookupDomains has no more than 1,000,000 records:
let Urls = datatable(url: string) [
"happydomain.com",
"a.happydomain.com",
"b.happydomain.com",
"angrydomain.com",
"a.angrydomain.com",
"q1.a.angrydomain.com",
"q2.a.angrydomain.com",
"b.angrydomain.com",
"q1.b.angrydomain.com",
"q2.b.angrydomain.com",
"surpriseddomain.co.il",
"a.surpriseddomain.co.il",
"b.surpriseddomain.co.il",
"q1.a.surpriseddomain.co.il",
"q2.b.surpriseddomain.co.il",
];
let LookupDomains = datatable(domain: string) [
"happydomain.com",
"a.angrydomain.com",
"q1.a.surpriseddomain.co.il"
];
Urls
| extend dl = split(url, ".")
| extend dl1 = tostring(dl[-1])
| extend dl2 = strcat(dl[-2], ".", dl1)
| extend dl3 = strcat(dl[-3], ".", dl2)
| extend dl4 = strcat(dl[-4], ".", dl3)
| extend dl5 = strcat(dl[-5], ".", dl4)
| extend LoopupDomain =
case(dl1 in (LookupDomains), dl1,
dl2 in (LookupDomains), dl2,
dl3 in (LookupDomains), dl3,
dl4 in (LookupDomains), dl4,
dl5 in (LookupDomains), dl5,
"")
| where isnotempty(LoopupDomain)
| project-away dl*
Output:
url
LoopupDomain
happydomain.com
happydomain.com
a.happydomain.com
happydomain.com
b.happydomain.com
happydomain.com
a.angrydomain.com
a.angrydomain.com
q1.a.angrydomain.com
a.angrydomain.com
q2.a.angrydomain.com
a.angrydomain.com
q1.a.surpriseddomain.co.il
q1.a.surpriseddomain.co.il
If the LookupDomains table has more than 1,000,000 records, then the in approach above won't work, and the next approach will have to be used instead.
Approach #2 - this approach will work regardless of the number of records in LookupDomains, but requires a little more work from your side:
First you'll need to add dl2 as a separate column in the Urls and LookupDomains tables (this can be done as part of your ingestion flow, or using an Update policy).
Then you'll need to use the following query instead of the query I wrote above:
LookupDomains
| join kind=inner Urls on dl2
| extend dl = split(url, ".")
| extend dl1 = tostring(dl[-1])
| extend dl3 = strcat(dl[-3], ".", dl2)
| extend dl4 = strcat(dl[-4], ".", dl3)
| extend dl5 = strcat(dl[-5], ".", dl4)
| where (dl1 == domain) or (dl2 == domain) or (dl3 == domain) or (dl4 == domain) or (dl5 == domain)
| project-away dl*

add entry in hashtable from string in Powershell

I have a string like this, retrieved from a txt file:
$txtindex = 5
$PRINTERNAME = get-content $txtPrinterfile | select -Index $txtindex
$PRINTERNAME = $PRINTERNAME.Trim()
$PRINTERNAME = $PRINTERNAME.Replace(" ","")
$Printername
NAME="W018"
So I thought I could easily add this to a hashtable:
$HashPrinter = #{}
$HashPrinter.Add($PRINTERNAME)
but this is not working.
Either this:
$HashPrinter = #{$PRINTERNAME}
If I type it manually:
$HashPrinter = #{NAME="W018"}
it works as expected.
What I am doing wrong?
PS C:\Users\alHaos> $HashTable = #{}
PS C:\Users\alHaos> $HashTable.Add("Name", "Pr001")
PS C:\Users\alHaos> $HashTable.Add("Manufacture", "Epson")
PS C:\Users\alHaos> $HashTable
Name Value
---- -----
Name Pr001
Manufacture Epson

how can i extract a string or pattern from a result set

I included an oracle command
$DOMAIN_HOME/reports/bin/rwdiag.sh -findall
in a linux script that does a check of our env and returns a sample output below
(1) Name = rep_wls_reports_orclas4t : Type = server : Host =
(2) Name = rep_wls_reports_ias10t_frasinst_1 : Type = server : Host
=
(3) Name = rptsvr_orclas5p_frasinst_1 : Type = server : Host =
...
(a list of existing report servers available for use in our ERP)
I am trying to extract just the portion **Name = rep****** and report just that only in my output. How can I use sed and/or grep to achieve this.
Many thanks for any assistance.
Regards
you can use the awk
$DOMAIN_HOME/reports/bin/rwdiag.sh -findall | awk -F' ' $'{print $2,$3,$4}'
With this command you use the string like a list separate by space(defined with -F).

Split a string on sqlite

i don't know sqlite but I have to implement a database already done. I'm programming with Corona SDK. The problem: i have a column called "answers" in this format: House,40|Bed,20|Mirror,10 ecc.
I want to split the string and remove "," "|" like this:
VARIABLE A=House
VARIABLE A1=40
VARIABLE B=Bed
VARIABLE B1=20
VARIABLE C=Mirror
VARIABLE C1=10
I'm sorry for my english. Thanks to everybody.
Try this:
If you want to simply remove the characters, then you can use the following:
Update 3 :
local myString = "House;home;flat,40|Bed;bunk,20|Mirror,10"
local myTable = {}
local tempTable = {}
local count_1 = 0
for word in string.gmatch(myString, "([^,|]+)") do
myTable[#myTable+1]=word
count_1=count_1+1
tempTable[count_1] = {} -- Multi Dimensional Array
local count_2 = 0
for word_ in string.gmatch(myTable[#myTable], "([^,|,;]+)") do
count_2=count_2+1
local str_ = word_
tempTable[count_1][count_2] = str_
--print(count_1.."|"..count_2.."|"..str_)
end
end
print("------------------------")
local myTable = {} -- Resetting my table, just for using it again :)
for i=1,count_1 do
for j=1,#tempTable[i] do
print("tempTable["..i.."]["..j.."] = "..tempTable[i][j])
if(j==1)then myTable[i] = tempTable[i][j] end
end
end
print("------------------------")
for i=1,#myTable do
print("myTable["..i.."] = "..myTable[i])
end
--[[ So now you will have a multidimensional array tempTable with
elements as:
tempTable = {{House,home,flat},
{40},
{Bed,bunk},
{20},
{Mirror},
{10}}
So you can simply take any random/desired value from each.
I am taking any of the 3 from the string "House,home,flat" and
assigning it to var1 below:
--]]
var1 = tempTable[1][math.random(3)]
print("var1 ="..var1)
-- So, as per your need, you can check var1 as:
for i=1,#tempTable[1] do -- #tempTable[1] means the count of array 'tempTable[1]'
if(var1==tempTable[1][i])then
print("Ok")
break;
end
end
----------------------------------------------------------------
-- Here you can print myTable(if needed) --
----------------------------------------------------------------
for i=1,#myTable do
print("myTable["..i.."]="..myTable[i])
end
--[[ The output is as follows:
myTable[1]=House
myTable[2]=40
myTable[3]=Bed
myTable[4]=20
myTable[5]=Mirror
myTable[6]=10
Is it is what you are looking for..?
]]--
----------------------------------------------------------------
Keep coding............. :)

Powershell Script loop output to txt file

I am using a Powershell script to write to a text file. A client showed me this Powershell script to use to replace a excel macro I used to use...
$computers= gc "C:\Users\Powershell\DeviceList.txt"
foreach ($computername in $computers)
{
write-output "<$computername>
active = yes
group =
interval = 5min
name = $computername
host = $computername
community =
version = 1
timeout = 0
retries = default
port = 161
qos_source = 1
</$computername>" | Out-File -filepath "C:\Users\Powershell\Cisco_Mon.txt" -append
}
It works great but now I wanted to build on it to add additional variables. In a perfect world I would like it to read from an excel spreadsheed grabbing each rowof data and each column being defined as a variable. For now using another text file is fine as well. Here is what I started with (it doesnt work) but you can see where I am going with it...
$computers= gc "C:\Users\Powershell\devicelist.txt"
$groups= gc "C:\Users\Powershell\grouplist.txt"
foreach ($computername in $computers) + ($groupname in $groups)
{
write-output "<$computername>
active = yes
group = $groupname
interval = 5min
name = $computername
host = $computername
community =
version = 1
timeout = 0
retries = default
port = 161
qos_source = 1
</$computername>" | Out-File -filepath "C:\Users\Powershell\Cisco_Mon.txt" -append
}
Of course it is not working. Essentially I would LOVE it if I could define each of the above options into a variable from an excel spreadsheet, such as $community, $interval, $active, etc.
Any help with this would be very much appreaciated. If someone could show me how to use an excel spreadsheet, have each column defined as a variable, and write the above text with the variables, that would be GREAT!!!.
Thanks,
smt1228#gmail.com
An Example of this would be the following...
Excel Data: (Colums seperated with "|"
IP | String | Group
10.1.2.3 | Public | Payless
Desired Output:
<10.1.2.3>
active = yes
group = Payless
interval = 5min
name = 10.1.2.3
host = 10.1.2.3
community =
version = 1
timeout = 0
retries = default
port = 161
qos_source = 1
</10.1.2.3>
Addition:
Pulling data from CSV for IP, String, Group where data is as follows in CSV...
10.1.2.3,public,group1
10.2.2.3,default,group2
10.3.2.3,public,group3
10.4.2.3,default,group4
to be writting into a .txt file as
IP = 10.1.2.3.
String = public
Group = Group1
and look for each line in the CSV
Ok, new answer now. The easiest way would be to save your Excel document as CSV so that it looks like this (i.e. very similar to how you presented your data above):
IP,String,Group
10.1.2.3,Public,Payless
You can still open that in Excel, too (and you avoid having to use the Office interop to try parsing out the values).
PowerShell can parse CSV just fine: with Import-Csv:
Import-Csv grouplist.csv | ForEach-Object {
"<{0}>
active = yes
group = {1}
interval = 5min
name = 10.1.2.3
host = 10.1.2.3
community =
version = 1
timeout = 0
retries = default
port = 161
qos_source = 1
</{0}>" -f $_.IP, $_.Group
}
I'm using a format string here where {0}, etc. are placeholders. -f then is the format operator which takes a format string on the left and arguments for the placeholders on the right. You can also see that you can access the individual columns by their name, thanks to Import-Csv.

Resources