Select from MySql with a variable - mysql

I'm trying to search in a database with python3 with PyMySQL. I want to have a list of ips, that can be put in a database and return that data to the ip address.
But i can't even search for one ip. Is it possible or not ?
ip = "10.0.4.64"
cur_syslog.execute("SELECT data FROM firewall WHERE source_ip = values (%s)",(ip))

Your query is not well written. It should be:
cur_syslog.execute("SELECT data FROM firewall WHERE source_ip = %s", (ip))

Related

Numbers change when querying MySQL/MariaDB through R (RMariaDB)/ Integer conversion in R

I was able to implement a connection from R through RMariaDB and DBI to a remote MariaDB-database. However, I am currently encountering a strange change of numbers when querying the database through R. I'll explain the differences:
I inserted one simple entry in my database with the following command:
INSERT INTO respondent ( id, name ) VALUES ( 2388793051, 'testuser' )
When I connect to this database directly on the remote server and execute a statement like this:
SELECT * FROM respondent;
it delivers these value
id: 2388793051, name: testuser
So I should also be able to connect to the database via R and receive the same results. So when I execute the following code in R, I expect to receive this inserted and saved information displayed above:
library(DBI)
library(RMariaDB)
conn <- DBI::dbConnect(drv=RMariaDB::MariaDB(), user="myusername", password="mypassword", host="127.0.0.1", port="1111", dbname="mydbname")
res <- dbGetQuery(conn, "SELECT * FROM respondent")
print(res)
However, the result of this query is the following
id name
-1906174245 testuser
As you can see, the id is now -1906174245 instead of the saved 2388793051 in the database. I don't understand this weird conversion of integers in the id-field. Can someone explain how this problem emerges and how I might solve it?
EDIT: I don't expect this to be a problem, but just to inform you: I am using an SSH tunnel to enable a connection via these specified ports from my local to my remote machine.
SOLUTION: What made the difference was to specify the id of a respondent in the database specification already as BIGINT instead of INT. Thanks to #JonnyCrunch

How to fix mysql uppercase query in php and mysql

I am currently working on the website that uses ADODB library. In entire website all the queries are written in UPPERCASE.
The problem is when I run the query it doesn't work because of table name which is UPPERCASE. But when I change the table name to lowercase it works.
$sql = "SELECT * FROM MEMBERS where USERNAME = '$username'";
$db = ADONewConnection('mysql');
$db->debug = true;
$db->Connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_NAME);
$resultFriends = $db->Execute($sql);
while ($row = $resultFriends->FetchRow()) {
var_dump($row);
die;
}
Here is the error I get:
ADOConnection._Execute(SELECT * FROM MEMBERS where USERNAME = 'fury', false) % line 1012, file: adodb.inc.php
ADOConnection.Execute(SELECT * FROM MEMBERS where USERNAME = 'fury') % line 15, file: index.php
Bear in mind I don't want to change the scripts. There are 1000 files and 10000 places.
Is there any library or are there any way that I can run this queries without error?
The version for live sire was linux kernel. but the new dev site is ubuntu.
I have done this on ubuntu/ mysql CML and it didn't work.
The solution is I had to reconfigure the mySql database in AWS/rdbs
You have to modify the “lower_case_table_names” parameter for your DB Instance(s). Prior to today, the lower_case_table_names parameter was not modifiable, with a system default of zero (0) or “table names stored as specified and comparisons are case sensitive.” Beginning immediately, values of zero and one (table names are stored in lowercase and comparisons are not case sensitive) are allowed. See the MySQL documentation for more information on the lower_case_table_names parameter.
The lower_case_table_names parameter can be specified via the rds-modify-db-parameter-group API. Simply include the parameter name and specify the desired value, such as in the following example:
rds-modify-db-parameter-group example --parameters "name=lower_case_table_names, value=1, method=pending-reboot" --region us-east-1
Support for modifying parameters via the AWS Management Console is expected to be added later this year.
setting the lower_case_table_names parameter via a custom DB Parameter Group and doing so before creating an associated DB Instance. Changing the parameter for existing DB Instances could cause inconsistencies with point-in-time recovery backups and with Read Replicas.
Amazon RDS

How to get ip address by session in Joomla using sql

Is there any way that I could get the session's ip address in my Joomla website using sql?
As I can see in the session table I only have session_id, client_id, guest, time, data, userid, username.
Is there anyway that I could extract also the IP address of user that has an open session on my website?
Please note that i cannot use JFactory::getSession()->get(...) or any other PHP code. I can only access my server's sql database.
You could set a custom session variable in Joomla like so:
$ip = $_SERVER['REMOTE_ADDR'];
$session = JFactory::getSession();
$session->set('ip', $ip);
Update:
Use the following to get the IP instead of $_SERVER['REMOTE_ADDR'];
https://joomla.stackexchange.com/a/15989/168

simple SQL query to display values from a table based on condition

Assume there is client and a server
Client:: Android mobile
Server:: AWS server
Server has mysql database installed in it
Database name:: Person
Tables in person Database:: "sam" and "carl"
Now mobile sends a request...
How to write an SQL query so that if mobile sends request i/p as
"sam" display all the values of sam table
But if the mobile sends an i/p as carl then dipslay all the values
of carl table
[Note]
I have used statement SELECT * FROM TABLE_NAME
What i am trying to achieve is a condition response when a client request comes in
I/p means :: say i get carl as ip .... i want to perform one query else sam as input ... i want to perform another query
what query statement should i need to write ?
hope i am stating my problem correctly
Thanks
First, you have to get the IP address of the Android client in your server code. In PHP (source):
$client_ip = $_SERVER['REMOTE_ADDR']
Or, if you have multiple Apache web servers behind ELB (source):
$http_headers = apache_request_headers();
$client_ip = $http_headers["X-Forwarded-For"];
Now your PHP code can choose which query to run according to the value of $client_ip. If the address is Sam's, run select * from sam, if it is Carl's, run select * from carl. If it's an unknown IP address, then you can respond with an error message.
Anyway, I don't think IP-based identification is a good idea. If Carl or Sam reboots his Android phone, its IP address will change, and the connection will no longer work.
There are problems with your database design, too. For example, if your current Sam and Carl tables contain chat messages, then the following schema would be better:
- User table: ID, Name, IPAddress
- Message table: ID, UserID, SendingTime, Text
For these tables, the query which lists the messages of the current user would look like this:
SELECT User.Name, Message.SendingTime, Message.Text
FROM User, Message
WHERE User.ID = Message.UserID AND User.IPAddress = 'xxx.xxx.xxx.xxx'
Here 'xxx.xxx.xxx.xxx' would be the value of $client_ip.
For this u have to require one more table where you store IP address and their table name and create query like this
Select tableName from IPTable where ip= 'xxx.xxx.xxx.xxx'
and using that table name in your second query
select * from tablename

MYSQL Query in Database View

SELECT `aversio`.`module`.`module` AS `module`
FROM
`projectmodule`
JOIN `module` ON `aversio`.`projectmodule`.`moduleID` = `aversio`.`module`.`moduleID`
JOIN `project` ON `aversio`.`project`.`projectID` = `aversio`.`projectmodule`.`projectID`
WHERE `aversio`.`module`.`actief` = _utf8'1'
AND `aversio`.`projectmodule`.`verwijderd` = _utf8'0'
AND `aversio`.`projectmodule`.`verwijderd` = _utf8'0'
MYSQL Error : There is no 'aversio'#'%' registered
What this error mean
That actually looks like a permissions issue, like you are trying to connect as aversio to a MySQL server instance which is not configured to allow that user from any (%) domain.
The syntax 'aversio'#'%' looks like the format 'username'#'host', and mysql uses % as the catch-all (any) host wildcard.
Make sure that you create the MySQL user named aversio, and give them the correct permissions on your DB.
EDIT:
IIRC, a user of that name may exist, but with a different domain (i.e. the users table is keyed on the combo of user and host. I've seen this kind of thing happen when I move code from a dev server to a staging server and try to connect fro mthe new host, having forgotten to modify the permissions in MySQL.