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

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

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

Anti MultiAccount [MySQL]

I'm creating Anti MultiAccount in MySQL.I have big problems with MultiAccounts in my game so I need strong security.
Now I'm make that every connection in my game log in mysql.With information about ip and time login.
Every time when player connect on my server I use this.
INSERT INTO MULTI(IP, Name,Country,date) VALUES ('127.0.0.1', 'Test' , 'Croatia' ,UNIX_TIMESTAMP())
Now I don't know how that Admins can see possible multiaccounts so I need:
1) How to get all user IP from MULTI table but without repetition same IP's
2) How to get all connected players but without repetition
For example:
A player is login with IP adress 127.0.0.1
B player is also login with 127.0.0.1 IP adress
So A and B player are conncted!They are possible multiaccounts,how to get them(But without repetition of course)
(Sorry for English)
Not sure what exactly do you want to select, but here is my approach:
http://sqlfiddle.com/#!9/91208b/1
SELECT DISTINCT multi.* FROM multi
INNER JOIN
(SELECT ip
FROM multi
GROUP BY ip
HAVING COUNT(DISTINCT name)>1
) filter
ON multi.ip = filter.ip

Look up my current userID with mySql

This scripts selects an account form my MySQL database with the userID 1:
$_SESSION['user_id'] = 1;
If my MySQL account has id 1 then it will change that account.
I wonder how you can change the = 1; so it automatically looks up which account you are on at the moment? Right now it only works with the account with userID 1.
MySQL database name for my userid is "userID".
Usually you would say something like
SELECT * FROM user_profiles WHERE user_id=1 in MySQL.
Also, I would recommend that you use PDO and properly escape the user input using
htmlspecialchars($_SESSION['user_id'])
in your PHP file since the user_id input most likely can be manipulated by the user.
You are using Session so it can not change the userID unless you destroy that session by using this PHP function:
session_destroy();
And if you want to know your current userID:
echo $_SESSION['user_id'];

Dynamic access to a server by server name

In SQL Server (i'm using 2008) is it possible to dynamically access server by server name?
My scenario: I have a production server, a development server, and a test server. Their structure is the same. There is a fourth server with some additional data - let's call it a data server.
On the data server there is a procedure. One of it's parameters is a name of the requesting server:
proc sp_myProcedure(#myId int, #serverName nvarchar(100))
The procedure accesses tables from the data server and from the requesting server. At the moment, to query the requesting server I'm using a case expression:
-- code on the data server
select additionalData = case #serverName
-- if the requesting server is production - query production
when 'ProdServer' then (select field1 from [ProdServer].[MyDataBase].[dbo].[MyTable] ...
-- if the requesting server is test - query test
when 'TestServer' then (select field1 from [TestServer].[MyDataBase].[dbo].[MyTable] ...
-- if the requesting server is development - query development
when 'DevServer' then (select field1 from [DevServer].[MyDataBase].[dbo].[MyTable] ...
end
My question is if there is any other way to access the requesting server. I'd like to replace ifs and cases with something more dynamic. Is it, for instance, possible to use the server name variable to dynamically access specific server. Something similar to the following (mocked) query:
declare myServer <server type> = Get_Server(#serverName)
-- the query
additionalData = select field1 from [myServer].[MyDataBase].[dbo].[MyTable]
I liked this approach
SELECT
SERVERPROPERTY('MachineName') AS [ServerName],
SERVERPROPERTY('ServerName') AS [ServerInstanceName],
SERVERPROPERTY('InstanceName') AS [Instance],
SERVERPROPERTY('Edition') AS [Edition],
SERVERPROPERTY('ProductVersion') AS [ProductVersion],
Left(##Version, Charindex('-', ##version) - 2) As VersionName
Link
Another approach which we were using was
Creating one database called database_yourprojectname
So, for the explanation I'm using database name as northwind
after that you can create one new database called northwind_db
Which has a following fields:
Servername,username(encrypted),password(encrypted),active
And then you can either make one page to insert/update/delete current database used there
or you can add statically data to it..so, you can use the database which is active currently.
Or use simple one:
SELECT ##SERVERNAME
Which is already stated here

SELECTing in a WHERE clause, inside a SELECT statement

I'm not exactly sure on the correct technical wording, so excuse my title, but here's the problem. I have a MySQL database, and in the user table I have *user_name*, a *password_salt*, and an md5 password containing the password then salt. In a program, users connect and I get one query to send to validate a user.
When a user connects I need a way of selecting their user_name, and comparing the given password to the stored password, which requires retrieving the salt somewhere in the WHERE statement (I guess).
This is my hypothetical "example":
SELECT user_name
FROM users
WHERE user_name='$nick' AND
password = md5(CONCAT('$md5pass', md5((select password_salt FROM users where user_name='$nick'))))
LIMIT 1
Resolution Update: Got it working, thanks for the suggestions, a normal select sufficed, the problem was that the sql-auth api wasn't receiving the password unless the port was specified.
Actually you can freely use any column from table declared in "FROM" clause not only in "SELECT" clause, but also in "WHERE" clause, so I don't see a need to subquery here. Let it be simply:
SELECT user_name
FROM users
WHERE user_name='$nick' AND
password = md5(CONCAT('$md5pass', md5(password_salt)))
LIMIT 1
This way a row is selected only if it matches both:
- user name is correct
- the password in row matches given password
I am not sure though if I used md5() functions correctly. I copied your example.
SELECT user_name
FROM users
WHERE user_name='$nick' AND
password = md5(CONCAT('$md5pass', password_salt))
LIMIT 1
Try this instead:
SELECT user_name
FROM users
WHERE user_name='$nick' AND
password = md5(CONCAT('$md5pass', md5(password_salt)))
LIMIT 1