简体   繁体   English

检查mysql列中是否存在值

[英]Check if a value exists in mysql column

Is there a way to check if a value exists in a mysql column? 有没有一种方法可以检查mysql列中是否存在值? I have table songs, and there are some columns, one of them is called 'agent_ip' where i will put a list/array of all user ip's that will visit the site. 我有台式歌曲,并且有一些列,其中一列称为“ agent_ip”,我将在其中放置将访问该站点的所有用户ip的列表/数组。 I need to check if current user ip is present in column 'agent_ip'. 我需要检查“ agent_ip”列中是否存在当前用户ip。 Here is some of my code: 这是我的一些代码:

public function voteSong($song_id, $case, $agent_ip) {           
    $query = $this->link->prepare("SELECT * FROM songs WHERE id = ? LIMIT 1"); 
    $query->bindValue(1, $song_id);
    $query->execute();
    $rowcount = $query->rowCount();        

    if ($rowcount != 0)
    {
        if (!in_array($agent_ip, $r['ip']))
        {
            if ($case === 'like')
            {
                while($r = $query->fetch())
                {
                    $vote = $r['votes'] + 1;
                }
            } 
            elseif ($case === 'dislike')
            {
                while ($r = $query->fetch())
                { 
                    if ($r['votes'] > 0)
                    {
                        $vote = $r['votes'] - 1;
                    }
                    else
                    {
                        $vote = 0;
                    }
                }
            }
            $query = $this->link->prepare("UPDATE songs SET datetime = ?, votes = ?, agent_ip = ? WHERE id = ?"); 
            $query->execute(array(date("Y-m-d H:i:s"), $vote, $agent_ip, $song_id));
        }
    }
}

The line if(!in_array($agent_ip, $r['ip'])) contains the wrong function which won't work, but i need an alternative for mysql. if(!in_array($agent_ip, $r['ip']))包含错误的函数,该函数不起作用,但我需要mysql的替代方法。 $r['ip'] variable is data from the 'agent_ip' column which look like this 127.0.0.1, 127.0.0.1, 127.0.0.1 (using 127.0.0.1 just for example, every 127.0.0.1 is a different ip) $ r ['ip']变量是来自'agent_ip'列的数据,看起来像这样127.0.0.1、127.0.0.1、127.0.0.1(例如,使用127.0.0.1,每个127.0.0.1是不同的ip)

If you're only checking against a single IP, why don't you just modify your query from: 如果仅根据单个IP进行检查,为什么不只修改以下内容的查询:

"SELECT * FROM songs WHERE id = ? LIMIT 1"

To: 至:

"SELECT * FROM songs WHERE id = ? AND agent_ip = ? LIMIT 1"

It seems a bit wasteful to query your whole result set when you are only querying against a specific IP and returning a single row. 当您只查询特定的IP并返回一行时,查询整个结果集似乎有点浪费。

EDIT: Your current method would be extremely inefficient, you are passing a unique agent_ip each time you want to query a song to check if the IP exists, that would be fine, but you are creating a new DB connection every time from which you pull back all info which belongs to that song. 编辑:您当前的方法效率极低,每次您要查询歌曲以检查IP是否存在时,您都传递一个唯一的agent_ip ,那很好,但是您每次从中拉出时都在创建一个新的数据库连接返回属于该歌曲的所有信息。

Lets say we have 1 song, and 3IP's, currently the application would work like this: 假设我们有1首歌曲和3IP,目前该应用程序将像这样运行:

1) Call the method, passing IP_1
2) Query the database getting all songs for ID1
3) Check if IP_1 is in the result set and do process

4) Call the method, passing IP_2
5) Query the database getting all songs for ID1
6) Check if IP_2 is in the result set and do process

7) Call the method, passing IP_3
8) Query the database getting all songs for ID1
9) Check if IP_2 is in the result set and do process

As you can see, there is a lot of repetition here which is going to hinder your apps performance as it scales, you would be so much better modifying your current function to accept a list of results for a song which is pre-queried only once and then recursively call a check function by passing that result array with your unique IP address. 如您所见,这里有很多重复,这会阻碍您的应用在扩展时的性能,您最好修改当前功能以接受仅查询一次的歌曲的结果列表然后通过传递带有唯一IP地址的结果数组来递归调用检查函数。

UPDATE You stated I understand that i need to have 2 tables(1 = songs; 2 = votes). But i cannot imagine how i will get songs from database, arranged by votes quantity. 更新您说I understand that i need to have 2 tables(1 = songs; 2 = votes). But i cannot imagine how i will get songs from database, arranged by votes quantity. I understand that i need to have 2 tables(1 = songs; 2 = votes). But i cannot imagine how i will get songs from database, arranged by votes quantity.

You should read SQL's JOIN documentation, the concept is simple - JOIN allows you to pull back a more detailed set of information based on what you want to query, in your example you may want to find out how many votes a specific song has. 您应该阅读SQL的JOIN文档,这个概念很简单-JOIN允许您根据要查询的内容提取更详细的信息集,在您的示例中,您可能希望找出特定歌曲的票数。

Your tables may look like: 您的表可能如下所示:

Songs 
SONG_ID     Primary Key
SONG_TITLE  
SONG_DURATION
SONG_TAGS

Votes
VOTE_ID     Primary Key
SONG_ID     Foreign Key - (references the song_id table)
VOTE_RES    Bool (either 0 for no, 1 for yes)
AGENT_IP    Who sent the vote

You could then find out how many people said they liked the song by performing a join: 然后,您可以通过加入来找出有多少人说他们喜欢这首歌:

SELECT * FROM songs 
JOIN votes 
ON songs.song_id = votes.song_id 
WHERE songs.song_id = 1 
AND votes.vote_res = 1;

This would return all the song with the id of 1 and all of its associated likes. 这将返回ID为1的所有歌曲及其所有相关的喜欢。 Hope that helps a bit :) 希望那有所帮助 :)

First you need to deserialize/decode the data from the column to the proper php array and then you can use in_array function. 首先,您需要将列中的数据反序列化/解码为适当的php array ,然后可以使用in_array函数。 In your post edit you stated that you have a comma separated list of IP's, so to convert it to array you need to use an explode function: 在您的帖子编辑中,您声明有一个用逗号分隔的IP列表,因此要将其转换为数组,需要使用explode函数:

$ip_list = explode(', ', $r['ip']);

now you can use the in_array function on the new array: 现在您可以在新数组上使用in_array函数:

if(!in_array($agent_ip, $ip_list))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM