How to make MSSQL query in PHP case-sensitive? - php

In a script on our web server, we display users who are online in-game by looking at a MSSQL database for specific values then displaying the members based on such data.
Lets say user login 'blargh' has 3 characters: MasteR, MasTeR, and BlarGh
Lets also say that he has logged in as 'MasteR'
The below script will look for the value of '1' in 'connectstat' field then grab his username along with some other pertinent data, then display it.
The problem lies when someone has two similar in-game names on a single account.
With the above example, this script will show MasteR, and MasTeR are both online, even though only one is actually in-game. I believe this is a case sensitivity issue where when it identifies 'MasteR' as online, it's ignoring the case and believes 'MasTeR' is online as well.
Anyone have ideas on how to isolate this by making it case-sensitive?
$query = 'Select GameIDC, Resets, Class, MapNumber, MapPosX, MapPosY, ServerName, OnlineHours, clevel From MEMB_STAT, AccountCharacter, Character where MEMB_STAT.Connectstat=1 and AccountCharacter.Id=MEMB_STAT.memb___id AND AccountCharacter.GameIDC=Character.Name collate Chinese_PRC_CI_AS order by GameIDC, ServerName desc';
$result = mssql_query($query);
This is on a xampp-based web server on winxp x86 using php5 and MSSQL (ODBC)

case sensitivity comes from collation. so if the collation is set as cs (case sensitive) then your query condition will be CS as well.
however, why dont you try to use group by
ie.
select playername from gameDb where status=1 group by playername;

Related

How to update badly encoded characters in MySql table?

In an existing database, we have discovered some text entries where characters with accents were badly encoded.
The following query:
SELECT
PR.Product_Ref__ AS ProductCode,
CONCAT(P.Name, IF (PR.Misc <> '', CONCAT(' ', PR.Misc), '')) AS Name
FROM
Product AS P
INNER JOIN
Product_Ref AS PR ON P.Product__ = PR.Product__
WHERE
Name like "%é%" AND
PR.Product_Ref__ IN ( 659491, 657274 )
returns two lines describing the issue:
Sometimes, e with accent has been inserted properly, sometimes not.
How can I detect and update such issue with an UPDATE ... WHERE ...? Or should I use a different statement/method?
P.S.: The corresponding application is developed in PHP.
P.S.2: The answers in the suggested possible duplicate question do not address the issue I am raising. It is not related to the collation of the database.
You just can simple detect all the occurrences that you want to correct and the use a simple update clause to make the substitutions.
Example for the case that you describe:
UPDATE Product
SET Name = REPLACE(Name, 'é', 'é')
WHERE Name like '%é%'
You can run this updates directly in mysql databases, using the command line or in a specific mysql user interface client application. Or if you like, using php functions that run sql statements in the database.

MySQL 5.7.9 fulltext research in multiple tables with Sphinx on WAMP

I I have to do a research in multiple MySQL tables for my internship.
In fact, I do this for a web phone directory. I have a form text input to enter the research.
I tried to use the MATCH/AGAINST syntax but it appears to be wrong.
My query is actually that one :
SELECT U_ID, 'users'
from users
where match ([columns that I want to search in]) AGAINST ([The text inside my search field])
UNION
SELECT S_ID, 'service'
from service
where match ([columns that I want to search in]) AGAINST ([The text inside my search field])
This problem is the following : With this type of search, I must send the variable in many MATCH so I can't have a relevant result (because of the multiple against elements). The perfect solution could be to replace the 'UNION' by an 'INTER' but It would be too easy.
I don't know if it will be usefull, but I use PDO to send my query with PhP
I tried to search solutions but I couldn't find one for me :
https://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html
Using Full-Text Search in SQL Server 2008 across multiple tables, columns
MySQL fulltext search on multiple tables with different fields
Then I tried to use Sphinx but the documentation is complicated to me and I couldn't understand it (http://sphinxsearch.com/docs/current.html).
Can someone help me to find the query that I need or can you give me a link of a very clear and simple Sphinx tutorial on Windows (I have read the IBM one) ?
Edit :
I wanted to illustrate my problem with the set theory (inter mean intersection).
For example, when I type "John accounting department" in my form input, I want to display all users named John, but only if they belong to the accounting department.
With my actual search, I will have the id of all the departments named : "John" or "accounting" or "department" and all the id for the people named "John" or accounting" or department".
That's my actual problem.
I think a materialized view would be the easiest way to solve this in MySQL.
They not strictly a feature of mysql (ie mysql cant maintain the view automatically, its not really a view) its a normal table, that just happens to be a copy of other table(s) data.
Can actually be created quite easily...
CREATE TABLE my_search_view (fulltext(U_Name,S_Name))
SELECT U_ID, U_Name, S_ID, S_Name
FROM user
INNER JOIN service USING (S_ID);
(If you update the source tables, just delete the table, and recreate it! Although can use triggers if want a more dynamic solution, but depending size of data it possibly not worth the effort)
Now you can just run a simply query against this table (it has a full-text index)
SELECT *
FROM my_search_view
WHERE MATCH (U_Name,S_Name) AGAINST ('John accounting department')

Mysql INNER JOIN and Resource #9

I am working on a UCP(User control panel) where a user loggs in by an unique ID that is given at registration.
My problem is i have two tables in my database:
serverplayers - Holds user ID and name and all of his info
and
vehicles - holds vehicle owner and vehicle model.
So what I need to do is connect those two together by users "Username".I tried Mysql INNER JOIN like this.
$car = mysql_query('SELECT vehicles.vOwner, vehicles.vModel
FROM vehicles
INNER JOIN serverplayers ON vehicles.vOwner = serverplayers.User
WHERE vehicles.vOwner = '.$ro['User'].'');
After this I get Recourse #9, i read that it's not an error it's just that the result is empty?I may have error's or problems in my mysql query so please tell everything whats wrong.
But what's the problem I think is that a vehicle is only connected by it's owner's name and doesn't contain UserID.So what I need is basically show a User what car he has by Connecting his username with vOwner.I'm not a very bright boy when it comes to mysql or php I'm just learning and I came across this thing as mysql INNER JOINS that I think is very usefull(but the main reason is that I don't want to recode the whole server and the UCP).
Vehicles:
ServerPlayers:
ServerPlayers table http://img94.imageshack.us/img94/3369/qebq.png
I'm not sure what $ro['User'] is, but it sounds like an ID based on your question. In that case I would just switch vehicles.vOwner to serverplayers.UserID in that comparison. I would also highly recommend changing your schema so vOwner is the ID and not the username (what if the username changes in one of the tables?) It also doesn't look like you need to join on serverplayers for anything.
The "resource" is a mysql results resource. You can't get anything out of it just by looking at it -- you need to run fetch operations on it:
$row = mysql_fetch_assoc($car);
I recommend using PDO or mysqli over the deprecated ext/mysql.

Perform accent insensitive fulltext search MySQL

I'm currently developing a search functionality for a website. Users search for other users by name. I'm having some trouble getting good results for users that have accents on their name.
I have a FULLTEXT index on the name column and the table's collation is utf8_general_ci.
Currently if somebody registers for the site, and has a name with accents (for example: Alberto Andrés), the name is stored in the DB as shown in the following image:
So if I perform the following query SELECT * MATCH(name) AGAINST('alberto andres') I get lots of results with better match scores like 'Alberto', 'Andres', 'Andrés' and finally with a low match score the record the user is probably looking for 'Alberto Andrés'.
What could I do to take into account the way accented records are currently stored in the DB?
Thanks!
It looks to me like the surname of el Señor Andrés is actually stored correctly. The rendering you showed us is the way some non-UTF apps mangle UTF8 text.
You might try this modification of your query if you don't yet have a whole bunch of records in your table. Fulltext (non-boolean) mode works weirdly on small data sets.
SELECT *
FROM TABLE
WHERE MATCH(name) AGAINST('alberto andres' IN BOOLEAN MODE)
You also might try
SELECT *
FROM TABLE
WHERE MATCH(name) AGAINST(CONVERT('alberto andres' USING utf8))
just to make sure your match string is in the same character set as your MySQL columns.

How to Concat in mysql Query

Im not even sure if this is possible (Im new to php)
Anyway, what I want to do is this:
mysql_query("SELECT * FROM user_table WHERE concat(username,'#',domain)='$username' LIMIT=1");
Ok, so the $username is an email address that is submitted by a user to search the database to check that they exist. In the user_table usernames are not stored in a single column, they are stored in several with the domain and the actual username being separate.
for example username could be bob and the domain could be website.com.au
Then when the user wants to search for that user the type in bob#website.com.au
This goes to the query above.
So, should it work or not? If not how can I make this work or what suggestions do you have for me?
As BobbyJack has mentioned, this is a slow way of locating a user record.
If you cannot store email address in a single column and place an index on that column, split the string in PHP and make your query:
SELECT * FROM user_table WHERE `username` = '$username' AND `domain` = '$domain'
You could then create a unique index combining domain + username so you wouldn't need LIMIT 1
probably worded the question slightly wrong.
Anyway this is what I have done "SELECT * FROM virtual_user WHERE concat_ws('#',username,domain)='$username'"
I no longer need to use the LIMIT=1, I probably never needed to as all results in the table are individual, so it will always only return a limit of 1 or nothing at all.
It isn't slow in my opinion, but then again Im not really sure what to compare it to. We have about 7000+ records it sorts through so yeah. Is there anyway to get it to tell you how long the query took to complete?
I would like to put both the username and domain into just a single indexed field but its for a postfix mail server and I'm not allowed or game to play with the queries it uses. Especially not on a functioning server that actually handles mail.

Categories