简体   繁体   English

MYSQL:从包含多个数据的一列中检索所有行

[英]MYSQL: Retrieve all rows from one column containing multiple data

I've looked online for about two days and I'm not sure how to ask Google this scenario. 我在网上看了大约两天,但不确定如何向Google问这种情况。 I'm trying to pull all rows from a single column that contain the word 'Helmet'. 我正在尝试从包含单词“头盔”的单个列中拉出所有行。 What I'm getting is only 2 rows. 我得到的只有2行。 There's actually about 10 rows but it contains words like 'Legs, Helmet' The rows that only have the word 'Helmet' are populating correctly. 实际上大约有10行,但其中包含“腿,头盔”之类的单词。仅包含“头盔”一词的行正确填充。 Code I'm using. 我正在使用的代码。

  id | rune_slot
  ---------------------------------
  0  | Shoulders
  ---------------------------------
  1  | Chest
  ---------------------------------
  2  | Main Hand, Off Hand, Ranged
  ---------------------------------
  3  | Helmet
  ---------------------------------
  4  | Legs, Helmet
  ---------------------------------
  5  | Helmet
  ---------------------------------
  6  | Legs, Helmet
  ---------------------------------

As you can see, this is my table. 如您所见,这是我的桌子。 Like I said, I'm getting rows 3 and 4, but I can't seem to get rows 4 and 6 also. 就像我说的那样,我将获得第3和第4行,但似乎也无法获得第4和第6行。

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
      die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM runes WHERE rune_slot LIKE "Helmet"' ;

mysql_select_db('runelist');
$retval = mysql_query( $sql, $conn );
if(!$retval )
{
      die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
      echo "Rune Name : {$row['rune_name']} "." Craft Level :  {$row['craft_level']} "."   Rune Slot : {$row['rune_slot']} ".
      "<br />";
} 
echo "Fetched data successfully\n";
mysql_close($conn);

This is the code I got from http://www.tutorialspoint.com/php/mysql_select_php.htm . 这是我从http://www.tutorialspoint.com/php/mysql_select_php.htm获得的代码。

I've tried as many different select statements that I know of. 我尝试了许多我知道的不同选择语句。 I've tried changing mysql_fetch_array to _assoc and I've browsed the http://www.php.net/manual/en/function.mysql-fetch-assoc.php . 我尝试将mysql_fetch_array更改为_assoc,并浏览了http://www.php.net/manual/en/function.mysql-fetch-assoc.php I'm guessing it's bad practice to put multiple data in a single column? 我猜测将多个数据放在单个列中是不好的做法? Any help would be appreciated, I come here often to learn about PHP and MYSQL, now I'm just stuck. 任何帮助将不胜感激,我经常来这里学习有关PHP和MYSQL的知识,但现在我陷入了困境。 Thanks!! 谢谢!!

You need to use % in your query: 您需要在查询中使用%

  $sql = 'SELECT * FROM runes WHERE rune_slot LIKE "%Helmet%"' ;

which means any combination of Helmet 这意味着Helmet任何组合

You are missing % in your like query. 您在类似查询中缺少%。 Change query to like "%helmet%" 将查询更改为“%helmet%”

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

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